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.
 
 
 

666 lines
17 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. "context"
  17. "fmt"
  18. "hash/crc32"
  19. "io"
  20. "io/ioutil"
  21. "log"
  22. "os"
  23. "time"
  24. "cloud.google.com/go/storage"
  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.Buckets(ctx, "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{{Entity: storage.AllUsers, Role: 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 limit the time to write an object (or do anything else
  377. // that takes a context), use context.WithTimeout.
  378. func ExampleWriter_Write_timeout() {
  379. ctx := context.Background()
  380. client, err := storage.NewClient(ctx)
  381. if err != nil {
  382. // TODO: handle error.
  383. }
  384. tctx, cancel := context.WithTimeout(ctx, 30*time.Second)
  385. defer cancel() // Cancel when done, whether we time out or not.
  386. wc := client.Bucket("bucketname").Object("filename1").NewWriter(tctx)
  387. wc.ContentType = "text/plain"
  388. wc.ACL = []storage.ACLRule{{Entity: storage.AllUsers, Role: storage.RoleReader}}
  389. if _, err := wc.Write([]byte("hello world")); err != nil {
  390. // TODO: handle error.
  391. // Note that Write may return nil in some error situations,
  392. // so always check the error from Close.
  393. }
  394. if err := wc.Close(); err != nil {
  395. // TODO: handle error.
  396. }
  397. fmt.Println("updated object:", wc.Attrs())
  398. }
  399. // To make sure the data you write is uncorrupted, use an MD5 or CRC32c
  400. // checksum. This example illustrates CRC32c.
  401. func ExampleWriter_Write_checksum() {
  402. ctx := context.Background()
  403. client, err := storage.NewClient(ctx)
  404. if err != nil {
  405. // TODO: handle error.
  406. }
  407. data := []byte("verify me")
  408. wc := client.Bucket("bucketname").Object("filename1").NewWriter(ctx)
  409. wc.CRC32C = crc32.Checksum(data, crc32.MakeTable(crc32.Castagnoli))
  410. wc.SendCRC32C = true
  411. if _, err := wc.Write([]byte("hello world")); err != nil {
  412. // TODO: handle error.
  413. // Note that Write may return nil in some error situations,
  414. // so always check the error from Close.
  415. }
  416. if err := wc.Close(); err != nil {
  417. // TODO: handle error.
  418. }
  419. fmt.Println("updated object:", wc.Attrs())
  420. }
  421. func ExampleObjectHandle_Delete() {
  422. ctx := context.Background()
  423. client, err := storage.NewClient(ctx)
  424. if err != nil {
  425. // TODO: handle error.
  426. }
  427. // To delete multiple objects in a bucket, list them with an
  428. // ObjectIterator, then Delete them.
  429. // If you are using this package on the App Engine Flex runtime,
  430. // you can init a bucket client with your app's default bucket name.
  431. // See http://godoc.org/google.golang.org/appengine/file#DefaultBucketName.
  432. bucket := client.Bucket("my-bucket")
  433. it := bucket.Objects(ctx, nil)
  434. for {
  435. objAttrs, err := it.Next()
  436. if err != nil && err != iterator.Done {
  437. // TODO: Handle error.
  438. }
  439. if err == iterator.Done {
  440. break
  441. }
  442. if err := bucket.Object(objAttrs.Name).Delete(ctx); err != nil {
  443. // TODO: Handle error.
  444. }
  445. }
  446. fmt.Println("deleted all object items in the bucket specified.")
  447. }
  448. func ExampleACLHandle_Delete() {
  449. ctx := context.Background()
  450. client, err := storage.NewClient(ctx)
  451. if err != nil {
  452. // TODO: handle error.
  453. }
  454. // No longer grant access to the bucket to everyone on the Internet.
  455. if err := client.Bucket("my-bucket").ACL().Delete(ctx, storage.AllUsers); err != nil {
  456. // TODO: handle error.
  457. }
  458. }
  459. func ExampleACLHandle_Set() {
  460. ctx := context.Background()
  461. client, err := storage.NewClient(ctx)
  462. if err != nil {
  463. // TODO: handle error.
  464. }
  465. // Let any authenticated user read my-bucket/my-object.
  466. obj := client.Bucket("my-bucket").Object("my-object")
  467. if err := obj.ACL().Set(ctx, storage.AllAuthenticatedUsers, storage.RoleReader); err != nil {
  468. // TODO: handle error.
  469. }
  470. }
  471. func ExampleACLHandle_List() {
  472. ctx := context.Background()
  473. client, err := storage.NewClient(ctx)
  474. if err != nil {
  475. // TODO: handle error.
  476. }
  477. // List the default object ACLs for my-bucket.
  478. aclRules, err := client.Bucket("my-bucket").DefaultObjectACL().List(ctx)
  479. if err != nil {
  480. // TODO: handle error.
  481. }
  482. fmt.Println(aclRules)
  483. }
  484. func ExampleCopier_Run() {
  485. ctx := context.Background()
  486. client, err := storage.NewClient(ctx)
  487. if err != nil {
  488. // TODO: handle error.
  489. }
  490. src := client.Bucket("bucketname").Object("file1")
  491. dst := client.Bucket("another-bucketname").Object("file2")
  492. // Copy content and modify metadata.
  493. copier := dst.CopierFrom(src)
  494. copier.ContentType = "text/plain"
  495. attrs, err := copier.Run(ctx)
  496. if err != nil {
  497. // TODO: Handle error, possibly resuming with copier.RewriteToken.
  498. }
  499. fmt.Println(attrs)
  500. // Just copy content.
  501. attrs, err = dst.CopierFrom(src).Run(ctx)
  502. if err != nil {
  503. // TODO: Handle error. No way to resume.
  504. }
  505. fmt.Println(attrs)
  506. }
  507. func ExampleCopier_Run_progress() {
  508. // Display progress across multiple rewrite RPCs.
  509. ctx := context.Background()
  510. client, err := storage.NewClient(ctx)
  511. if err != nil {
  512. // TODO: handle error.
  513. }
  514. src := client.Bucket("bucketname").Object("file1")
  515. dst := client.Bucket("another-bucketname").Object("file2")
  516. copier := dst.CopierFrom(src)
  517. copier.ProgressFunc = func(copiedBytes, totalBytes uint64) {
  518. log.Printf("copy %.1f%% done", float64(copiedBytes)/float64(totalBytes)*100)
  519. }
  520. if _, err := copier.Run(ctx); err != nil {
  521. // TODO: handle error.
  522. }
  523. }
  524. var key1, key2 []byte
  525. func ExampleObjectHandle_CopierFrom_rotateEncryptionKeys() {
  526. // To rotate the encryption key on an object, copy it onto itself.
  527. ctx := context.Background()
  528. client, err := storage.NewClient(ctx)
  529. if err != nil {
  530. // TODO: handle error.
  531. }
  532. obj := client.Bucket("bucketname").Object("obj")
  533. // Assume obj is encrypted with key1, and we want to change to key2.
  534. _, err = obj.Key(key2).CopierFrom(obj.Key(key1)).Run(ctx)
  535. if err != nil {
  536. // TODO: handle error.
  537. }
  538. }
  539. func ExampleComposer_Run() {
  540. ctx := context.Background()
  541. client, err := storage.NewClient(ctx)
  542. if err != nil {
  543. // TODO: handle error.
  544. }
  545. bkt := client.Bucket("bucketname")
  546. src1 := bkt.Object("o1")
  547. src2 := bkt.Object("o2")
  548. dst := bkt.Object("o3")
  549. // Compose and modify metadata.
  550. c := dst.ComposerFrom(src1, src2)
  551. c.ContentType = "text/plain"
  552. attrs, err := c.Run(ctx)
  553. if err != nil {
  554. // TODO: Handle error.
  555. }
  556. fmt.Println(attrs)
  557. // Just compose.
  558. attrs, err = dst.ComposerFrom(src1, src2).Run(ctx)
  559. if err != nil {
  560. // TODO: Handle error.
  561. }
  562. fmt.Println(attrs)
  563. }
  564. var gen int64
  565. func ExampleObjectHandle_Generation() {
  566. // Read an object's contents from generation gen, regardless of the
  567. // current generation of the object.
  568. ctx := context.Background()
  569. client, err := storage.NewClient(ctx)
  570. if err != nil {
  571. // TODO: handle error.
  572. }
  573. obj := client.Bucket("my-bucket").Object("my-object")
  574. rc, err := obj.Generation(gen).NewReader(ctx)
  575. if err != nil {
  576. // TODO: handle error.
  577. }
  578. defer rc.Close()
  579. if _, err := io.Copy(os.Stdout, rc); err != nil {
  580. // TODO: handle error.
  581. }
  582. }
  583. func ExampleObjectHandle_If() {
  584. // Read from an object only if the current generation is gen.
  585. ctx := context.Background()
  586. client, err := storage.NewClient(ctx)
  587. if err != nil {
  588. // TODO: handle error.
  589. }
  590. obj := client.Bucket("my-bucket").Object("my-object")
  591. rc, err := obj.If(storage.Conditions{GenerationMatch: gen}).NewReader(ctx)
  592. if err != nil {
  593. // TODO: handle error.
  594. }
  595. defer rc.Close()
  596. if _, err := io.Copy(os.Stdout, rc); err != nil {
  597. // TODO: handle error.
  598. }
  599. }
  600. var secretKey []byte
  601. func ExampleObjectHandle_Key() {
  602. ctx := context.Background()
  603. client, err := storage.NewClient(ctx)
  604. if err != nil {
  605. // TODO: handle error.
  606. }
  607. obj := client.Bucket("my-bucket").Object("my-object")
  608. // Encrypt the object's contents.
  609. w := obj.Key(secretKey).NewWriter(ctx)
  610. if _, err := w.Write([]byte("top secret")); err != nil {
  611. // TODO: handle error.
  612. }
  613. if err := w.Close(); err != nil {
  614. // TODO: handle error.
  615. }
  616. }