example_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2014 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. package storage_test
  15. import (
  16. "io/ioutil"
  17. "log"
  18. "golang.org/x/net/context"
  19. "golang.org/x/oauth2/google"
  20. "google.golang.org/cloud"
  21. "google.golang.org/cloud/storage"
  22. )
  23. func Example_auth() {
  24. // Initialize an authorized context with Google Developers Console
  25. // JSON key. Read the google package examples to learn more about
  26. // different authorization flows you can use.
  27. // http://godoc.org/golang.org/x/oauth2/google
  28. jsonKey, err := ioutil.ReadFile("/path/to/json/keyfile.json")
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. conf, err := google.JWTConfigFromJSON(
  33. jsonKey,
  34. storage.ScopeFullControl,
  35. )
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. ctx := context.Background()
  40. client, err := storage.NewClient(ctx, cloud.WithTokenSource(conf.TokenSource(ctx)))
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. // Use the client (see other examples)
  45. doSomething(client)
  46. // After using the client, free any resources (e.g. network connections).
  47. client.Close()
  48. }
  49. func ExampleListObjects() {
  50. ctx := context.Background()
  51. var client *storage.Client // See Example (Auth)
  52. var query *storage.Query
  53. for {
  54. // If you are using this package on App Engine Managed VMs runtime,
  55. // you can init a bucket client with your app's default bucket name.
  56. // See http://godoc.org/google.golang.org/appengine/file#DefaultBucketName.
  57. objects, err := client.Bucket("bucketname").List(ctx, query)
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61. for _, obj := range objects.Results {
  62. log.Printf("object name: %s, size: %v", obj.Name, obj.Size)
  63. }
  64. // If there are more results, objects.Next will be non-nil.
  65. if objects.Next == nil {
  66. break
  67. }
  68. query = objects.Next
  69. }
  70. log.Println("paginated through all object items in the bucket you specified.")
  71. }
  72. func ExampleNewReader() {
  73. ctx := context.Background()
  74. var client *storage.Client // See Example (Auth)
  75. rc, err := client.Bucket("bucketname").Object("filename1").NewReader(ctx)
  76. if err != nil {
  77. log.Fatal(err)
  78. }
  79. slurp, err := ioutil.ReadAll(rc)
  80. rc.Close()
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. log.Println("file contents:", slurp)
  85. }
  86. func ExampleNewWriter() {
  87. ctx := context.Background()
  88. var client *storage.Client // See Example (Auth)
  89. wc := client.Bucket("bucketname").Object("filename1").NewWriter(ctx)
  90. wc.ContentType = "text/plain"
  91. wc.ACL = []storage.ACLRule{{storage.AllUsers, storage.RoleReader}}
  92. if _, err := wc.Write([]byte("hello world")); err != nil {
  93. log.Fatal(err)
  94. }
  95. if err := wc.Close(); err != nil {
  96. log.Fatal(err)
  97. }
  98. log.Println("updated object:", wc.Attrs())
  99. }
  100. func ExampleCopyObject() {
  101. ctx := context.Background()
  102. var client *storage.Client // See Example (Auth)
  103. o, err := client.CopyObject(ctx, "bucketname", "file1", "another-bucketname", "file2", nil)
  104. if err != nil {
  105. log.Fatal(err)
  106. }
  107. log.Println("copied file:", o)
  108. }
  109. func ExampleDeleteObject() {
  110. ctx := context.Background()
  111. var client *storage.Client // See Example (Auth)
  112. // To delete multiple objects in a bucket, first List then Delete them.
  113. // If you are using this package on App Engine Managed VMs runtime,
  114. // you can init a bucket client with your app's default bucket name.
  115. // See http://godoc.org/google.golang.org/appengine/file#DefaultBucketName.
  116. bucket := client.Bucket("bucketname")
  117. var query *storage.Query // Set up query as desired.
  118. for {
  119. objects, err := bucket.List(ctx, query)
  120. if err != nil {
  121. log.Fatal(err)
  122. }
  123. for _, obj := range objects.Results {
  124. log.Printf("deleting object name: %q, size: %v", obj.Name, obj.Size)
  125. if err := bucket.Object(obj.Name).Delete(ctx); err != nil {
  126. log.Fatalf("unable to delete %q: %v", obj.Name, err)
  127. }
  128. }
  129. // If there are more results, objects.Next will be non-nil.
  130. if objects.Next == nil {
  131. break
  132. }
  133. query = objects.Next
  134. }
  135. log.Println("deleted all object items in the bucket specified.")
  136. }
  137. func doSomething(c *storage.Client) {}