google-cloud-storage 1.37.0 → 1.54.0

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.
@@ -109,6 +109,24 @@ module Google
109
109
  @gapi.id
110
110
  end
111
111
 
112
+ ##
113
+ # The autoclass configuration of the bucket
114
+ #
115
+ # @return [Google::Apis::StorageV1::Bucket::Autoclass]
116
+ #
117
+ def autoclass
118
+ @gapi.autoclass
119
+ end
120
+
121
+ ##
122
+ # The object retention configuration of the bucket
123
+ #
124
+ # @return [Google::Apis::StorageV1::Bucket::ObjectRetention]
125
+ #
126
+ def object_retention
127
+ @gapi.object_retention
128
+ end
129
+
112
130
  ##
113
131
  # The name of the bucket.
114
132
  #
@@ -301,6 +319,20 @@ module Google
301
319
  @gapi.location_type
302
320
  end
303
321
 
322
+ ##
323
+ # Returns the list of regional locations for custom dual-region buckets.
324
+ #
325
+ # @return [String, nil] Returns nil if the property has not been set before creation,
326
+ # if the bucket's resource has not been loaded from the server,
327
+ # or if the bucket is not a dual-regions bucket.
328
+
329
+ # @see https://cloud.google.com/storage/docs/json_api/v1/buckets and
330
+ # https://cloud.google.com/storage/docs/locations
331
+ #
332
+ def data_locations
333
+ @gapi.custom_placement_config&.data_locations
334
+ end
335
+
304
336
  ##
305
337
  # The destination bucket name for the bucket's logs.
306
338
  #
@@ -393,6 +425,77 @@ module Google
393
425
  patch_gapi! :storage_class
394
426
  end
395
427
 
428
+ ##
429
+ # Whether Autoclass is enabled for the bucket.
430
+ #
431
+ # @return [Boolean]
432
+ #
433
+ def autoclass_enabled
434
+ @gapi.autoclass&.enabled?
435
+ end
436
+
437
+ ##
438
+ # Toggle time of the autoclass
439
+ #
440
+ # @return [DateTime]
441
+ #
442
+ def autoclass_toggle_time
443
+ @gapi.autoclass&.toggle_time
444
+ end
445
+
446
+ ##
447
+ # Terminal Storage class of the autoclass
448
+ #
449
+ # @return [String]
450
+ #
451
+ def autoclass_terminal_storage_class
452
+ @gapi.autoclass&.terminal_storage_class
453
+ end
454
+
455
+ ##
456
+ # Update time at which the autoclass terminal storage class was last modified
457
+ #
458
+ # @return [DateTime]
459
+ #
460
+ def autoclass_terminal_storage_class_update_time
461
+ @gapi.autoclass&.terminal_storage_class_update_time
462
+ end
463
+
464
+ ##
465
+ # Updates bucket's autoclass configuration. This defines the default class for objects in the
466
+ # bucket and down/up-grades the storage class of objects based on the access patterns.
467
+ # Accepted values are `:false`, and `:true`.
468
+ #
469
+ # For more information, see [Storage
470
+ # Classes](https://cloud.google.com/storage/docs/using-autoclass).
471
+ #
472
+ # @param [Boolean] toggle for autoclass configuration of the bucket.
473
+ #
474
+ def autoclass_enabled= toggle
475
+ @gapi.autoclass ||= API::Bucket::Autoclass.new
476
+ @gapi.autoclass.enabled = toggle
477
+ patch_gapi! :autoclass
478
+ end
479
+
480
+ ##
481
+ # Update method to update all attributes of autoclass of a bucket
482
+ # It accepts params as a Hash of attributes in the following format:
483
+ #
484
+ # { enabled: true, terminal_storage_class: "ARCHIVE" }
485
+ #
486
+ # terminal_storage_class field is optional. It defaults to `NEARLINE`.
487
+ # Valid terminal_storage_class values are `NEARLINE` and `ARCHIVE`.
488
+ #
489
+ # @param [Hash(String => String)] autoclass_attributes
490
+ #
491
+ def update_autoclass autoclass_attributes
492
+ @gapi.autoclass ||= API::Bucket::Autoclass.new
493
+ autoclass_attributes.each do |k, v|
494
+ @gapi.autoclass.send "#{k}=", v
495
+ end
496
+ patch_gapi! :autoclass
497
+ end
498
+
396
499
  ##
397
500
  # Whether [Object
398
501
  # Versioning](https://cloud.google.com/storage/docs/object-versioning)
@@ -1091,6 +1194,121 @@ module Google
1091
1194
  patch_gapi! :rpo
1092
1195
  end
1093
1196
 
1197
+ ##
1198
+ # The bucket's soft delete policy. If this policy is set, any deleted
1199
+ # objects will be soft-deleted according to the time specified in the
1200
+ # policy.
1201
+ # This value can be modified by calling {#soft_delete_policy=}.
1202
+ #
1203
+ # @return [Google::Apis::StorageV1::Bucket::SoftDeletePolicy] The default retention policy is for 7
1204
+ # days.
1205
+ #
1206
+ # @example
1207
+ # require "google/cloud/storage"
1208
+ #
1209
+ # storage = Google::Cloud::Storage.new
1210
+ #
1211
+ # bucket = storage.bucket "my-bucket"
1212
+ #
1213
+ # bucket.soft_delete_policy
1214
+ #
1215
+ def soft_delete_policy
1216
+ @gapi.soft_delete_policy
1217
+ end
1218
+
1219
+ ##
1220
+ # Sets the value for Soft Delete Policy in the bucket. This value can
1221
+ # be queried by calling {#soft_delete_policy}.
1222
+ #
1223
+ # @param [Google::Apis::StorageV1::Bucket::SoftDeletePolicy,
1224
+ # Hash(String => String)] new_soft_delete_policy The bucket's
1225
+ # new Soft Delete Policy.
1226
+ #
1227
+ # @example Set Soft Delete Policy to 10 days using SoftDeletePolicy class:
1228
+ # require "google/cloud/storage"
1229
+ # require "date"
1230
+ #
1231
+ # storage = Google::Cloud::Storage.new
1232
+ #
1233
+ # bucket = storage.bucket "my-bucket"
1234
+ #
1235
+ # soft_delete_policy = Google::Apis::StorageV1::Bucket::SoftDeletePolicy.new
1236
+ # soft_delete_policy.retention_duration_seconds = 10*24*60*60
1237
+ #
1238
+ # bucket.soft_delete_policy = soft_delete_policy
1239
+ #
1240
+ # @example Set Soft Delete Policy to 5 days using Hash:
1241
+ # require "google/cloud/storage"
1242
+ # require "date"
1243
+ #
1244
+ # storage = Google::Cloud::Storage.new
1245
+ #
1246
+ # bucket = storage.bucket "my-bucket"
1247
+ #
1248
+ # soft_delete_policy = { retention_duration_seconds: 432000 }
1249
+ # bucket.soft_delete_policy = soft_delete_policy
1250
+ #
1251
+ def soft_delete_policy= new_soft_delete_policy
1252
+ @gapi.soft_delete_policy = new_soft_delete_policy || {}
1253
+ patch_gapi! :soft_delete_policy
1254
+ end
1255
+
1256
+ ##
1257
+ # The bucket's hierarchical namespace (Folders) configuration.
1258
+ # This value can be modified by calling {#hierarchical_namespace=}.
1259
+ #
1260
+ # @return [Google::Apis::StorageV1::Bucket::HierarchicalNamespace]
1261
+ #
1262
+ # @example
1263
+ # require "google/cloud/storage"
1264
+ #
1265
+ # storage = Google::Cloud::Storage.new
1266
+ #
1267
+ # bucket = storage.bucket "my-bucket"
1268
+ #
1269
+ # bucket.hierarchical_namespace
1270
+ #
1271
+ def hierarchical_namespace
1272
+ @gapi.hierarchical_namespace
1273
+ end
1274
+
1275
+ ##
1276
+ # Sets the value of Hierarchical Namespace (Folders) for the bucket.
1277
+ # This can only be enabled at bucket create time. If this is enabled,
1278
+ # Uniform Bucket-Level Access must also be enabled.
1279
+ # This value can be queried by calling {#hierarchical_namespace}.
1280
+ #
1281
+ # @param [Google::Apis::StorageV1::Bucket::HierarchicalNamespace,
1282
+ # Hash(String => String)] new_hierarchical_namespace The
1283
+ # bucket's new Hierarchical Namespace Configuration.
1284
+ #
1285
+ # @example Enabled Hierarchical Namespace using HierarchicalNamespace class:
1286
+ # require "google/cloud/storage"
1287
+ #
1288
+ # storage = Google::Cloud::Storage.new
1289
+ #
1290
+ # bucket = storage.bucket "my-bucket"
1291
+ #
1292
+ # hierarchical_namespace = Google::Apis::StorageV1::Bucket::HierarchicalNamespace.new
1293
+ # hierarchical_namespace.enabled = true
1294
+ #
1295
+ # bucket.hierarchical_namespace = hierarchical_namespace
1296
+ #
1297
+ # @example Disable Hierarchical Namespace using Hash:
1298
+ # require "google/cloud/storage"
1299
+ #
1300
+ # storage = Google::Cloud::Storage.new
1301
+ #
1302
+ # bucket = storage.bucket "my-bucket"
1303
+ #
1304
+ # hierarchical_namespace = { enabled: false }
1305
+ # bucket.hierarchical_namespace = hierarchical_namespace
1306
+ #
1307
+ def hierarchical_namespace= new_hierarchical_namespace
1308
+ @gapi.hierarchical_namespace = new_hierarchical_namespace || {}
1309
+ patch_gapi! :hierarchical_namespace
1310
+ end
1311
+
1094
1312
  ##
1095
1313
  # Updates the bucket with changes made in the given block in a single
1096
1314
  # PATCH request. The following attributes may be set: {#cors},
@@ -1156,9 +1374,9 @@ module Google
1156
1374
  updater.check_for_mutable_cors!
1157
1375
  updater.check_for_mutable_lifecycle!
1158
1376
  return if updater.updates.empty?
1159
- patch_gapi! updater.updates,
1160
- if_metageneration_match: if_metageneration_match,
1161
- if_metageneration_not_match: if_metageneration_not_match
1377
+ update_gapi! updater.updates,
1378
+ if_metageneration_match: if_metageneration_match,
1379
+ if_metageneration_not_match: if_metageneration_not_match
1162
1380
  end
1163
1381
 
1164
1382
  ##
@@ -1205,6 +1423,9 @@ module Google
1205
1423
  # `prefixes` are omitted.
1206
1424
  # @param [String] token A previously-returned page token representing
1207
1425
  # part of the larger set of results to view.
1426
+ # @param [String] match_glob A glob pattern used to filter results returned in items (e.g. `foo*bar`).
1427
+ # The string value must be UTF-8 encoded. See:
1428
+ # https://cloud.google.com/storage/docs/json_api/v1/objects/list#list-object-glob
1208
1429
  # @param [Integer] max Maximum number of items plus prefixes to return.
1209
1430
  # As duplicate prefixes are omitted, fewer total results may be
1210
1431
  # returned than requested. The default value of this parameter is
@@ -1213,6 +1434,11 @@ module Google
1213
1434
  # as distinct results. The default is `false`. For more information,
1214
1435
  # see [Object Versioning
1215
1436
  # ](https://cloud.google.com/storage/docs/object-versioning).
1437
+ # @param [Boolean] include_folders_as_prefixes If `true`, will also include
1438
+ # folders and managed folders, besides objects, in the returned prefixes.
1439
+ # Only applicable if delimiter is set to '/'.
1440
+ # @param [Boolean] soft_deleted If true, only soft-deleted object
1441
+ # versions will be listed. The default is false.
1216
1442
  #
1217
1443
  # @return [Array<Google::Cloud::Storage::File>] (See
1218
1444
  # {Google::Cloud::Storage::File::List})
@@ -1240,14 +1466,22 @@ module Google
1240
1466
  # end
1241
1467
  #
1242
1468
  def files prefix: nil, delimiter: nil, token: nil, max: nil,
1243
- versions: nil
1469
+ versions: nil, match_glob: nil, include_folders_as_prefixes: nil,
1470
+ soft_deleted: nil
1244
1471
  ensure_service!
1245
1472
  gapi = service.list_files name, prefix: prefix, delimiter: delimiter,
1246
1473
  token: token, max: max,
1247
1474
  versions: versions,
1248
- user_project: user_project
1475
+ user_project: user_project,
1476
+ match_glob: match_glob,
1477
+ include_folders_as_prefixes: include_folders_as_prefixes,
1478
+ soft_deleted: soft_deleted
1249
1479
  File::List.from_gapi gapi, service, name, prefix, delimiter, max,
1250
- versions, user_project: user_project
1480
+ versions,
1481
+ user_project: user_project,
1482
+ match_glob: match_glob,
1483
+ include_folders_as_prefixes: include_folders_as_prefixes,
1484
+ soft_deleted: soft_deleted
1251
1485
  end
1252
1486
  alias find_files files
1253
1487
 
@@ -1283,6 +1517,8 @@ module Google
1283
1517
  # @param [String] encryption_key Optional. The customer-supplied,
1284
1518
  # AES-256 encryption key used to encrypt the file, if one was provided
1285
1519
  # to {#create_file}. (Not used if `skip_lookup` is also set.)
1520
+ # @param [Boolean] soft_deleted Optional. If true, only soft-deleted
1521
+ # object versions will be listed. The default is false.
1286
1522
  #
1287
1523
  # @return [Google::Cloud::Storage::File, nil] Returns nil if file does
1288
1524
  # not exist
@@ -1304,7 +1540,8 @@ module Google
1304
1540
  if_metageneration_match: nil,
1305
1541
  if_metageneration_not_match: nil,
1306
1542
  skip_lookup: nil,
1307
- encryption_key: nil
1543
+ encryption_key: nil,
1544
+ soft_deleted: nil
1308
1545
  ensure_service!
1309
1546
  if skip_lookup
1310
1547
  return File.new_lazy name, path, service,
@@ -1317,7 +1554,8 @@ module Google
1317
1554
  if_metageneration_match: if_metageneration_match,
1318
1555
  if_metageneration_not_match: if_metageneration_not_match,
1319
1556
  key: encryption_key,
1320
- user_project: user_project
1557
+ user_project: user_project,
1558
+ soft_deleted: soft_deleted
1321
1559
  File.from_gapi gapi, service, user_project: user_project
1322
1560
  rescue Google::Cloud::NotFoundError
1323
1561
  nil
@@ -1598,6 +1836,77 @@ module Google
1598
1836
  alias upload_file create_file
1599
1837
  alias new_file create_file
1600
1838
 
1839
+ ##
1840
+ # Restores a soft-deleted object.
1841
+ #
1842
+ # @param [String] file_path
1843
+ # Name of the file.
1844
+ # @param [Fixnum] generation
1845
+ # Selects a specific revision of this object.
1846
+ # @param [Boolean] copy_source_acl
1847
+ # If true, copies the source file's ACL; otherwise, uses the
1848
+ # bucket's default file ACL. The default is false.
1849
+ # @param [Fixnum] if_generation_match
1850
+ # Makes the operation conditional on whether the file's one live
1851
+ # generation matches the given value. Setting to 0 makes the
1852
+ # operation succeed only if there are no live versions of the file.
1853
+ # @param [Fixnum] if_generation_not_match
1854
+ # Makes the operation conditional on whether none of the file's live
1855
+ # generations match the given value. If no live file exists, the
1856
+ # precondition fails. Setting to 0 makes the operation succeed only
1857
+ # if there is a live version of the file.
1858
+ # @param [Fixnum] if_metageneration_match
1859
+ # Makes the operation conditional on whether the file's one live
1860
+ # metageneration matches the given value.
1861
+ # @param [Fixnum] if_metageneration_not_match
1862
+ # Makes the operation conditional on whether none of the object's
1863
+ # live metagenerations match the given value.
1864
+ # @param [String] projection
1865
+ # Set of properties to return. Defaults to full.
1866
+ # @param [String] user_project
1867
+ # The project to be billed for this request. Required for Requester
1868
+ # Pays buckets.
1869
+ # @param [String] fields
1870
+ # Selector specifying which fields to include in a partial response.
1871
+ #
1872
+ # @return [Google::Cloud::Storage::File]
1873
+ #
1874
+ # @example
1875
+ # require "google/cloud/storage"
1876
+ #
1877
+ # storage = Google::Cloud::Storage.new
1878
+ #
1879
+ # bucket = storage.bucket "my-bucket"
1880
+ #
1881
+ # bucket.restore_file "path/of/file", <generation-of-the-file>
1882
+ #
1883
+ def restore_file file_path,
1884
+ generation,
1885
+ copy_source_acl: nil,
1886
+ if_generation_match: nil,
1887
+ if_generation_not_match: nil,
1888
+ if_metageneration_match: nil,
1889
+ if_metageneration_not_match: nil,
1890
+ projection: nil,
1891
+ user_project: nil,
1892
+ fields: nil,
1893
+ options: {}
1894
+ ensure_service!
1895
+ gapi = service.restore_file name,
1896
+ file_path,
1897
+ generation,
1898
+ copy_source_acl: File::Acl.predefined_rule_for(copy_source_acl),
1899
+ if_generation_match: if_generation_match,
1900
+ if_generation_not_match: if_generation_not_match,
1901
+ if_metageneration_match: if_metageneration_match,
1902
+ if_metageneration_not_match: if_metageneration_not_match,
1903
+ projection: projection,
1904
+ user_project: user_project,
1905
+ fields: fields,
1906
+ options: options
1907
+ File.from_gapi gapi, service, user_project: user_project
1908
+ end
1909
+
1601
1910
  ##
1602
1911
  # Concatenates a list of existing files in the bucket into a new file in
1603
1912
  # the bucket. There is a limit (currently 32) to the number of files
@@ -2878,9 +3187,9 @@ module Google
2878
3187
  attributes.flatten!
2879
3188
  return if attributes.empty?
2880
3189
  ensure_service!
2881
- patch_args = Hash[attributes.map do |attr|
3190
+ patch_args = attributes.to_h do |attr|
2882
3191
  [attr, @gapi.send(attr)]
2883
- end]
3192
+ end
2884
3193
  patch_gapi = API::Bucket.new(**patch_args)
2885
3194
  @gapi = service.patch_bucket name,
2886
3195
  patch_gapi,
@@ -2891,6 +3200,26 @@ module Google
2891
3200
  self
2892
3201
  end
2893
3202
 
3203
+ def update_gapi! attributes,
3204
+ if_metageneration_match: nil,
3205
+ if_metageneration_not_match: nil
3206
+ attributes = Array(attributes)
3207
+ attributes.flatten!
3208
+ return if attributes.empty?
3209
+ ensure_service!
3210
+ update_args = attributes.to_h do |attr|
3211
+ [attr, @gapi.send(attr)]
3212
+ end
3213
+ update_gapi = API::Bucket.new(**update_args)
3214
+ @gapi = service.update_bucket name,
3215
+ update_gapi,
3216
+ if_metageneration_match: if_metageneration_match,
3217
+ if_metageneration_not_match: if_metageneration_not_match,
3218
+ user_project: user_project
3219
+ @lazy = nil
3220
+ self
3221
+ end
3222
+
2894
3223
  ##
2895
3224
  # Raise an error if the file is not found.
2896
3225
  def ensure_io_or_file_exists! file
@@ -83,10 +83,12 @@ module Google
83
83
  token: @token,
84
84
  max: @max,
85
85
  versions: @versions,
86
- user_project: @user_project
86
+ user_project: @user_project,
87
+ match_glob: @match_glob
87
88
  File::List.from_gapi gapi, @service, @bucket, @prefix,
88
89
  @delimiter, @max, @versions,
89
- user_project: @user_project
90
+ user_project: @user_project,
91
+ match_glob: @match_glob
90
92
  end
91
93
 
92
94
  ##
@@ -163,7 +165,9 @@ module Google
163
165
  # Google::Apis::StorageV1::Objects object.
164
166
  def self.from_gapi gapi_list, service, bucket = nil, prefix = nil,
165
167
  delimiter = nil, max = nil, versions = nil,
166
- user_project: nil
168
+ user_project: nil, match_glob: nil,
169
+ include_folders_as_prefixes: nil,
170
+ soft_deleted: nil
167
171
  files = new(Array(gapi_list.items).map do |gapi_object|
168
172
  File.from_gapi gapi_object, service, user_project: user_project
169
173
  end)
@@ -176,6 +180,9 @@ module Google
176
180
  files.instance_variable_set :@max, max
177
181
  files.instance_variable_set :@versions, versions
178
182
  files.instance_variable_set :@user_project, user_project
183
+ files.instance_variable_set :@match_glob, match_glob
184
+ files.instance_variable_set :@include_folders_as_prefixes, include_folders_as_prefixes
185
+ files.instance_variable_set :@soft_deleted, soft_deleted
179
186
  files
180
187
  end
181
188
 
@@ -60,7 +60,8 @@ module Google
60
60
  ##
61
61
  # The external url to the file.
62
62
  def ext_url
63
- "#{GOOGLEAPIS_URL}#{ext_path}"
63
+ root_url = @service.service.root_url.chomp "/"
64
+ "#{root_url}#{ext_path}"
64
65
  end
65
66
 
66
67
  def apply_option_defaults options
@@ -91,7 +92,7 @@ module Google
91
92
 
92
93
  def error_msg attr_name
93
94
  "Service account credentials '#{attr_name}' is missing. To generate service account credentials " \
94
- "see https://cloud.google.com/iam/docs/service-accounts"
95
+ "see https://cloud.google.com/iam/docs/service-accounts"
95
96
  end
96
97
 
97
98
  def post_object options
@@ -144,8 +145,8 @@ module Google
144
145
 
145
146
  def generate_signed_url issuer, signed_string, expires, query
146
147
  url = "#{ext_url}?GoogleAccessId=#{url_escape issuer}" \
147
- "&Expires=#{expires}" \
148
- "&Signature=#{url_escape signed_string}"
148
+ "&Expires=#{expires}" \
149
+ "&Signature=#{url_escape signed_string}"
149
150
 
150
151
  query&.each do |name, value|
151
152
  url << "&#{url_escape name}=#{url_escape value}"
@@ -131,7 +131,7 @@ module Google
131
131
  # methods below are public visibility only for unit testing
132
132
  # rubocop:disable Style/StringLiterals
133
133
  def escape_characters str
134
- str.split("").map do |s|
134
+ str.chars.map do |s|
135
135
  if s.ascii_only?
136
136
  case s
137
137
  when "\\"
@@ -207,7 +207,7 @@ module Google
207
207
 
208
208
  def error_msg attr_name
209
209
  "Service account credentials '#{attr_name}' is missing. To generate service account credentials " \
210
- "see https://cloud.google.com/iam/docs/service-accounts"
210
+ "see https://cloud.google.com/iam/docs/service-accounts"
211
211
  end
212
212
 
213
213
  def service_account_signer signer
@@ -243,7 +243,7 @@ module Google
243
243
  headers_arr = canonical_headers.map do |k, v|
244
244
  [k.downcase, v.strip.gsub(/[^\S\t]+/, " ").gsub(/\t+/, " ")]
245
245
  end
246
- canonical_headers = Hash[headers_arr]
246
+ canonical_headers = headers_arr.to_h
247
247
  canonical_headers["host"] = host_name virtual_hosted_style, bucket_bound_hostname
248
248
 
249
249
  canonical_headers = canonical_headers.sort_by(&:first).to_h
@@ -295,13 +295,13 @@ module Google
295
295
  ##
296
296
  # The external path to the bucket, with trailing slash.
297
297
  def bucket_path path_style
298
- return "/#{@bucket_name}/" if path_style
298
+ "/#{@bucket_name}/" if path_style
299
299
  end
300
300
 
301
301
  ##
302
302
  # The external url to the file.
303
303
  def ext_url scheme, virtual_hosted_style, bucket_bound_hostname
304
- url = GOOGLEAPIS_URL.dup
304
+ url = @service.service.root_url.chomp "/"
305
305
  if virtual_hosted_style
306
306
  parts = url.split "//"
307
307
  parts[1] = "#{@bucket_name}.#{parts[1]}"