google-cloud-storage 1.14.0 → 1.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,27 @@
1
+ # Enabling Logging
2
+
3
+ To enable logging for this library, set the logger for the underlying [Google
4
+ API
5
+ Client](https://github.com/google/google-api-ruby-client/blob/master/README.md#logging)
6
+ library. The logger that you set may be a Ruby stdlib
7
+ [`Logger`](https://ruby-doc.org/stdlib-2.4.0/libdoc/logger/rdoc/Logger.html) as
8
+ shown below, or a
9
+ [`Google::Cloud::Logging::Logger`](https://googlecloudplatform.github.io/google-cloud-ruby/docs/google-cloud-logging/latest/Google/Cloud/Logging/Logger)
10
+ that will write logs to [Stackdriver
11
+ Logging](https://cloud.google.com/logging/).
12
+
13
+ If you do not set the logger explicitly and your application is running in a
14
+ Rails environment, it will default to `Rails.logger`. Otherwise, if you do not
15
+ set the logger and you are not using Rails, logging is disabled by default.
16
+
17
+ Configuring a Ruby stdlib logger:
18
+
19
+ ```ruby
20
+ require "logger"
21
+
22
+ my_logger = Logger.new $stderr
23
+ my_logger.level = Logger::WARN
24
+
25
+ # Set the Google API Client logger
26
+ Google::Apis.logger = my_logger
27
+ ```
@@ -0,0 +1,573 @@
1
+ # Google Cloud Storage
2
+
3
+ Google Cloud Storage is an Internet service to store data in Google's cloud. It
4
+ allows world-wide storage and retrieval of any amount of data and at any time,
5
+ taking advantage of Google's own reliable and fast networking infrastructure to
6
+ perform data operations in a cost effective manner.
7
+
8
+ The goal of google-cloud is to provide an API that is comfortable to Rubyists.
9
+ Your authentication credentials are detected automatically in Google Cloud
10
+ Platform environments such as Google Compute Engine, Google App Engine and
11
+ Google Kubernetes Engine. In other environments you can configure authentication
12
+ easily, either directly in your code or via environment variables. Read more
13
+ about the options for connecting in the {file:AUTHENTICATION.md Authentication
14
+ Guide}.
15
+
16
+ ```ruby
17
+ require "google/cloud/storage"
18
+
19
+ storage = Google::Cloud::Storage.new(
20
+ project_id: "my-project",
21
+ credentials: "/path/to/keyfile.json"
22
+ )
23
+
24
+ bucket = storage.bucket "my-bucket"
25
+ file = bucket.file "path/to/my-file.ext"
26
+ ```
27
+
28
+ To learn more about Cloud Storage, read the [Google Cloud Storage Overview
29
+ ](https://cloud.google.com/storage/docs/overview).
30
+
31
+ ## Retrieving Buckets
32
+
33
+ A {Google::Cloud::Storage::Bucket Bucket} instance is a container for your data.
34
+ There is no limit on the number of buckets that you can create in a project. You
35
+ can use buckets to organize and control access to your data. For more
36
+ information, see [Working with
37
+ Buckets](https://cloud.google.com/storage/docs/creating-buckets).
38
+
39
+ Each bucket has a globally unique name, which is how they are retrieved: (See
40
+ {Google::Cloud::Storage::Project#bucket Project#bucket})
41
+
42
+ ```ruby
43
+ require "google/cloud/storage"
44
+
45
+ storage = Google::Cloud::Storage.new
46
+
47
+ bucket = storage.bucket "my-todo-app"
48
+ ```
49
+
50
+ You can also retrieve all buckets on a project: (See
51
+ {Google::Cloud::Storage::Project#buckets Project#buckets})
52
+
53
+ ```ruby
54
+ require "google/cloud/storage"
55
+
56
+ storage = Google::Cloud::Storage.new
57
+
58
+ all_buckets = storage.buckets
59
+ ```
60
+
61
+ If you have a significant number of buckets, you may need to fetch them in
62
+ multiple service requests.
63
+
64
+ Iterating over each bucket, potentially with multiple API calls, by invoking
65
+ {Google::Cloud::Storage::Bucket::List#all Bucket::List#all} with a block:
66
+
67
+ ```ruby
68
+ require "google/cloud/storage"
69
+
70
+ storage = Google::Cloud::Storage.new
71
+
72
+ buckets = storage.buckets
73
+ buckets.all do |bucket|
74
+ puts bucket.name
75
+ end
76
+ ```
77
+
78
+ Limiting the number of API calls made:
79
+
80
+ ```ruby
81
+ require "google/cloud/storage"
82
+
83
+ storage = Google::Cloud::Storage.new
84
+
85
+ buckets = storage.buckets
86
+ buckets.all(request_limit: 10) do |bucket|
87
+ puts bucket.name
88
+ end
89
+ ```
90
+
91
+ See {Google::Cloud::Storage::Bucket::List Bucket::List} for details.
92
+
93
+ ## Creating a Bucket
94
+
95
+ A unique name is all that is needed to create a new bucket: (See
96
+ {Google::Cloud::Storage::Project#create_bucket Project#create_bucket})
97
+
98
+ ```ruby
99
+ require "google/cloud/storage"
100
+
101
+ storage = Google::Cloud::Storage.new
102
+
103
+ bucket = storage.create_bucket "my-todo-app-attachments"
104
+ ```
105
+
106
+ ## Retrieving Files
107
+
108
+ A {Google::Cloud::Storage::File File} instance is an individual data object that
109
+ you store in Google Cloud Storage. Files contain the data stored as well as
110
+ metadata describing the data. Files belong to a bucket and cannot be shared
111
+ among buckets. There is no limit on the number of files that you can create in a
112
+ bucket. For more information, see [Working with
113
+ Objects](https://cloud.google.com/storage/docs/object-basics).
114
+
115
+ Files are retrieved by their name, which is the path of the file in the bucket:
116
+ (See {Google::Cloud::Storage::Bucket#file Bucket#file})
117
+
118
+ ```ruby
119
+ require "google/cloud/storage"
120
+
121
+ storage = Google::Cloud::Storage.new
122
+
123
+ bucket = storage.bucket "my-todo-app"
124
+ file = bucket.file "avatars/heidi/400x400.png"
125
+ ```
126
+
127
+ You can also retrieve all files in a bucket: (See
128
+ {Google::Cloud::Storage::Bucket#files Bucket#files})
129
+
130
+ ```ruby
131
+ require "google/cloud/storage"
132
+
133
+ storage = Google::Cloud::Storage.new
134
+
135
+ bucket = storage.bucket "my-todo-app"
136
+ all_files = bucket.files
137
+ ```
138
+
139
+ Or you can retrieve all files in a specified path:
140
+
141
+ ```ruby
142
+ require "google/cloud/storage"
143
+
144
+ storage = Google::Cloud::Storage.new
145
+
146
+ bucket = storage.bucket "my-todo-app"
147
+ avatar_files = bucket.files prefix: "avatars/"
148
+ ```
149
+
150
+ If you have a significant number of files, you may need to fetch them in
151
+ multiple service requests.
152
+
153
+ Iterating over each file, potentially with multiple API calls, by invoking
154
+ {Google::Cloud::Storage::File::List#all File::List#all} with a block:
155
+
156
+ ```ruby
157
+ require "google/cloud/storage"
158
+
159
+ storage = Google::Cloud::Storage.new
160
+ bucket = storage.bucket "my-todo-app"
161
+
162
+ files = storage.files
163
+ files.all do |file|
164
+ puts file.name
165
+ end
166
+ ```
167
+
168
+ Limiting the number of API calls made:
169
+
170
+ ```ruby
171
+ require "google/cloud/storage"
172
+
173
+ storage = Google::Cloud::Storage.new
174
+
175
+ files = storage.files
176
+ files.all(request_limit: 10) do |file|
177
+ puts bucket.name
178
+ end
179
+ ```
180
+
181
+ See {Google::Cloud::Storage::File::List File::List} for details.
182
+
183
+ ## Creating a File
184
+
185
+ A new file can be uploaded by specifying the location of a file on the local
186
+ file system, and the name/path that the file should be stored in the bucket.
187
+ (See {Google::Cloud::Storage::Bucket#create_file Bucket#create_file})
188
+
189
+ ```ruby
190
+ require "google/cloud/storage"
191
+
192
+ storage = Google::Cloud::Storage.new
193
+
194
+ bucket = storage.bucket "my-todo-app"
195
+ bucket.create_file "/var/todo-app/avatars/heidi/400x400.png",
196
+ "avatars/heidi/400x400.png"
197
+ ```
198
+
199
+ Files can also be created from an in-memory StringIO object:
200
+
201
+ ```ruby
202
+ require "google/cloud/storage"
203
+
204
+ storage = Google::Cloud::Storage.new
205
+
206
+ bucket = storage.bucket "my-todo-app"
207
+ bucket.create_file StringIO.new("Hello world!"), "hello-world.txt"
208
+ ```
209
+
210
+ ### Customer-supplied encryption keys
211
+
212
+ By default, Google Cloud Storage manages server-side encryption keys on your
213
+ behalf. However, a [customer-supplied encryption
214
+ key](https://cloud.google.com/storage/docs/encryption#customer-supplied) can be
215
+ provided with the `encryption_key` option. If given, the same key must be
216
+ provided to subsequently download or copy the file. If you use customer-supplied
217
+ encryption keys, you must securely manage your keys and ensure that they are not
218
+ lost. Also, please note that file metadata is not encrypted, with the exception
219
+ of the CRC32C checksum and MD5 hash. The names of files and buckets are also not
220
+ encrypted, and you can read or update the metadata of an encrypted file without
221
+ providing the encryption key.
222
+
223
+ ```ruby
224
+ require "google/cloud/storage"
225
+
226
+ storage = Google::Cloud::Storage.new
227
+ bucket = storage.bucket "my-todo-app"
228
+
229
+ # Key generation shown for example purposes only. Write your own.
230
+ cipher = OpenSSL::Cipher.new "aes-256-cfb"
231
+ cipher.encrypt
232
+ key = cipher.random_key
233
+
234
+ bucket.create_file "/var/todo-app/avatars/heidi/400x400.png",
235
+ "avatars/heidi/400x400.png",
236
+ encryption_key: key
237
+
238
+ # Store your key and hash securely for later use.
239
+ file = bucket.file "avatars/heidi/400x400.png",
240
+ encryption_key: key
241
+ ```
242
+
243
+ Use {Google::Cloud::Storage::File#rotate File#rotate} to rotate
244
+ customer-supplied encryption keys.
245
+
246
+ ```ruby
247
+ require "google/cloud/storage"
248
+
249
+ storage = Google::Cloud::Storage.new
250
+ bucket = storage.bucket "my-todo-app"
251
+
252
+ # Old key was stored securely for later use.
253
+ old_key = "y\x03\"\x0E\xB6\xD3\x9B\x0E\xAB*\x19\xFAv\xDEY\xBEI..."
254
+
255
+ file = bucket.file "path/to/my-file.ext", encryption_key: old_key
256
+
257
+ # Key generation shown for example purposes only. Write your own.
258
+ cipher = OpenSSL::Cipher.new "aes-256-cfb"
259
+ cipher.encrypt
260
+ new_key = cipher.random_key
261
+
262
+ file.rotate encryption_key: old_key, new_encryption_key: new_key
263
+ ```
264
+
265
+ ## Downloading a File
266
+
267
+ Files can be downloaded to the local file system. (See
268
+ {Google::Cloud::Storage::File#download File#download})
269
+
270
+ ```ruby
271
+ require "google/cloud/storage"
272
+
273
+ storage = Google::Cloud::Storage.new
274
+
275
+ bucket = storage.bucket "my-todo-app"
276
+ file = bucket.file "avatars/heidi/400x400.png"
277
+ file.download "/var/todo-app/avatars/heidi/400x400.png"
278
+ ```
279
+
280
+ Files can also be downloaded to an in-memory StringIO object:
281
+
282
+ ```ruby
283
+ require "google/cloud/storage"
284
+
285
+ storage = Google::Cloud::Storage.new
286
+
287
+ bucket = storage.bucket "my-todo-app"
288
+ file = bucket.file "hello-world.txt"
289
+
290
+ downloaded = file.download
291
+ downloaded.rewind
292
+ downloaded.read #=> "Hello world!"
293
+ ```
294
+
295
+ Download a public file with an anonymous, unauthenticated client. Use
296
+ `skip_lookup` to avoid errors retrieving non-public bucket and file metadata.
297
+
298
+ ```ruby
299
+ require "google/cloud/storage"
300
+
301
+ storage = Google::Cloud::Storage.anonymous
302
+
303
+ bucket = storage.bucket "public-bucket", skip_lookup: true
304
+ file = bucket.file "path/to/public-file.ext", skip_lookup: true
305
+
306
+ downloaded = file.download
307
+ downloaded.rewind
308
+ downloaded.read #=> "Hello world!"
309
+ ```
310
+
311
+ ## Creating and downloading gzip-encoded files
312
+
313
+ When uploading a gzip-compressed file, you should pass `content_encoding:
314
+ "gzip"` if you want the file to be eligible for [decompressive
315
+ transcoding](https://cloud.google.com/storage/docs/transcoding) when it is later
316
+ downloaded. In addition, giving the gzip-compressed file a name containing the
317
+ original file extension (for example, `.txt`) will ensure that the file's
318
+ `Content-Type` metadata is set correctly. (You can also set the file's
319
+ `Content-Type` metadata explicitly with the `content_type` option.)
320
+
321
+ ```ruby
322
+ require "zlib"
323
+ require "google/cloud/storage"
324
+
325
+ storage = Google::Cloud::Storage.new
326
+
327
+ gz = StringIO.new ""
328
+ z = Zlib::GzipWriter.new gz
329
+ z.write "Hello world!"
330
+ z.close
331
+ data = StringIO.new gz.string
332
+
333
+ bucket = storage.bucket "my-bucket"
334
+
335
+ bucket.create_file data, "path/to/gzipped.txt",
336
+ content_encoding: "gzip"
337
+
338
+ file = bucket.file "path/to/gzipped.txt"
339
+
340
+ # The downloaded data is decompressed by default.
341
+ file.download "path/to/downloaded/hello.txt"
342
+
343
+ # The downloaded data remains compressed with skip_decompress.
344
+ file.download "path/to/downloaded/gzipped.txt",
345
+ skip_decompress: true
346
+ ```
347
+
348
+ ## Using Signed URLs
349
+
350
+ Access without authentication can be granted to a file for a specified period of
351
+ time. This URL uses a cryptographic signature of your credentials to access the
352
+ file. (See {Google::Cloud::Storage::File#signed_url File#signed_url})
353
+
354
+ ```ruby
355
+ require "google/cloud/storage"
356
+
357
+ storage = Google::Cloud::Storage.new
358
+
359
+ bucket = storage.bucket "my-todo-app"
360
+ file = bucket.file "avatars/heidi/400x400.png"
361
+ shared_url = file.signed_url method: "GET",
362
+ expires: 300 # 5 minutes from now
363
+ ```
364
+
365
+ ## Controlling Access to a Bucket
366
+
367
+ Access to a bucket is controlled with {Google::Cloud::Storage::Bucket#acl
368
+ Bucket#acl}. A bucket has owners, writers, and readers. Permissions can be
369
+ granted to an individual user's email address, a group's email address, as well
370
+ as many predefined lists. See the [Access Control
371
+ guide](https://cloud.google.com/storage/docs/access-control) for more.
372
+
373
+ Access to a bucket can be granted to a user by appending `"user-"` to the email
374
+ address:
375
+
376
+ ```ruby
377
+ require "google/cloud/storage"
378
+
379
+ storage = Google::Cloud::Storage.new
380
+
381
+ bucket = storage.bucket "my-todo-app"
382
+
383
+ email = "heidi@example.net"
384
+ bucket.acl.add_reader "user-#{email}"
385
+ ```
386
+
387
+ Access to a bucket can be granted to a group by appending `"group-"` to the
388
+ email address:
389
+
390
+ ```ruby
391
+ require "google/cloud/storage"
392
+
393
+ storage = Google::Cloud::Storage.new
394
+
395
+ bucket = storage.bucket "my-todo-app"
396
+
397
+ email = "authors@example.net"
398
+ bucket.acl.add_reader "group-#{email}"
399
+ ```
400
+
401
+ Access to a bucket can also be granted to a predefined list of permissions:
402
+
403
+ ```ruby
404
+ require "google/cloud/storage"
405
+
406
+ storage = Google::Cloud::Storage.new
407
+
408
+ bucket = storage.bucket "my-todo-app"
409
+
410
+ bucket.acl.public!
411
+ ```
412
+
413
+ ## Controlling Access to a File
414
+
415
+ Access to a file is controlled in two ways, either by the setting the default
416
+ permissions to all files in a bucket with
417
+ {Google::Cloud::Storage::Bucket#default_acl Bucket#default_acl}, or by setting
418
+ permissions to an individual file with {Google::Cloud::Storage::File#acl
419
+ File#acl}.
420
+
421
+ Access to a file can be granted to a user by appending `"user-"` to the email
422
+ address:
423
+
424
+ ```ruby
425
+ require "google/cloud/storage"
426
+
427
+ storage = Google::Cloud::Storage.new
428
+
429
+ bucket = storage.bucket "my-todo-app"
430
+ file = bucket.file "avatars/heidi/400x400.png"
431
+
432
+ email = "heidi@example.net"
433
+ file.acl.add_reader "user-#{email}"
434
+ ```
435
+
436
+ Access to a file can be granted to a group by appending `"group-"` to the email
437
+ address:
438
+
439
+ ```ruby
440
+ require "google/cloud/storage"
441
+
442
+ storage = Google::Cloud::Storage.new
443
+
444
+ bucket = storage.bucket "my-todo-app"
445
+ file = bucket.file "avatars/heidi/400x400.png"
446
+
447
+ email = "authors@example.net"
448
+ file.acl.add_reader "group-#{email}"
449
+ ```
450
+
451
+ Access to a file can also be granted to a predefined list of permissions:
452
+
453
+ ```ruby
454
+ require "google/cloud/storage"
455
+
456
+ storage = Google::Cloud::Storage.new
457
+
458
+ bucket = storage.bucket "my-todo-app"
459
+ file = bucket.file "avatars/heidi/400x400.png"
460
+
461
+ file.acl.public!
462
+ ```
463
+
464
+ ## Assigning payment to the requester
465
+
466
+ The requester pays feature enables the owner of a bucket to indicate that a
467
+ client accessing the bucket or a file it contains must assume the transit costs
468
+ related to the access.
469
+
470
+ Assign transit costs for bucket and file operations to requesting clients with
471
+ the `requester_pays` flag:
472
+
473
+ ```ruby
474
+ require "google/cloud/storage"
475
+
476
+ storage = Google::Cloud::Storage.new
477
+
478
+ bucket = storage.bucket "my-bucket"
479
+
480
+ bucket.requester_pays = true # API call
481
+ # Clients must now provide `user_project` option when calling
482
+ # Project#bucket to access this bucket.
483
+ ```
484
+
485
+ Once the `requester_pays` flag is enabled for a bucket, a client attempting to
486
+ access the bucket and its files must provide the `user_project` option to
487
+ {Google::Cloud::Storage::Project#bucket Project#bucket}. If the argument given
488
+ is `true`, transit costs for operations on the requested bucket or a file it
489
+ contains will be billed to the current project for the client. (See
490
+ {Google::Cloud::Storage::Project#project Project#project} for the ID of the
491
+ current project.)
492
+
493
+ ```ruby
494
+ require "google/cloud/storage"
495
+
496
+ storage = Google::Cloud::Storage.new
497
+
498
+ bucket = storage.bucket "other-project-bucket", user_project: true
499
+
500
+ files = bucket.files # Billed to current project
501
+ ```
502
+
503
+ If the argument is a project ID string, and the indicated project is authorized
504
+ for the currently authenticated service account, transit costs will be billed to
505
+ the indicated project.
506
+
507
+ ```ruby
508
+ require "google/cloud/storage"
509
+
510
+ storage = Google::Cloud::Storage.new
511
+
512
+ bucket = storage.bucket "other-project-bucket",
513
+ user_project: "my-other-project"
514
+ files = bucket.files # Billed to "my-other-project"
515
+ ```
516
+
517
+ ## Configuring Pub/Sub notification subscriptions
518
+
519
+ You can configure notifications to send Google Cloud Pub/Sub messages about
520
+ changes to files in your buckets. For example, you can track files that are
521
+ created and deleted in your bucket. Each notification contains information
522
+ describing both the event that triggered it and the file that changed.
523
+
524
+ You can send notifications to any Cloud Pub/Sub topic in any project for which
525
+ your service account has sufficient permissions. As shown below, you need to
526
+ explicitly grant permission to your service account to enable Google Cloud
527
+ Storage to publish on behalf of your account. (Even if your current project
528
+ created and owns the topic.)
529
+
530
+ ```ruby
531
+ require "google/cloud/pubsub"
532
+ require "google/cloud/storage"
533
+
534
+ pubsub = Google::Cloud::Pubsub.new
535
+ storage = Google::Cloud::Storage.new
536
+
537
+ topic = pubsub.create_topic "my-topic"
538
+ topic.policy do |p|
539
+ p.add "roles/pubsub.publisher",
540
+ "serviceAccount:#{storage.service_account_email}"
541
+ end
542
+
543
+ bucket = storage.bucket "my-bucket"
544
+
545
+ notification = bucket.create_notification topic.name
546
+ ```
547
+
548
+ ## Configuring retries and timeout
549
+
550
+ You can configure how many times API requests may be automatically retried. When
551
+ an API request fails, the response will be inspected to see if the request meets
552
+ criteria indicating that it may succeed on retry, such as `500` and `503` status
553
+ codes or a specific internal error code such as `rateLimitExceeded`. If it meets
554
+ the criteria, the request will be retried after a delay. If another error
555
+ occurs, the delay will be increased before a subsequent attempt, until the
556
+ `retries` limit is reached.
557
+
558
+ You can also set the request `timeout` value in seconds.
559
+
560
+ ```ruby
561
+ require "google/cloud/storage"
562
+
563
+ storage = Google::Cloud::Storage.new retries: 10, timeout: 120
564
+ ```
565
+
566
+ See the [Storage status and error
567
+ codes](https://cloud.google.com/storage/docs/json_api/v1/status-codes)
568
+ for a list of error conditions.
569
+
570
+ ## Additional information
571
+
572
+ Google Cloud Storage can be configured to use logging. To learn more, see the
573
+ {file:LOGGING.md Logging guide}.