You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

642 lines
16 KiB

  1. // Copyright 2014 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage_test
  15. import (
  16. "fmt"
  17. "hash/crc32"
  18. "io"
  19. "io/ioutil"
  20. "log"
  21. "os"
  22. "time"
  23. "cloud.google.com/go/storage"
  24. "golang.org/x/net/context"
  25. "google.golang.org/api/iterator"
  26. "google.golang.org/api/option"
  27. )
  28. func ExampleNewClient() {
  29. ctx := context.Background()
  30. // Use Google Application Default Credentials to authorize and authenticate the client.
  31. // More information about Application Default Credentials and how to enable is at
  32. // https://developers.google.com/identity/protocols/application-default-credentials.
  33. client, err := storage.NewClient(ctx)
  34. if err != nil {
  35. // TODO: handle error.
  36. }
  37. // Use the client.
  38. // Close the client when finished.
  39. if err := client.Close(); err != nil {
  40. // TODO: handle error.
  41. }
  42. }
  43. // This example shows how to create an unauthenticated client, which
  44. // can be used to access public data.
  45. func ExampleNewClient_unauthenticated() {
  46. ctx := context.Background()
  47. client, err := storage.NewClient(ctx, option.WithoutAuthentication())
  48. if err != nil {
  49. // TODO: handle error.
  50. }
  51. // Use the client.
  52. // Close the client when finished.
  53. if err := client.Close(); err != nil {
  54. // TODO: handle error.
  55. }
  56. }
  57. func ExampleBucketHandle_Create() {
  58. ctx := context.Background()
  59. client, err := storage.NewClient(ctx)
  60. if err != nil {
  61. // TODO: handle error.
  62. }
  63. if err := client.Bucket("my-bucket").Create(ctx, "my-project", nil); err != nil {
  64. // TODO: handle error.
  65. }
  66. }
  67. func ExampleBucketHandle_Delete() {
  68. ctx := context.Background()
  69. client, err := storage.NewClient(ctx)
  70. if err != nil {
  71. // TODO: handle error.
  72. }
  73. if err := client.Bucket("my-bucket").Delete(ctx); err != nil {
  74. // TODO: handle error.
  75. }
  76. }
  77. func ExampleBucketHandle_Attrs() {
  78. ctx := context.Background()
  79. client, err := storage.NewClient(ctx)
  80. if err != nil {
  81. // TODO: handle error.
  82. }
  83. attrs, err := client.Bucket("my-bucket").Attrs(ctx)
  84. if err != nil {
  85. // TODO: handle error.
  86. }
  87. fmt.Println(attrs)
  88. }
  89. func ExampleBucketHandle_Update() {
  90. ctx := context.Background()
  91. client, err := storage.NewClient(ctx)
  92. if err != nil {
  93. // TODO: handle error.
  94. }
  95. // Enable versioning in the bucket, regardless of its previous value.
  96. attrs, err := client.Bucket("my-bucket").Update(ctx,
  97. storage.BucketAttrsToUpdate{VersioningEnabled: true})
  98. if err != nil {
  99. // TODO: handle error.
  100. }
  101. fmt.Println(attrs)
  102. }
  103. // If your update is based on the bucket's previous attributes, match the
  104. // metageneration number to make sure the bucket hasn't changed since you read it.
  105. func ExampleBucketHandle_Update_readModifyWrite() {
  106. ctx := context.Background()
  107. client, err := storage.NewClient(ctx)
  108. if err != nil {
  109. // TODO: handle error.
  110. }
  111. b := client.Bucket("my-bucket")
  112. attrs, err := b.Attrs(ctx)
  113. if err != nil {
  114. // TODO: handle error.
  115. }
  116. var au storage.BucketAttrsToUpdate
  117. au.SetLabel("lab", attrs.Labels["lab"]+"-more")
  118. if attrs.Labels["delete-me"] == "yes" {
  119. au.DeleteLabel("delete-me")
  120. }
  121. attrs, err = b.
  122. If(storage.BucketConditions{MetagenerationMatch: attrs.MetaGeneration}).
  123. Update(ctx, au)
  124. if err != nil {
  125. // TODO: handle error.
  126. }
  127. fmt.Println(attrs)
  128. }
  129. func ExampleClient_Buckets() {
  130. ctx := context.Background()
  131. client, err := storage.NewClient(ctx)
  132. if err != nil {
  133. // TODO: handle error.
  134. }
  135. it := client.Bucket("my-bucket")
  136. _ = it // TODO: iterate using Next or iterator.Pager.
  137. }
  138. func ExampleBucketIterator_Next() {
  139. ctx := context.Background()
  140. client, err := storage.NewClient(ctx)
  141. if err != nil {
  142. // TODO: handle error.
  143. }
  144. it := client.Buckets(ctx, "my-project")
  145. for {
  146. bucketAttrs, err := it.Next()
  147. if err == iterator.Done {
  148. break
  149. }
  150. if err != nil {
  151. // TODO: Handle error.
  152. }
  153. fmt.Println(bucketAttrs)
  154. }
  155. }
  156. func ExampleBucketHandle_Objects() {
  157. ctx := context.Background()
  158. client, err := storage.NewClient(ctx)
  159. if err != nil {
  160. // TODO: handle error.
  161. }
  162. it := client.Bucket("my-bucket").Objects(ctx, nil)
  163. _ = it // TODO: iterate using Next or iterator.Pager.
  164. }
  165. func ExampleBucketHandle_AddNotification() {
  166. ctx := context.Background()
  167. client, err := storage.NewClient(ctx)
  168. if err != nil {
  169. // TODO: handle error.
  170. }
  171. b := client.Bucket("my-bucket")
  172. n, err := b.AddNotification(ctx, &storage.Notification{
  173. TopicProjectID: "my-project",
  174. TopicID: "my-topic",
  175. PayloadFormat: storage.JSONPayload,
  176. })
  177. if err != nil {
  178. // TODO: handle error.
  179. }
  180. fmt.Println(n.ID)
  181. }
  182. func ExampleBucketHandle_LockRetentionPolicy() {
  183. ctx := context.Background()
  184. client, err := storage.NewClient(ctx)
  185. if err != nil {
  186. // TODO: handle error.
  187. }
  188. b := client.Bucket("my-bucket")
  189. attrs, err := b.Attrs(ctx)
  190. if err != nil {
  191. // TODO: handle error.
  192. }
  193. // Note that locking the bucket without first attaching a RetentionPolicy
  194. // that's at least 1 day is a no-op
  195. err = b.If(storage.BucketConditions{MetagenerationMatch: attrs.MetaGeneration}).LockRetentionPolicy(ctx)
  196. if err != nil {
  197. // TODO: handle err
  198. }
  199. }
  200. func ExampleBucketHandle_Notifications() {
  201. ctx := context.Background()
  202. client, err := storage.NewClient(ctx)
  203. if err != nil {
  204. // TODO: handle error.
  205. }
  206. b := client.Bucket("my-bucket")
  207. ns, err := b.Notifications(ctx)
  208. if err != nil {
  209. // TODO: handle error.
  210. }
  211. for id, n := range ns {
  212. fmt.Printf("%s: %+v\n", id, n)
  213. }
  214. }
  215. var notificationID string
  216. func ExampleBucketHandle_DeleteNotification() {
  217. ctx := context.Background()
  218. client, err := storage.NewClient(ctx)
  219. if err != nil {
  220. // TODO: handle error.
  221. }
  222. b := client.Bucket("my-bucket")
  223. // TODO: Obtain notificationID from BucketHandle.AddNotification
  224. // or BucketHandle.Notifications.
  225. err = b.DeleteNotification(ctx, notificationID)
  226. if err != nil {
  227. // TODO: handle error.
  228. }
  229. }
  230. func ExampleObjectIterator_Next() {
  231. ctx := context.Background()
  232. client, err := storage.NewClient(ctx)
  233. if err != nil {
  234. // TODO: handle error.
  235. }
  236. it := client.Bucket("my-bucket").Objects(ctx, nil)
  237. for {
  238. objAttrs, err := it.Next()
  239. if err == iterator.Done {
  240. break
  241. }
  242. if err != nil {
  243. // TODO: Handle error.
  244. }
  245. fmt.Println(objAttrs)
  246. }
  247. }
  248. func ExampleSignedURL() {
  249. pkey, err := ioutil.ReadFile("my-private-key.pem")
  250. if err != nil {
  251. // TODO: handle error.
  252. }
  253. url, err := storage.SignedURL("my-bucket", "my-object", &storage.SignedURLOptions{
  254. GoogleAccessID: "xxx@developer.gserviceaccount.com",
  255. PrivateKey: pkey,
  256. Method: "GET",
  257. Expires: time.Now().Add(48 * time.Hour),
  258. })
  259. if err != nil {
  260. // TODO: handle error.
  261. }
  262. fmt.Println(url)
  263. }
  264. func ExampleObjectHandle_Attrs() {
  265. ctx := context.Background()
  266. client, err := storage.NewClient(ctx)
  267. if err != nil {
  268. // TODO: handle error.
  269. }
  270. objAttrs, err := client.Bucket("my-bucket").Object("my-object").Attrs(ctx)
  271. if err != nil {
  272. // TODO: handle error.
  273. }
  274. fmt.Println(objAttrs)
  275. }
  276. func ExampleObjectHandle_Attrs_withConditions() {
  277. ctx := context.Background()
  278. client, err := storage.NewClient(ctx)
  279. if err != nil {
  280. // TODO: handle error.
  281. }
  282. obj := client.Bucket("my-bucket").Object("my-object")
  283. // Read the object.
  284. objAttrs1, err := obj.Attrs(ctx)
  285. if err != nil {
  286. // TODO: handle error.
  287. }
  288. // Do something else for a while.
  289. time.Sleep(5 * time.Minute)
  290. // Now read the same contents, even if the object has been written since the last read.
  291. objAttrs2, err := obj.Generation(objAttrs1.Generation).Attrs(ctx)
  292. if err != nil {
  293. // TODO: handle error.
  294. }
  295. fmt.Println(objAttrs1, objAttrs2)
  296. }
  297. func ExampleObjectHandle_Update() {
  298. ctx := context.Background()
  299. client, err := storage.NewClient(ctx)
  300. if err != nil {
  301. // TODO: handle error.
  302. }
  303. // Change only the content type of the object.
  304. objAttrs, err := client.Bucket("my-bucket").Object("my-object").Update(ctx, storage.ObjectAttrsToUpdate{
  305. ContentType: "text/html",
  306. ContentDisposition: "", // delete ContentDisposition
  307. })
  308. if err != nil {
  309. // TODO: handle error.
  310. }
  311. fmt.Println(objAttrs)
  312. }
  313. func ExampleObjectHandle_NewReader() {
  314. ctx := context.Background()
  315. client, err := storage.NewClient(ctx)
  316. if err != nil {
  317. // TODO: handle error.
  318. }
  319. rc, err := client.Bucket("my-bucket").Object("my-object").NewReader(ctx)
  320. if err != nil {
  321. // TODO: handle error.
  322. }
  323. slurp, err := ioutil.ReadAll(rc)
  324. rc.Close()
  325. if err != nil {
  326. // TODO: handle error.
  327. }
  328. fmt.Println("file contents:", slurp)
  329. }
  330. func ExampleObjectHandle_NewRangeReader() {
  331. ctx := context.Background()
  332. client, err := storage.NewClient(ctx)
  333. if err != nil {
  334. // TODO: handle error.
  335. }
  336. // Read only the first 64K.
  337. rc, err := client.Bucket("bucketname").Object("filename1").NewRangeReader(ctx, 0, 64*1024)
  338. if err != nil {
  339. // TODO: handle error.
  340. }
  341. slurp, err := ioutil.ReadAll(rc)
  342. rc.Close()
  343. if err != nil {
  344. // TODO: handle error.
  345. }
  346. fmt.Println("first 64K of file contents:", slurp)
  347. }
  348. func ExampleObjectHandle_NewWriter() {
  349. ctx := context.Background()
  350. client, err := storage.NewClient(ctx)
  351. if err != nil {
  352. // TODO: handle error.
  353. }
  354. wc := client.Bucket("bucketname").Object("filename1").NewWriter(ctx)
  355. _ = wc // TODO: Use the Writer.
  356. }
  357. func ExampleWriter_Write() {
  358. ctx := context.Background()
  359. client, err := storage.NewClient(ctx)
  360. if err != nil {
  361. // TODO: handle error.
  362. }
  363. wc := client.Bucket("bucketname").Object("filename1").NewWriter(ctx)
  364. wc.ContentType = "text/plain"
  365. wc.ACL = []storage.ACLRule{{storage.AllUsers, storage.RoleReader}}
  366. if _, err := wc.Write([]byte("hello world")); err != nil {
  367. // TODO: handle error.
  368. // Note that Write may return nil in some error situations,
  369. // so always check the error from Close.
  370. }
  371. if err := wc.Close(); err != nil {
  372. // TODO: handle error.
  373. }
  374. fmt.Println("updated object:", wc.Attrs())
  375. }
  376. // To make sure the data you write is uncorrupted, use an MD5 or CRC32c
  377. // checksum. This example illustrates CRC32c.
  378. func ExampleWriter_Write_checksum() {
  379. ctx := context.Background()
  380. client, err := storage.NewClient(ctx)
  381. if err != nil {
  382. // TODO: handle error.
  383. }
  384. data := []byte("verify me")
  385. wc := client.Bucket("bucketname").Object("filename1").NewWriter(ctx)
  386. wc.CRC32C = crc32.Checksum(data, crc32.MakeTable(crc32.Castagnoli))
  387. wc.SendCRC32C = true
  388. if _, err := wc.Write([]byte("hello world")); err != nil {
  389. // TODO: handle error.
  390. // Note that Write may return nil in some error situations,
  391. // so always check the error from Close.
  392. }
  393. if err := wc.Close(); err != nil {
  394. // TODO: handle error.
  395. }
  396. fmt.Println("updated object:", wc.Attrs())
  397. }
  398. func ExampleObjectHandle_Delete() {
  399. ctx := context.Background()
  400. client, err := storage.NewClient(ctx)
  401. if err != nil {
  402. // TODO: handle error.
  403. }
  404. // To delete multiple objects in a bucket, list them with an
  405. // ObjectIterator, then Delete them.
  406. // If you are using this package on the App Engine Flex runtime,
  407. // you can init a bucket client with your app's default bucket name.
  408. // See http://godoc.org/google.golang.org/appengine/file#DefaultBucketName.
  409. bucket := client.Bucket("my-bucket")
  410. it := bucket.Objects(ctx, nil)
  411. for {
  412. objAttrs, err := it.Next()
  413. if err != nil && err != iterator.Done {
  414. // TODO: Handle error.
  415. }
  416. if err == iterator.Done {
  417. break
  418. }
  419. if err := bucket.Object(objAttrs.Name).Delete(ctx); err != nil {
  420. // TODO: Handle error.
  421. }
  422. }
  423. fmt.Println("deleted all object items in the bucket specified.")
  424. }
  425. func ExampleACLHandle_Delete() {
  426. ctx := context.Background()
  427. client, err := storage.NewClient(ctx)
  428. if err != nil {
  429. // TODO: handle error.
  430. }
  431. // No longer grant access to the bucket to everyone on the Internet.
  432. if err := client.Bucket("my-bucket").ACL().Delete(ctx, storage.AllUsers); err != nil {
  433. // TODO: handle error.
  434. }
  435. }
  436. func ExampleACLHandle_Set() {
  437. ctx := context.Background()
  438. client, err := storage.NewClient(ctx)
  439. if err != nil {
  440. // TODO: handle error.
  441. }
  442. // Let any authenticated user read my-bucket/my-object.
  443. obj := client.Bucket("my-bucket").Object("my-object")
  444. if err := obj.ACL().Set(ctx, storage.AllAuthenticatedUsers, storage.RoleReader); err != nil {
  445. // TODO: handle error.
  446. }
  447. }
  448. func ExampleACLHandle_List() {
  449. ctx := context.Background()
  450. client, err := storage.NewClient(ctx)
  451. if err != nil {
  452. // TODO: handle error.
  453. }
  454. // List the default object ACLs for my-bucket.
  455. aclRules, err := client.Bucket("my-bucket").DefaultObjectACL().List(ctx)
  456. if err != nil {
  457. // TODO: handle error.
  458. }
  459. fmt.Println(aclRules)
  460. }
  461. func ExampleCopier_Run() {
  462. ctx := context.Background()
  463. client, err := storage.NewClient(ctx)
  464. if err != nil {
  465. // TODO: handle error.
  466. }
  467. src := client.Bucket("bucketname").Object("file1")
  468. dst := client.Bucket("another-bucketname").Object("file2")
  469. // Copy content and modify metadata.
  470. copier := dst.CopierFrom(src)
  471. copier.ContentType = "text/plain"
  472. attrs, err := copier.Run(ctx)
  473. if err != nil {
  474. // TODO: Handle error, possibly resuming with copier.RewriteToken.
  475. }
  476. fmt.Println(attrs)
  477. // Just copy content.
  478. attrs, err = dst.CopierFrom(src).Run(ctx)
  479. if err != nil {
  480. // TODO: Handle error. No way to resume.
  481. }
  482. fmt.Println(attrs)
  483. }
  484. func ExampleCopier_Run_progress() {
  485. // Display progress across multiple rewrite RPCs.
  486. ctx := context.Background()
  487. client, err := storage.NewClient(ctx)
  488. if err != nil {
  489. // TODO: handle error.
  490. }
  491. src := client.Bucket("bucketname").Object("file1")
  492. dst := client.Bucket("another-bucketname").Object("file2")
  493. copier := dst.CopierFrom(src)
  494. copier.ProgressFunc = func(copiedBytes, totalBytes uint64) {
  495. log.Printf("copy %.1f%% done", float64(copiedBytes)/float64(totalBytes)*100)
  496. }
  497. if _, err := copier.Run(ctx); err != nil {
  498. // TODO: handle error.
  499. }
  500. }
  501. var key1, key2 []byte
  502. func ExampleObjectHandle_CopierFrom_rotateEncryptionKeys() {
  503. // To rotate the encryption key on an object, copy it onto itself.
  504. ctx := context.Background()
  505. client, err := storage.NewClient(ctx)
  506. if err != nil {
  507. // TODO: handle error.
  508. }
  509. obj := client.Bucket("bucketname").Object("obj")
  510. // Assume obj is encrypted with key1, and we want to change to key2.
  511. _, err = obj.Key(key2).CopierFrom(obj.Key(key1)).Run(ctx)
  512. if err != nil {
  513. // TODO: handle error.
  514. }
  515. }
  516. func ExampleComposer_Run() {
  517. ctx := context.Background()
  518. client, err := storage.NewClient(ctx)
  519. if err != nil {
  520. // TODO: handle error.
  521. }
  522. bkt := client.Bucket("bucketname")
  523. src1 := bkt.Object("o1")
  524. src2 := bkt.Object("o2")
  525. dst := bkt.Object("o3")
  526. // Compose and modify metadata.
  527. c := dst.ComposerFrom(src1, src2)
  528. c.ContentType = "text/plain"
  529. attrs, err := c.Run(ctx)
  530. if err != nil {
  531. // TODO: Handle error.
  532. }
  533. fmt.Println(attrs)
  534. // Just compose.
  535. attrs, err = dst.ComposerFrom(src1, src2).Run(ctx)
  536. if err != nil {
  537. // TODO: Handle error.
  538. }
  539. fmt.Println(attrs)
  540. }
  541. var gen int64
  542. func ExampleObjectHandle_Generation() {
  543. // Read an object's contents from generation gen, regardless of the
  544. // current generation of the object.
  545. ctx := context.Background()
  546. client, err := storage.NewClient(ctx)
  547. if err != nil {
  548. // TODO: handle error.
  549. }
  550. obj := client.Bucket("my-bucket").Object("my-object")
  551. rc, err := obj.Generation(gen).NewReader(ctx)
  552. if err != nil {
  553. // TODO: handle error.
  554. }
  555. defer rc.Close()
  556. if _, err := io.Copy(os.Stdout, rc); err != nil {
  557. // TODO: handle error.
  558. }
  559. }
  560. func ExampleObjectHandle_If() {
  561. // Read from an object only if the current generation is gen.
  562. ctx := context.Background()
  563. client, err := storage.NewClient(ctx)
  564. if err != nil {
  565. // TODO: handle error.
  566. }
  567. obj := client.Bucket("my-bucket").Object("my-object")
  568. rc, err := obj.If(storage.Conditions{GenerationMatch: gen}).NewReader(ctx)
  569. if err != nil {
  570. // TODO: handle error.
  571. }
  572. defer rc.Close()
  573. if _, err := io.Copy(os.Stdout, rc); err != nil {
  574. // TODO: handle error.
  575. }
  576. }
  577. var secretKey []byte
  578. func ExampleObjectHandle_Key() {
  579. ctx := context.Background()
  580. client, err := storage.NewClient(ctx)
  581. if err != nil {
  582. // TODO: handle error.
  583. }
  584. obj := client.Bucket("my-bucket").Object("my-object")
  585. // Encrypt the object's contents.
  586. w := obj.Key(secretKey).NewWriter(ctx)
  587. if _, err := w.Write([]byte("top secret")); err != nil {
  588. // TODO: handle error.
  589. }
  590. if err := w.Close(); err != nil {
  591. // TODO: handle error.
  592. }
  593. }