-
Notifications
You must be signed in to change notification settings - Fork 556
/
Copy pathbucket.rb
2353 lines (2263 loc) · 95.3 KB
/
bucket.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2014 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "google/cloud/storage/bucket/acl"
require "google/cloud/storage/bucket/list"
require "google/cloud/storage/bucket/cors"
require "google/cloud/storage/bucket/lifecycle"
require "google/cloud/storage/convert"
require "google/cloud/storage/file"
require "google/cloud/storage/notification"
require "google/cloud/storage/policy"
require "google/cloud/storage/post_object"
require "pathname"
module Google
module Cloud
module Storage
##
# # Bucket
#
# Represents a Storage bucket. Belongs to a Project and has many Files.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
# file = bucket.file "path/to/my-file.ext"
#
class Bucket
include Convert
##
# @private Alias to the Google Client API module
API = Google::Apis::StorageV1
##
# @private The Service object.
attr_accessor :service
##
# @private The Google API Client object.
attr_accessor :gapi
##
# A boolean value or a project ID string to indicate the project to
# be billed for operations on the bucket and its files. If this
# attribute is set to `true`, transit costs for operations on the bucket
# will be billed to the current project for this client. (See
# {Project#project} for the ID of the current project.) If this
# attribute is set to a project ID, and that project is authorized for
# the currently authenticated service account, transit costs will be
# billed to that project. This attribute is required with requester
# pays-enabled buckets. The default is `nil`.
#
# In general, this attribute should be set when first retrieving the
# bucket by providing the `user_project` option to {Project#bucket}.
#
# See also {#requester_pays=} and {#requester_pays}.
#
# @example Setting a non-default project:
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "other-project-bucket", user_project: true
# files = bucket.files # Billed to current project
# bucket.user_project = "my-other-project"
# files = bucket.files # Billed to "my-other-project"
#
attr_accessor :user_project
##
# @private Create an empty Bucket object.
def initialize
@service = nil
@gapi = API::Bucket.new
@user_project = nil
end
##
# The kind of item this is.
# For buckets, this is always `storage#bucket`.
#
# @return [String]
#
def kind
@gapi.kind
end
##
# The ID of the bucket.
#
# @return [String]
#
def id
@gapi.id
end
##
# The name of the bucket.
#
# @return [String]
#
def name
@gapi.name
end
##
# A URL that can be used to access the bucket using the REST API.
#
# @return [String]
#
def api_url
@gapi.self_link
end
##
# Creation time of the bucket.
#
# @return [DateTime]
#
def created_at
@gapi.time_created
end
##
# The metadata generation of the bucket.
#
# @return [Integer] The metageneration.
#
def metageneration
@gapi.metageneration
end
##
# Returns the current CORS configuration for a static website served
# from the bucket.
#
# The return value is a frozen (unmodifiable) array of hashes containing
# the attributes specified for the Bucket resource field
# [cors](https://cloud.google.com/storage/docs/json_api/v1/buckets#cors).
#
# This method also accepts a block for updating the bucket's CORS rules.
# See {Bucket::Cors} for details.
#
# @see https://cloud.google.com/storage/docs/cross-origin Cross-Origin
# Resource Sharing (CORS)
#
# @yield [cors] a block for setting CORS rules
# @yieldparam [Bucket::Cors] cors the object accepting CORS rules
#
# @return [Bucket::Cors] The frozen builder object.
#
# @example Retrieving the bucket's CORS rules.
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-todo-app"
# bucket.cors.size #=> 2
# rule = bucket.cors.first
# rule.origin #=> ["http://example.org"]
# rule.methods #=> ["GET","POST","DELETE"]
# rule.headers #=> ["X-My-Custom-Header"]
# rule.max_age #=> 3600
#
# @example Updating the bucket's CORS rules inside a block.
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
# bucket = storage.bucket "my-todo-app"
#
# bucket.update do |b|
# b.cors do |c|
# c.add_rule ["http://example.org", "https://example.org"],
# "*",
# headers: ["X-My-Custom-Header"],
# max_age: 3600
# end
# end
#
def cors
cors_builder = Bucket::Cors.from_gapi @gapi.cors_configurations
if block_given?
yield cors_builder
if cors_builder.changed?
@gapi.cors_configurations = cors_builder.to_gapi
patch_gapi! :cors_configurations
end
end
cors_builder.freeze # always return frozen objects
end
##
# Returns the current Object Lifecycle Management rules configuration
# for the bucket.
#
# This method also accepts a block for updating the bucket's Object
# Lifecycle Management rules. See {Bucket::Lifecycle} for details.
#
# @see https://cloud.google.com/storage/docs/lifecycle Object
# Lifecycle Management
# @see https://cloud.google.com/storage/docs/managing-lifecycles
# Managing Object Lifecycles
#
# @yield [lifecycle] a block for setting Object Lifecycle Management
# rules
# @yieldparam [Bucket::Lifecycle] lifecycle the object accepting Object
# Lifecycle Management rules
#
# @return [Bucket::Lifecycle] The frozen builder object.
#
# @example Retrieving a bucket's lifecycle management rules.
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
# bucket = storage.bucket "my-bucket"
#
# bucket.lifecycle.size #=> 2
# rule = bucket.lifecycle.first
# rule.action #=> "SetStorageClass"
# rule.storage_class #=> "COLDLINE"
# rule.age #=> 10
# rule.matches_storage_class #=> ["STANDARD", "NEARLINE"]
#
# @example Updating the bucket's lifecycle management rules in a block.
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
# bucket = storage.bucket "my-bucket"
#
# bucket.update do |b|
# b.lifecycle do |l|
# # Remove the last rule from the array
# l.pop
# # Remove rules with the given condition
# l.delete_if do |r|
# r.matches_storage_class.include? "NEARLINE"
# end
# # Update rules
# l.each do |r|
# r.age = 90 if r.action == "Delete"
# end
# # Add a rule
# l.add_set_storage_class_rule "COLDLINE", age: 10
# end
# end
#
def lifecycle
lifecycle_builder = Bucket::Lifecycle.from_gapi @gapi.lifecycle
if block_given?
yield lifecycle_builder
if lifecycle_builder.changed?
@gapi.lifecycle = lifecycle_builder.to_gapi
patch_gapi! :lifecycle
end
end
lifecycle_builder.freeze # always return frozen objects
end
##
# The location of the bucket.
# Object data for objects in the bucket resides in physical
# storage within this region. Defaults to US.
# See the developer's guide for the authoritative list.
#
# @return [String]
#
# @see https://cloud.google.com/storage/docs/concepts-techniques
#
def location
@gapi.location
end
##
# The bucket's location type. Location type defines the geographic
# placement of the bucket's data and affects cost, performance, and
# availability. There are three possible values:
#
# * `region` - Lowest latency within a single region
# * `multi-region` - Highest availability across largest area
# * `dual-region` - High availability and low latency across 2 regions
#
# @return [String] The location type code: "region", "multi-region", or
# "dual-region"
#
def location_type
@gapi.location_type
end
##
# The destination bucket name for the bucket's logs.
#
# @return [String]
#
# @see https://cloud.google.com/storage/docs/access-logs Access Logs
#
def logging_bucket
@gapi.logging.log_bucket if @gapi.logging
end
##
# Updates the destination bucket for the bucket's logs.
#
# @see https://cloud.google.com/storage/docs/access-logs Access Logs
#
# @param [String] logging_bucket The bucket to hold the logging output
#
def logging_bucket= logging_bucket
@gapi.logging ||= API::Bucket::Logging.new
@gapi.logging.log_bucket = logging_bucket
patch_gapi! :logging
end
##
# The logging object prefix for the bucket's logs. For more information,
#
# @see https://cloud.google.com/storage/docs/access-logs Access Logs
#
# @return [String]
#
def logging_prefix
@gapi.logging.log_object_prefix if @gapi.logging
end
##
# Updates the logging object prefix. This prefix will be used to create
# log object names for the bucket. It can be at most 900 characters and
# must be a [valid object
# name](https://cloud.google.com/storage/docs/bucket-naming#objectnames).
# By default, the object prefix is the name of the bucket for which the
# logs are enabled.
#
# @see https://cloud.google.com/storage/docs/access-logs Access Logs
#
# @param [String] logging_prefix The logging object prefix.
#
def logging_prefix= logging_prefix
@gapi.logging ||= API::Bucket::Logging.new
@gapi.logging.log_object_prefix = logging_prefix
patch_gapi! :logging
end
##
# The bucket's storage class. This defines how objects in the bucket are
# stored and determines the SLA and the cost of storage. Values include
# `STANDARD`, `NEARLINE`, `COLDLINE`, and `ARCHIVE`. `REGIONAL`,`MULTI_REGIONAL`,
# and `DURABLE_REDUCED_AVAILABILITY` are supported as legacy storage
# classes.
#
# @return [String]
#
def storage_class
@gapi.storage_class
end
##
# Updates the bucket's storage class. This defines how objects in the
# bucket are stored and determines the SLA and the cost of storage.
# Accepted values include `:standard`, `:nearline`, `:coldline`, and
# `:archive`, as well as the equivalent strings returned by
# {Bucket#storage_class}. `:multi_regional`, `:regional`, and
# `durable_reduced_availability` are accepted as legacy storage classes.
# For more information, see [Storage
# Classes](https://cloud.google.com/storage/docs/storage-classes).
#
# @param [Symbol, String] new_storage_class Storage class of the bucket.
#
def storage_class= new_storage_class
@gapi.storage_class = storage_class_for new_storage_class
patch_gapi! :storage_class
end
##
# Whether [Object
# Versioning](https://cloud.google.com/storage/docs/object-versioning)
# is enabled for the bucket.
#
# @return [Boolean]
#
def versioning?
@gapi.versioning.enabled? unless @gapi.versioning.nil?
end
##
# Updates whether [Object
# Versioning](https://cloud.google.com/storage/docs/object-versioning)
# is enabled for the bucket.
#
# @param [Boolean] new_versioning true if versioning is to be enabled
# for the bucket.
#
def versioning= new_versioning
@gapi.versioning ||= API::Bucket::Versioning.new
@gapi.versioning.enabled = new_versioning
patch_gapi! :versioning
end
##
# The main page suffix for a static website. If the requested object
# path is missing, the service will ensure the path has a trailing '/',
# append this suffix, and attempt to retrieve the resulting object. This
# allows the creation of index.html objects to represent directory
# pages.
#
# @see https://cloud.google.com/storage/docs/website-configuration#step4
# How to Host a Static Website
#
# @return [String] The main page suffix.
#
def website_main
@gapi.website.main_page_suffix if @gapi.website
end
##
# Updates the main page suffix for a static website.
#
# @see https://cloud.google.com/storage/docs/website-configuration#step4
# How to Host a Static Website
#
# @param [String] website_main The main page suffix.
#
def website_main= website_main
@gapi.website ||= API::Bucket::Website.new
@gapi.website.main_page_suffix = website_main
patch_gapi! :website
end
##
# The page returned from a static website served from the bucket when a
# site visitor requests a resource that does not exist.
#
# @see https://cloud.google.com/storage/docs/website-configuration#step4
# How to Host a Static Website
#
# @return [String]
#
def website_404
@gapi.website.not_found_page if @gapi.website
end
##
# A hash of user-provided labels. The hash is frozen and changes are not
# allowed.
#
# @return [Hash(String => String)]
#
def labels
m = @gapi.labels
m = m.to_h if m.respond_to? :to_h
m.dup.freeze
end
##
# Updates the hash of user-provided labels.
#
# @param [Hash(String => String)] labels The user-provided labels.
#
def labels= labels
@gapi.labels = labels
patch_gapi! :labels
end
##
# Updates the page returned from a static website served from the bucket
# when a site visitor requests a resource that does not exist.
#
# @see https://cloud.google.com/storage/docs/website-configuration#step4
# How to Host a Static Website
#
def website_404= website_404
@gapi.website ||= API::Bucket::Website.new
@gapi.website.not_found_page = website_404
patch_gapi! :website
end
##
# Indicates that a client accessing the bucket or a file it contains
# must assume the transit costs related to the access. The requester
# must pass the `user_project` option to {Project#bucket} and
# {Project#buckets} to indicate the project to which the access costs
# should be billed.
#
# @return [Boolean, nil] Returns `true` if requester pays is enabled for
# the bucket.
#
def requester_pays
@gapi.billing.requester_pays if @gapi.billing
end
alias requester_pays? requester_pays
##
# Enables requester pays for the bucket. If enabled, a client accessing
# the bucket or a file it contains must assume the transit costs related
# to the access. The requester must pass the `user_project` option to
# {Project#bucket} and {Project#buckets} to indicate the project to
# which the access costs should be billed.
#
# @param [Boolean] new_requester_pays When set to `true`, requester pays
# is enabled for the bucket.
#
# @example Enable requester pays for a bucket:
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# bucket.requester_pays = true # API call
# # Other projects must now provide `user_project` option when calling
# # Project#bucket or Project#buckets to access this bucket.
#
def requester_pays= new_requester_pays
@gapi.billing ||= API::Bucket::Billing.new
@gapi.billing.requester_pays = new_requester_pays
patch_gapi! :billing
end
##
# The Cloud KMS encryption key that will be used to protect files.
# For example: `projects/a/locations/b/keyRings/c/cryptoKeys/d`
#
# @return [String, nil] A Cloud KMS encryption key, or `nil` if none
# has been configured.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# # KMS key ring must use the same location as the bucket.
# kms_key_name = "projects/a/locations/b/keyRings/c/cryptoKeys/d"
# bucket.default_kms_key = kms_key_name
#
# bucket.default_kms_key #=> kms_key_name
#
def default_kms_key
@gapi.encryption && @gapi.encryption.default_kms_key_name
end
##
# Set the Cloud KMS encryption key that will be used to protect files.
# For example: `projects/a/locations/b/keyRings/c/cryptoKeys/d`
#
# @param [String] new_default_kms_key New Cloud KMS key name.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# # KMS key ring must use the same location as the bucket.
# kms_key_name = "projects/a/locations/b/keyRings/c/cryptoKeys/d"
#
# bucket.default_kms_key = kms_key_name
#
def default_kms_key= new_default_kms_key
@gapi.encryption = API::Bucket::Encryption.new \
default_kms_key_name: new_default_kms_key
patch_gapi! :encryption
end
##
# The period of time (in seconds) that files in the bucket must be
# retained, and cannot be deleted, overwritten, or archived.
# The value must be between 0 and 100 years (in seconds.)
#
# See also: {#retention_period=}, {#retention_effective_at}, and
# {#retention_policy_locked?}.
#
# @return [Integer, nil] The retention period defined in seconds, if a
# retention policy exists for the bucket.
#
def retention_period
@gapi.retention_policy && @gapi.retention_policy.retention_period
end
##
# The period of time (in seconds) that files in the bucket must be
# retained, and cannot be deleted, overwritten, or archived. Passing a
# valid Integer value will add a new retention policy to the bucket
# if none exists. Passing `nil` will remove the retention policy from
# the bucket if it exists, unless the policy is locked.
#
# Locked policies can be extended in duration by using this method
# to set a higher value. Such an extension is permanent, and it cannot
# later be reduced. The extended duration will apply retroactively to
# all files currently in the bucket.
#
# See also: {#lock_retention_policy!}, {#retention_period},
# {#retention_effective_at}, and {#retention_policy_locked?}.
#
# @param [Integer, nil] new_retention_period The retention period
# defined in seconds. The value must be between 0 and 100 years (in
# seconds), or `nil`.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# bucket.retention_period = 2592000 # 30 days in seconds
#
# file = bucket.create_file "path/to/local.file.ext"
# file.delete # raises Google::Cloud::PermissionDeniedError
#
def retention_period= new_retention_period
if new_retention_period.nil?
@gapi.retention_policy = nil
else
@gapi.retention_policy ||= API::Bucket::RetentionPolicy.new
@gapi.retention_policy.retention_period = new_retention_period
end
patch_gapi! :retention_policy
end
##
# The time from which the retention policy was effective. Whenever a
# retention policy is created or extended, GCS updates the effective
# date of the policy. The effective date signals the date starting from
# which objects were guaranteed to be retained for the full duration of
# the policy.
#
# This field is updated when the retention policy is created or
# modified, including extension of a locked policy.
#
# @return [DateTime, nil] The effective date of the bucket's retention
# policy, if a policy exists.
#
def retention_effective_at
@gapi.retention_policy && @gapi.retention_policy.effective_time
end
##
# Whether the bucket's file retention policy is locked and its retention
# period cannot be reduced. See {#retention_period=} and
# {#lock_retention_policy!}.
#
# This value can only be set to `true` by calling
# {Bucket#lock_retention_policy!}.
#
# @return [Boolean] Returns `false` if there is no retention policy or
# if the retention policy is unlocked and the retention period can be
# reduced. Returns `true` if the retention policy is locked and the
# retention period cannot be reduced.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# bucket.retention_period = 2592000 # 30 days in seconds
# bucket.lock_retention_policy!
# bucket.retention_policy_locked? # true
#
# file = bucket.create_file "path/to/local.file.ext"
# file.delete # raises Google::Cloud::PermissionDeniedError
#
def retention_policy_locked?
return false unless @gapi.retention_policy
[email protected]_policy.is_locked.nil? &&
@gapi.retention_policy.is_locked
end
##
# Whether the `event_based_hold` field for newly-created files in the
# bucket will be initially set to `true`. See
# {#default_event_based_hold=}, {File#event_based_hold?} and
# {File#set_event_based_hold!}.
#
# @return [Boolean] Returns `true` if the `event_based_hold` field for
# newly-created files in the bucket will be initially set to `true`,
# otherwise `false`.
#
def default_event_based_hold?
[email protected]_event_based_hold.nil? && @gapi.default_event_based_hold
end
##
# Updates the default event-based hold field for the bucket. This field
# controls the initial state of the `event_based_hold` field for
# newly-created files in the bucket.
#
# See {File#event_based_hold?} and {File#set_event_based_hold!}.
#
# @param [Boolean] new_default_event_based_hold The default event-based
# hold field for the bucket.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# bucket.update do |b|
# b.retention_period = 2592000 # 30 days in seconds
# b.default_event_based_hold = true
# end
#
# file = bucket.create_file "path/to/local.file.ext"
# file.event_based_hold? # true
# file.delete # raises Google::Cloud::PermissionDeniedError
# file.release_event_based_hold!
#
# # The end of the retention period is calculated from the time that
# # the event-based hold was released.
# file.retention_expires_at
#
def default_event_based_hold= new_default_event_based_hold
@gapi.default_event_based_hold = new_default_event_based_hold
patch_gapi! :default_event_based_hold
end
##
# PERMANENTLY locks the retention policy (see {#retention_period=}) on
# the bucket if one exists. The policy is transitioned to a locked state
# in which its duration cannot be reduced.
#
# Locked policies can be extended in duration by setting
# {#retention_period=} to a higher value. Such an extension is
# permanent, and it cannot later be reduced. The extended duration will
# apply retroactively to all files currently in the bucket.
#
# This method also [creates a
# lien](https://cloud.google.com/resource-manager/reference/rest/v1/liens/create)
# on the `resourcemanager.projects.delete` permission for the project
# containing the bucket.
#
# The bucket's metageneration value is required for the lock policy API
# call. Attempting to call this method on a bucket that was loaded with
# the `skip_lookup: true` option will result in an error.
#
# @return [Boolean] Returns `true` if the lock operation is successful.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# bucket.retention_period = 2592000 # 30 days in seconds
# bucket.lock_retention_policy!
# bucket.retention_policy_locked? # true
#
# file = bucket.create_file "path/to/local.file.ext"
# file.delete # raises Google::Cloud::PermissionDeniedError
#
# # Locked policies can be extended in duration
# bucket.retention_period = 7776000 # 90 days in seconds
#
def lock_retention_policy!
ensure_service!
@gapi = service.lock_bucket_retention_policy \
name, metageneration, user_project: user_project
true
end
##
# Whether the bucket's file IAM configuration enables uniform bucket-level access. The default is false. This
# value can be modified by calling {Bucket#uniform_bucket_level_access=}.
#
# @return [Boolean] Returns `false` if the bucket has no IAM configuration or if uniform bucket-level access is
# not enabled in the IAM configuration. Returns `true` if uniform bucket-level access is enabled in the IAM
# configuration.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# bucket.uniform_bucket_level_access = true
# bucket.uniform_bucket_level_access? # true
#
def uniform_bucket_level_access?
return false unless @gapi.iam_configuration && @gapi.iam_configuration.uniform_bucket_level_access
[email protected]_configuration.uniform_bucket_level_access.enabled.nil? &&
@gapi.iam_configuration.uniform_bucket_level_access.enabled
end
##
# Sets whether uniform bucket-level access is enabled for this bucket. When this is enabled, access to the
# bucket will be configured through IAM, and legacy ACL policies will not work. When it is first enabled,
# {#uniform_bucket_level_access_locked_at} will be set by the API automatically. The uniform bucket-level access
# can then be disabled until the time specified, after which it will become immutable and calls to change it
# will fail. If uniform bucket-level access is enabled, calls to access legacy ACL information will fail.
#
# Before enabling uniform bucket-level access please review [uniform bucket-level
# access](https://cloud.google.com/storage/docs/uniform-bucket-level-access).
#
# @param [Boolean] new_uniform_bucket_level_access When set to `true`, uniform bucket-level access is enabled in
# the bucket's IAM configuration.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# bucket.uniform_bucket_level_access = true
# bucket.uniform_bucket_level_access? # true
#
# bucket.default_acl.public! # Google::Cloud::InvalidArgumentError
#
# # The deadline for disabling uniform bucket-level access.
# puts bucket.uniform_bucket_level_access_locked_at
#
def uniform_bucket_level_access= new_uniform_bucket_level_access
@gapi.iam_configuration ||= API::Bucket::IamConfiguration.new
@gapi.iam_configuration.uniform_bucket_level_access ||= \
API::Bucket::IamConfiguration::UniformBucketLevelAccess.new
@gapi.iam_configuration.uniform_bucket_level_access.enabled = new_uniform_bucket_level_access
patch_gapi! :iam_configuration
end
##
# The deadline time for disabling uniform bucket-level access by calling {Bucket#uniform_bucket_level_access=}.
# After the locked time the uniform bucket-level access setting cannot be changed from true to false.
# Corresponds to the property `locked_time`.
#
# @return [DateTime, nil] The deadline time for changing {Bucket#uniform_bucket_level_access=} from true to
# false, or `nil` if {Bucket#uniform_bucket_level_access?} is false.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# bucket.uniform_bucket_level_access = true
#
# # The deadline for disabling uniform bucket-level access.
# puts bucket.uniform_bucket_level_access_locked_at
#
def uniform_bucket_level_access_locked_at
return nil unless @gapi.iam_configuration && @gapi.iam_configuration.uniform_bucket_level_access
@gapi.iam_configuration.uniform_bucket_level_access.locked_time
end
##
# @deprecated Use {#uniform_bucket_level_access?} instead.
#
def policy_only?
uniform_bucket_level_access?
end
##
# @deprecated Use {#uniform_bucket_level_access=} instead.
#
def policy_only= new_policy_only
self.uniform_bucket_level_access = new_policy_only
end
##
# @deprecated Use {#uniform_bucket_level_access_locked_at} instead.
#
def policy_only_locked_at
uniform_bucket_level_access_locked_at
end
##
# Updates the bucket with changes made in the given block in a single
# PATCH request. The following attributes may be set: {#cors},
# {#logging_bucket=}, {#logging_prefix=}, {#versioning=},
# {#website_main=}, {#website_404=}, and {#requester_pays=}.
#
# In addition, the #cors configuration accessible in the block is
# completely mutable and will be included in the request. (See
# {Bucket::Cors})
#
# @yield [bucket] a block yielding a delegate object for updating the
# file
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-todo-app"
# bucket.update do |b|
# b.website_main = "index.html"
# b.website_404 = "not_found.html"
# b.cors[0].methods = ["GET","POST","DELETE"]
# b.cors[1].headers << "X-Another-Custom-Header"
# end
#
# @example New CORS rules can also be added in a nested block:
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
# bucket = storage.bucket "my-todo-app"
#
# bucket.update do |b|
# b.cors do |c|
# c.add_rule ["http://example.org", "https://example.org"],
# "*",
# headers: ["X-My-Custom-Header"],
# max_age: 300
# end
# end
#
def update
updater = Updater.new @gapi
yield updater
# Add check for mutable cors
updater.check_for_changed_labels!
updater.check_for_mutable_cors!
updater.check_for_mutable_lifecycle!
patch_gapi! updater.updates unless updater.updates.empty?
end
##
# Permanently deletes the bucket.
# The bucket must be empty before it can be deleted.
#
# The API call to delete the bucket may be retried under certain
# conditions. See {Google::Cloud#storage} to control this behavior.
#
# @return [Boolean] Returns `true` if the bucket was deleted.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
# bucket.delete
#
def delete
ensure_service!
service.delete_bucket name, user_project: user_project
true
end
##
# Retrieves a list of files matching the criteria.
#
# @param [String] prefix Filter results to files whose names begin with
# this prefix.
# @param [String] delimiter Returns results in a directory-like mode.
# `items` will contain only objects whose names, aside from the
# `prefix`, do not contain `delimiter`. Objects whose names, aside
# from the `prefix`, contain `delimiter` will have their name,
# truncated after the `delimiter`, returned in `prefixes`. Duplicate
# `prefixes` are omitted.
# @param [String] token A previously-returned page token representing
# part of the larger set of results to view.
# @param [Integer] max Maximum number of items plus prefixes to return.
# As duplicate prefixes are omitted, fewer total results may be
# returned than requested. The default value of this parameter is
# 1,000 items.
# @param [Boolean] versions If `true`, lists all versions of an object
# as distinct results. The default is `false`. For more information,
# see [Object Versioning
# ](https://cloud.google.com/storage/docs/object-versioning).
#
# @return [Array<Google::Cloud::Storage::File>] (See
# {Google::Cloud::Storage::File::List})
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
# files = bucket.files
# files.each do |file|
# puts file.name
# end
#
# @example Retrieve all files: (See {File::List#all})
# require "google/cloud/storage"
#