您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

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