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.
 
 
 

177 lines
6.0 KiB

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