google-cloud-storage 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 474743d603eb149e305fbbbaf7db863b04806cc0
4
- data.tar.gz: f8eb5c36d9816604516c4c0d91c5350ba5152c76
3
+ metadata.gz: 68663d7f63e76435070cbcd4748022693d2561a5
4
+ data.tar.gz: cb6ecfdfaadeb41ed569d08ca9c069edf67c0ea4
5
5
  SHA512:
6
- metadata.gz: e75929f212d3cea1b8b7064136ff2a6228571358225e1980ba5f46d982a31dc212d50cda1d59d117356c8429388b89d40502352f7020c7578a40e5361dc4ba82
7
- data.tar.gz: ebd0b51233c64e48728850b2eb0a0e5ad4699486e0bc8a12d7be442b34092e7c73419051c857dabd4a23b6693da1a0d3bf9e2e3287e61df2a5bc9d0a523d16d4
6
+ metadata.gz: 1382c121c18b2cc7fa8a916895bec8c0a2b7c40a99b4fea1a54edc3562ba53b8e9f9538ea3e613f20c39572bdcc21cb2190d7a26da82240fe966901b24bf8ebb
7
+ data.tar.gz: 78b666563914f310968e53d4701c78781ebedae1977e1f51f6f3874ee69d23e9dfcf0b235178d0e13a0437c22fd41a12e9210577e1aa23d0ca2937b4babb0500
data/README.md CHANGED
@@ -48,8 +48,6 @@ This library is supported on Ruby 2.0+.
48
48
 
49
49
  This library follows [Semantic Versioning](http://semver.org/).
50
50
 
51
- It is currently in major version zero (0.y.z), which means that anything may change at any time and the public API should not be considered stable.
52
-
53
51
  ## Contributing
54
52
 
55
53
  Contributions to this library are always welcome and highly encouraged.
@@ -234,12 +234,25 @@ module Google
234
234
  ##
235
235
  # The bucket's storage class. This defines how objects in the bucket are
236
236
  # stored and determines the SLA and the cost of storage. Values include
237
- # `MULTI_REGIONAL`, `REGIONAL`, `NEARLINE`, `COLDLINE`, `STANDARD`,
238
- # and `DURABLE_REDUCED_AVAILABILITY`.
237
+ # `MULTI_REGIONAL`, `REGIONAL`, `NEARLINE`, `COLDLINE`, and
238
+ # `DURABLE_REDUCED_AVAILABILITY`.
239
239
  def storage_class
240
240
  @gapi.storage_class
241
241
  end
242
242
 
243
+ ##
244
+ # Updates the bucket's storage class. This defines how objects in the
245
+ # bucket are stored and determines the SLA and the cost of storage.
246
+ # Accepted values include `:multi_regional`, `:regional`, `:nearline`,
247
+ # and `:coldline`, as well as the equivalent strings returned by
248
+ # {Bucket#storage_class}. For more information, see [Storage
249
+ # Classes](https://cloud.google.com/storage/docs/storage-classes).
250
+ # @param [Symbol, String] new_storage_class Storage class of the bucket.
251
+ def storage_class= new_storage_class
252
+ @gapi.storage_class = storage_class_for(new_storage_class)
253
+ patch_gapi! :storage_class
254
+ end
255
+
243
256
  ##
244
257
  # Whether [Object
245
258
  # Versioning](https://cloud.google.com/storage/docs/object-versioning)
@@ -514,9 +527,13 @@ module Google
514
527
  # @param [String] path Name (path) of the file.
515
528
  # @param [Integer] generation When present, selects a specific revision
516
529
  # of this object. Default is the latest version.
530
+ # @param [Boolean] skip_lookup Optionally create a Bucket object
531
+ # without verifying the bucket resource exists on the Storage service.
532
+ # Calls made on this object will raise errors if the bucket resource
533
+ # does not exist. Default is `false`.
517
534
  # @param [String] encryption_key Optional. The customer-supplied,
518
535
  # AES-256 encryption key used to encrypt the file, if one was provided
519
- # to {#create_file}.
536
+ # to {#create_file}. (Not used if `skip_lookup` is also set.)
520
537
  #
521
538
  # @return [Google::Cloud::Storage::File, nil] Returns nil if file does
522
539
  # not exist
@@ -531,8 +548,13 @@ module Google
531
548
  # file = bucket.file "path/to/my-file.ext"
532
549
  # puts file.name
533
550
  #
534
- def file path, generation: nil, encryption_key: nil
551
+ def file path, generation: nil, skip_lookup: nil, encryption_key: nil
535
552
  ensure_service!
553
+ if skip_lookup
554
+ return File.new_lazy name, path, service,
555
+ generation: generation,
556
+ user_project: user_project
557
+ end
536
558
  gapi = service.get_file name, path, generation: generation,
537
559
  key: encryption_key,
538
560
  user_project: user_project
@@ -615,12 +637,11 @@ module Google
615
637
  # file as "x-goog-meta-" response headers.
616
638
  # @param [Symbol, String] storage_class Storage class of the file.
617
639
  # Determines how the file is stored and determines the SLA and the
618
- # cost of storage. Values include `:multi_regional`, `:regional`,
619
- # `:nearline`, `:coldline`, `:standard`, and `:dra` (Durable Reduced
620
- # Availability), as well as the strings returned by
621
- # {#storage_class}. For more information, see [Storage
622
- # Classes](https://cloud.google.com/storage/docs/storage-classes) and
623
- # [Per-Object Storage
640
+ # cost of storage. Accepted values include `:multi_regional`,
641
+ # `:regional`, `:nearline`, and `:coldline`, as well as the equivalent
642
+ # strings returned by {#storage_class}. For more information, see
643
+ # [Storage Classes](https://cloud.google.com/storage/docs/storage-classes)
644
+ # and [Per-Object Storage
624
645
  # Class](https://cloud.google.com/storage/docs/per-object-storage-class).
625
646
  # The default value is the default storage class for the bucket.
626
647
  # @param [String] encryption_key Optional. A customer-supplied, AES-256
@@ -1120,17 +1141,53 @@ module Google
1120
1141
  # Reloads the bucket with current data from the Storage service.
1121
1142
  def reload!
1122
1143
  ensure_service!
1123
- @gapi = service.get_bucket name
1144
+ @gapi = service.get_bucket name, user_project: user_project
1145
+ # If NotFound then lazy will never be unset
1146
+ @lazy = nil
1147
+ self
1124
1148
  end
1125
1149
  alias_method :refresh!, :reload!
1126
1150
 
1151
+ ##
1152
+ # Determines whether the bucket exists in the Storage service.
1153
+ def exists?
1154
+ # Always true if we have a grpc object
1155
+ return true unless lazy?
1156
+ # If we have a value, return it
1157
+ return @exists unless @exists.nil?
1158
+ ensure_gapi!
1159
+ @exists = true
1160
+ rescue Google::Cloud::NotFoundError
1161
+ @exists = false
1162
+ end
1163
+
1164
+ ##
1165
+ # @private
1166
+ # Determines whether the bucket was created without retrieving the
1167
+ # resource record from the API.
1168
+ def lazy?
1169
+ @lazy
1170
+ end
1171
+
1127
1172
  ##
1128
1173
  # @private New Bucket from a Google API Client object.
1129
1174
  def self.from_gapi gapi, service, user_project: nil
1130
- new.tap do |f|
1131
- f.gapi = gapi
1132
- f.service = service
1133
- f.user_project = user_project
1175
+ new.tap do |b|
1176
+ b.gapi = gapi
1177
+ b.service = service
1178
+ b.user_project = user_project
1179
+ end
1180
+ end
1181
+
1182
+ ##
1183
+ # @private New lazy Bucket object without making an HTTP request.
1184
+ def self.new_lazy name, service, user_project: nil
1185
+ # TODO: raise if name is nil?
1186
+ new.tap do |b|
1187
+ b.gapi.name = name
1188
+ b.service = service
1189
+ b.user_project = user_project
1190
+ b.instance_variable_set :@lazy, true
1134
1191
  end
1135
1192
  end
1136
1193
 
@@ -1142,6 +1199,14 @@ module Google
1142
1199
  fail "Must have active connection" unless service
1143
1200
  end
1144
1201
 
1202
+ ##
1203
+ # Ensures the Google::Apis::StorageV1::Bucket object exists.
1204
+ def ensure_gapi!
1205
+ ensure_service!
1206
+ return unless lazy?
1207
+ reload!
1208
+ end
1209
+
1145
1210
  def patch_gapi! *attributes
1146
1211
  attributes.flatten!
1147
1212
  return if attributes.empty?
@@ -1152,6 +1217,8 @@ module Google
1152
1217
  patch_gapi = Google::Apis::StorageV1::Bucket.new patch_args
1153
1218
  @gapi = service.patch_bucket name, patch_gapi,
1154
1219
  user_project: user_project
1220
+ @lazy = nil
1221
+ self
1155
1222
  end
1156
1223
 
1157
1224
  ##
@@ -314,10 +314,10 @@ module Google
314
314
 
315
315
  ##
316
316
  # Updates how the file is stored and determines the SLA and the cost of
317
- # storage. Values include `:multi_regional`, `:regional`, `:nearline`,
318
- # `:coldline`, `:standard`, and `:dra` (Durable Reduced Availability),
319
- # as well as the strings returned by {File#storage_class} or
320
- # {Bucket#storage_class}. For more information, see [Storage
317
+ # storage. Accepted values include `:multi_regional`, `:regional`,
318
+ # `:nearline`, and `:coldline`, as well as the equivalent strings
319
+ # returned by {File#storage_class} or {Bucket#storage_class}. For more
320
+ # information, see [Storage
321
321
  # Classes](https://cloud.google.com/storage/docs/storage-classes) and
322
322
  # [Per-Object Storage
323
323
  # Class](https://cloud.google.com/storage/docs/per-object-storage-class).
@@ -329,6 +329,45 @@ module Google
329
329
  update_gapi! :storage_class
330
330
  end
331
331
 
332
+ ##
333
+ # Retrieves a list of versioned files for the current object.
334
+ #
335
+ # Useful for listing archived versions of the file, restoring the live
336
+ # version of the file to an older version, or deleting an archived
337
+ # version. You can turn versioning on or off for a bucket at any time
338
+ # with {Bucket#versioning=}. Turning versioning off leaves existing file
339
+ # versions in place and causes the bucket to stop accumulating new
340
+ # archived object versions. (See {Bucket#versioning} and
341
+ # {File#generation})
342
+ #
343
+ # @see https://cloud.google.com/storage/docs/object-versioning Object
344
+ # Versioning
345
+ #
346
+ # @return [Array<Google::Cloud::Storage::File>] (See
347
+ # {Google::Cloud::Storage::File::List})
348
+ #
349
+ # @example
350
+ # require "google/cloud/storage"
351
+ #
352
+ # storage = Google::Cloud::Storage.new
353
+ #
354
+ # bucket = storage.bucket "my-bucket"
355
+ #
356
+ # file = bucket.file "path/to/my-file.ext"
357
+ # file.generation #=> 1234567890
358
+ # file.generations.each do |versioned_file|
359
+ # versioned_file.generation
360
+ # end
361
+ #
362
+ def generations
363
+ ensure_service!
364
+ gapi = service.list_files bucket, prefix: name,
365
+ versions: true,
366
+ user_project: user_project
367
+ File::List.from_gapi gapi, service, bucket, name, nil, nil, true,
368
+ user_project: user_project
369
+ end
370
+
332
371
  ##
333
372
  # Updates the file with changes made in the given block in a single
334
373
  # PATCH request. The following attributes may be set: {#cache_control=},
@@ -639,6 +678,11 @@ module Google
639
678
  # Permanently deletes the file.
640
679
  #
641
680
  # @return [Boolean] Returns `true` if the file was deleted.
681
+ # @param [Boolean, Integer] generation Specify a version of the file to
682
+ # delete. When `true`, it will delete the version returned by
683
+ # {#generation}. The default behavior is to delete the latest version
684
+ # of the file (regardless of the version to which the file is set,
685
+ # which is the version returned by {#generation}.)
642
686
  #
643
687
  # @example
644
688
  # require "google/cloud/storage"
@@ -650,9 +694,31 @@ module Google
650
694
  # file = bucket.file "path/to/my-file.ext"
651
695
  # file.delete
652
696
  #
653
- def delete
697
+ # @example The file's generation can used by passing `true`:
698
+ # require "google/cloud/storage"
699
+ #
700
+ # storage = Google::Cloud::Storage.new
701
+ #
702
+ # bucket = storage.bucket "my-bucket"
703
+ #
704
+ # file = bucket.file "path/to/my-file.ext"
705
+ # file.delete generation: true
706
+ #
707
+ # @example A generation can also be specified:
708
+ # require "google/cloud/storage"
709
+ #
710
+ # storage = Google::Cloud::Storage.new
711
+ #
712
+ # bucket = storage.bucket "my-bucket"
713
+ #
714
+ # file = bucket.file "path/to/my-file.ext"
715
+ # file.delete generation: 123456
716
+ #
717
+ def delete generation: nil
718
+ generation = self.generation if generation == true
654
719
  ensure_service!
655
- service.delete_file bucket, name, user_project: user_project
720
+ service.delete_file bucket, name, generation: generation,
721
+ user_project: user_project
656
722
  true
657
723
  end
658
724
 
@@ -846,12 +912,75 @@ module Google
846
912
 
847
913
  ##
848
914
  # Reloads the file with current data from the Storage service.
849
- def reload!
915
+ #
916
+ # @param [Boolean, Integer] generation Specify a version of the file to
917
+ # reload with. When `true`, it will reload the version returned by
918
+ # {#generation}. The default behavior is to reload with the latest
919
+ # version of the file (regardless of the version to which the file is
920
+ # set, which is the version returned by {#generation}.)
921
+ #
922
+ # @example
923
+ # require "google/cloud/storage"
924
+ #
925
+ # storage = Google::Cloud::Storage.new
926
+ #
927
+ # bucket = storage.bucket "my-bucket"
928
+ #
929
+ # file = bucket.file "path/to/my-file.ext"
930
+ # file.reload!
931
+ #
932
+ # @example The file's generation can used by passing `true`:
933
+ # require "google/cloud/storage"
934
+ #
935
+ # storage = Google::Cloud::Storage.new
936
+ #
937
+ # bucket = storage.bucket "my-bucket"
938
+ #
939
+ # file = bucket.file "path/to/my-file.ext", generation: 123456
940
+ # file.reload! generation: true
941
+ #
942
+ # @example A generation can also be specified:
943
+ # require "google/cloud/storage"
944
+ #
945
+ # storage = Google::Cloud::Storage.new
946
+ #
947
+ # bucket = storage.bucket "my-bucket"
948
+ #
949
+ # file = bucket.file "path/to/my-file.ext", generation: 123456
950
+ # file.reload! generation: 123457
951
+ #
952
+ def reload! generation: nil
953
+ generation = self.generation if generation == true
850
954
  ensure_service!
851
- @gapi = service.get_file bucket, name, user_project: user_project
955
+ @gapi = service.get_file bucket, name, generation: generation,
956
+ user_project: user_project
957
+ # If NotFound then lazy will never be unset
958
+ @lazy = nil
959
+ self
852
960
  end
853
961
  alias_method :refresh!, :reload!
854
962
 
963
+ ##
964
+ # Determines whether the file exists in the Storage service.
965
+ def exists?
966
+ # Always true if we have a grpc object
967
+ return true unless lazy?
968
+ # If we have a value, return it
969
+ return @exists unless @exists.nil?
970
+ ensure_gapi!
971
+ @exists = true
972
+ rescue Google::Cloud::NotFoundError
973
+ @exists = false
974
+ end
975
+
976
+ ##
977
+ # @private
978
+ # Determines whether the file was created without retrieving the
979
+ # resource record from the API.
980
+ def lazy?
981
+ @lazy
982
+ end
983
+
855
984
  ##
856
985
  # @private URI of the location and file name in the format of
857
986
  # <code>gs://my-bucket/file-name.json</code>.
@@ -869,6 +998,21 @@ module Google
869
998
  end
870
999
  end
871
1000
 
1001
+ ##
1002
+ # @private New lazy Bucket object without making an HTTP request.
1003
+ def self.new_lazy bucket, name, service, generation: nil,
1004
+ user_project: nil
1005
+ # TODO: raise if name is nil?
1006
+ new.tap do |f|
1007
+ f.gapi.bucket = bucket
1008
+ f.gapi.name = name
1009
+ f.gapi.generation = generation
1010
+ f.service = service
1011
+ f.user_project = user_project
1012
+ f.instance_variable_set :@lazy, true
1013
+ end
1014
+ end
1015
+
872
1016
  protected
873
1017
 
874
1018
  ##
@@ -877,6 +1021,14 @@ module Google
877
1021
  fail "Must have active connection" unless service
878
1022
  end
879
1023
 
1024
+ ##
1025
+ # Ensures the Google::Apis::StorageV1::Bucket object exists.
1026
+ def ensure_gapi!
1027
+ ensure_service!
1028
+ return unless lazy?
1029
+ reload! generation: true
1030
+ end
1031
+
880
1032
  def update_gapi! *attributes
881
1033
  attributes.flatten!
882
1034
  return if attributes.empty?
@@ -931,8 +1083,8 @@ module Google
931
1083
  def verify_file! file, verify = :md5
932
1084
  verify_md5 = verify == :md5 || verify == :all
933
1085
  verify_crc32c = verify == :crc32c || verify == :all
934
- Verifier.verify_md5! self, file if verify_md5
935
- Verifier.verify_crc32c! self, file if verify_crc32c
1086
+ Verifier.verify_md5! self, file if verify_md5 && md5
1087
+ Verifier.verify_crc32c! self, file if verify_crc32c && crc32c
936
1088
  file
937
1089
  end
938
1090
 
@@ -163,7 +163,7 @@ module Google
163
163
  delimiter = nil, max = nil, versions = nil,
164
164
  user_project: nil
165
165
  files = new(Array(gapi_list.items).map do |gapi_object|
166
- File.from_gapi gapi_object, service
166
+ File.from_gapi gapi_object, service, user_project: user_project
167
167
  end)
168
168
  files.instance_variable_set :@token, gapi_list.next_page_token
169
169
  files.instance_variable_set :@prefixes, Array(gapi_list.prefixes)
@@ -137,6 +137,10 @@ module Google
137
137
  # Retrieves bucket by name.
138
138
  #
139
139
  # @param [String] bucket_name Name of a bucket.
140
+ # @param [Boolean] skip_lookup Optionally create a Bucket object
141
+ # without verifying the bucket resource exists on the Storage service.
142
+ # Calls made on this object will raise errors if the bucket resource
143
+ # does not exist. Default is `false`.
140
144
  # @param [Boolean, String] user_project If the `requester_pays` flag is
141
145
  # enabled for the requested bucket, and if this parameter is set to
142
146
  # `true`, transit costs for operations on the requested bucket or a
@@ -180,7 +184,11 @@ module Google
180
184
  # user_project: "my-other-project"
181
185
  # files = bucket.files # Billed to "my-other-project"
182
186
  #
183
- def bucket bucket_name, user_project: nil
187
+ def bucket bucket_name, skip_lookup: false, user_project: nil
188
+ if skip_lookup
189
+ return Bucket.new_lazy bucket_name, service,
190
+ user_project: user_project
191
+ end
184
192
  gapi = service.get_bucket bucket_name, user_project: user_project
185
193
  Bucket.from_gapi gapi, service, user_project: user_project
186
194
  rescue Google::Cloud::NotFoundError
@@ -258,12 +266,11 @@ module Google
258
266
  # Logs](https://cloud.google.com/storage/docs/access-logs).
259
267
  # @param [Symbol, String] storage_class Defines how objects in the
260
268
  # bucket are stored and determines the SLA and the cost of storage.
261
- # Values include `:multi_regional`, `:regional`, `:nearline`,
262
- # `:coldline`, `:standard`, and `:dra` (Durable Reduced
263
- # Availability), as well as the strings returned by
269
+ # Accepted values include `:multi_regional`, `:regional`, `:nearline`,
270
+ # and `:coldline`, as well as the equivalent strings returned by
264
271
  # {Bucket#storage_class}. For more information, see [Storage
265
272
  # Classes](https://cloud.google.com/storage/docs/storage-classes). The
266
- # default value is `:standard`, which is equivalent to
273
+ # default value is the Standard storage class, which is equivalent to
267
274
  # `:multi_regional` or `:regional` depending on the bucket's location
268
275
  # settings.
269
276
  # @param [Boolean] versioning Whether [Object
@@ -313,12 +320,13 @@ module Google
313
320
  website_main: nil, website_404: nil, versioning: nil
314
321
  new_bucket = Google::Apis::StorageV1::Bucket.new({
315
322
  name: bucket_name,
316
- location: location,
317
- storage_class: storage_class_for(storage_class)
323
+ location: location
318
324
  }.delete_if { |_, v| v.nil? })
325
+ storage_class = storage_class_for(storage_class)
319
326
  updater = Bucket::Updater.new(new_bucket).tap do |b|
320
327
  b.logging_bucket = logging_bucket unless logging_bucket.nil?
321
328
  b.logging_prefix = logging_prefix unless logging_prefix.nil?
329
+ b.storage_class = storage_class unless storage_class.nil?
322
330
  b.website_main = website_main unless website_main.nil?
323
331
  b.website_404 = website_404 unless website_404.nil?
324
332
  b.versioning = versioning unless versioning.nil?
@@ -326,9 +326,11 @@ module Google
326
326
 
327
327
  ##
328
328
  # Permanently deletes a file.
329
- def delete_file bucket_name, file_path, user_project: nil
329
+ def delete_file bucket_name, file_path, generation: nil,
330
+ user_project: nil
330
331
  execute do
331
332
  service.delete_object bucket_name, file_path,
333
+ generation: generation,
332
334
  user_project: user_project(user_project)
333
335
  end
334
336
  end
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Storage
19
- VERSION = "1.3.0"
19
+ VERSION = "1.4.0"
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-07-11 00:00:00.000000000 Z
12
+ date: 2017-08-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-cloud-core