doc.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  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. /*
  15. Package storage provides an easy way to work with Google Cloud Storage.
  16. Google Cloud Storage stores data in named objects, which are grouped into buckets.
  17. More information about Google Cloud Storage is available at
  18. https://cloud.google.com/storage/docs.
  19. All of the methods of this package use exponential backoff to retry calls
  20. that fail with certain errors, as described in
  21. https://cloud.google.com/storage/docs/exponential-backoff.
  22. Note: This package is in beta. Some backwards-incompatible changes may occur.
  23. Creating a Client
  24. To start working with this package, create a client:
  25. ctx := context.Background()
  26. client, err := storage.NewClient(ctx)
  27. if err != nil {
  28. // TODO: Handle error.
  29. }
  30. Buckets
  31. A Google Cloud Storage bucket is a collection of objects. To work with a
  32. bucket, make a bucket handle:
  33. bkt := client.Bucket(bucketName)
  34. A handle is a reference to a bucket. You can have a handle even if the
  35. bucket doesn't exist yet. To create a bucket in Google Cloud Storage,
  36. call Create on the handle:
  37. if err := bkt.Create(ctx, projectID, nil); err != nil {
  38. // TODO: Handle error.
  39. }
  40. Note that although buckets are associated with projects, bucket names are
  41. global across all projects.
  42. Each bucket has associated metadata, represented in this package by
  43. BucketAttrs. The third argument to BucketHandle.Create allows you to set
  44. the intial BucketAttrs of a bucket. To retrieve a bucket's attributes, use
  45. Attrs:
  46. attrs, err := bkt.Attrs(ctx)
  47. if err != nil {
  48. // TODO: Handle error.
  49. }
  50. fmt.Printf("bucket %s, created at %s, is located in %s with storage class %s\n",
  51. attrs.Name, attrs.Created, attrs.Location, attrs.StorageClass)
  52. Objects
  53. An object holds arbitrary data as a sequence of bytes, like a file. You
  54. refer to objects using a handle, just as with buckets. You can use the
  55. standard Go io.Reader and io.Writer interfaces to read and write
  56. object data:
  57. obj := bkt.Object("data")
  58. // Write something to obj.
  59. // w implements io.Writer.
  60. w := obj.NewWriter(ctx)
  61. // Write some text to obj. This will overwrite whatever is there.
  62. if _, err := fmt.Fprintf(w, "This object contains text.\n"); err != nil {
  63. // TODO: Handle error.
  64. }
  65. // Close, just like writing a file.
  66. if err := w.Close(); err != nil {
  67. // TODO: Handle error.
  68. }
  69. // Read it back.
  70. r, err := obj.NewReader(ctx)
  71. if err != nil {
  72. // TODO: Handle error.
  73. }
  74. defer r.Close()
  75. if _, err := io.Copy(os.Stdout, r); err != nil {
  76. // TODO: Handle error.
  77. }
  78. // Prints "This object contains text."
  79. Objects also have attributes, which you can fetch with Attrs:
  80. objAttrs, err := obj.Attrs(ctx)
  81. if err != nil {
  82. // TODO: Handle error.
  83. }
  84. fmt.Printf("object %s has size %d and can be read using %s\n",
  85. objAttrs.Name, objAttrs.Size, objAttrs.MediaLink)
  86. ACLs
  87. Both objects and buckets have ACLs (Access Control Lists). An ACL is a list of
  88. ACLRules, each of which specifies the role of a user, group or project. ACLs
  89. are suitable for fine-grained control, but you may prefer using IAM to control
  90. access at the project level (see
  91. https://cloud.google.com/storage/docs/access-control/iam).
  92. To list the ACLs of a bucket or object, obtain an ACLHandle and call its List method:
  93. acls, err := obj.ACL().List(ctx)
  94. if err != nil {
  95. // TODO: Handle error.
  96. }
  97. for _, rule := range acls {
  98. fmt.Printf("%s has role %s\n", rule.Entity, rule.Role)
  99. }
  100. You can also set and delete ACLs.
  101. Conditions
  102. Every object has a generation and a metageneration. The generation changes
  103. whenever the content changes, and the metageneration changes whenever the
  104. metadata changes. Conditions let you check these values before an operation;
  105. the operation only executes if the conditions match. You can use conditions to
  106. prevent race conditions in read-modify-write operations.
  107. For example, say you've read an object's metadata into objAttrs. Now
  108. you want to write to that object, but only if its contents haven't changed
  109. since you read it. Here is how to express that:
  110. w = obj.If(storage.Conditions{GenerationMatch: objAttrs.Generation}).NewWriter(ctx)
  111. // Proceed with writing as above.
  112. Signed URLs
  113. You can obtain a URL that lets anyone read or write an object for a limited time.
  114. You don't need to create a client to do this. See the documentation of
  115. SignedURL for details.
  116. url, err := storage.SignedURL(bucketName, "shared-object", opts)
  117. if err != nil {
  118. // TODO: Handle error.
  119. }
  120. fmt.Println(url)
  121. Authentication
  122. See examples of authorization and authentication at
  123. https://godoc.org/cloud.google.com/go#pkg-examples.
  124. */
  125. package storage // import "cloud.google.com/go/storage"