forked from googleapis/google-cloud-ruby
/
file.rb
2025 lines (1938 loc) · 79.8 KB
/
file.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/convert"
require "google/cloud/storage/file/acl"
require "google/cloud/storage/file/list"
require "google/cloud/storage/file/verifier"
require "google/cloud/storage/file/signer_v2"
require "google/cloud/storage/file/signer_v4"
require "zlib"
module Google
module Cloud
module Storage
GOOGLEAPIS_URL = "https://storage.googleapis.com".freeze
##
# # File
#
# Represents a File
# ([Object](https://cloud.google.com/storage/docs/json_api/v1/objects))
# that belongs to a {Bucket}. Files (Objects) are the individual pieces of
# data that you store in Google Cloud Storage. A file can be up to 5 TB in
# size. Files have two components: data and metadata. The data component
# is the data from an external file or other data source that you want to
# store in Google Cloud Storage. The metadata component is a collection of
# name-value pairs that describe various qualities of the data.
#
# @see https://cloud.google.com/storage/docs/concepts-techniques Concepts
# and Techniques
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# file = bucket.file "path/to/my-file.ext"
# file.download "path/to/downloaded/file.ext"
#
# @example Download a public file with an unauthenticated client:
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.anonymous
#
# bucket = storage.bucket "public-bucket", skip_lookup: true
# file = bucket.file "path/to/public-file.ext", skip_lookup: true
#
# downloaded = file.download
# downloaded.rewind
# downloaded.read #=> "Hello world!"
#
class File
include Convert
##
# @private The Connection object.
attr_accessor :service
##
# If this attribute is set to `true`, transit costs for operations on
# the file 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
# owning bucket by providing the `user_project` option to
# {Project#bucket} or {Project#buckets}.
#
# See also {Bucket#requester_pays=} and {Bucket#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
# file = bucket.file "path/to/file.ext" # Billed to current project
# file.user_project = "my-other-project"
# file.download "file.ext" # Billed to "my-other-project"
#
attr_accessor :user_project
##
# @private The Google API Client object.
attr_accessor :gapi
##
# @private Create an empty File object.
def initialize
@service = nil
@gapi = Google::Apis::StorageV1::Object.new
@user_project = nil
end
##
# The kind of item this is.
# For files, this is always storage#object.
#
# @return [String]
#
def kind
@gapi.kind
end
##
# The ID of the file.
#
# @return [String]
#
def id
@gapi.id
end
##
# The name of this file.
#
# @return [String]
#
def name
@gapi.name
end
##
# The name of the {Bucket} containing this file.
#
# @return [String]
#
def bucket
@gapi.bucket
end
##
# The content generation of this file.
# Used for object versioning.
#
# @return [Fixnum]
#
def generation
@gapi.generation
end
##
# The version of the metadata for this file at this generation.
# Used for preconditions and for detecting changes in metadata.
# A metageneration number is only meaningful in the context of a
# particular generation of a particular file.
#
# @return [Fixnum]
#
def metageneration
@gapi.metageneration
end
##
# A URL that can be used to access the file using the REST API.
#
# @return [String]
#
def api_url
@gapi.self_link
end
##
# A URL that can be used to download the file using the REST API.
#
# @return [String]
#
def media_url
@gapi.media_link
end
##
# Content-Length of the data in bytes.
#
# @return [Integer]
#
def size
@gapi.size.to_i if @gapi.size
end
##
# Creation time of the file.
#
# @return [DateTime]
#
def created_at
@gapi.time_created
end
##
# The creation or modification time of the file.
# For buckets with versioning enabled, changing an object's
# metadata does not change this property.
#
# @return [DateTime]
#
def updated_at
@gapi.updated
end
##
# MD5 hash of the data; encoded using base64.
#
# @return [String]
#
def md5
@gapi.md5_hash
end
##
# The CRC32c checksum of the data, as described in
# [RFC 4960, Appendix B](http://tools.ietf.org/html/rfc4960#appendix-B).
# Encoded using base64 in big-endian byte order.
#
# @return [String]
#
def crc32c
@gapi.crc32c
end
##
# HTTP 1.1 Entity tag for the file.
#
# @return [String]
#
def etag
@gapi.etag
end
##
# The [Cache-Control](https://tools.ietf.org/html/rfc7234#section-5.2)
# directive for the file data. If omitted, and the file is accessible
# to all anonymous users, the default will be `public, max-age=3600`.
#
# @return [String]
#
def cache_control
@gapi.cache_control
end
##
# Updates the
# [Cache-Control](https://tools.ietf.org/html/rfc7234#section-5.2)
# directive for the file data. If omitted, and the file is accessible
# to all anonymous users, the default will be `public, max-age=3600`.
#
# @param [String] cache_control The Cache-Control directive.
#
def cache_control= cache_control
@gapi.cache_control = cache_control
update_gapi! :cache_control
end
##
# The [Content-Disposition](https://tools.ietf.org/html/rfc6266) of the
# file data.
#
# @return [String]
#
def content_disposition
@gapi.content_disposition
end
##
# Updates the [Content-Disposition](https://tools.ietf.org/html/rfc6266)
# of the file data.
#
# @param [String] content_disposition The Content-Disposition of the
# file.
#
def content_disposition= content_disposition
@gapi.content_disposition = content_disposition
update_gapi! :content_disposition
end
##
# The [Content-Encoding
# ](https://tools.ietf.org/html/rfc7231#section-3.1.2.2) of the file
# data.
#
# @return [String]
#
def content_encoding
@gapi.content_encoding
end
##
# Updates the [Content-Encoding
# ](https://tools.ietf.org/html/rfc7231#section-3.1.2.2) of the file
# data.
#
# @param [String] content_encoding The Content-Encoding of the file.
#
def content_encoding= content_encoding
@gapi.content_encoding = content_encoding
update_gapi! :content_encoding
end
##
# The [Content-Language](http://tools.ietf.org/html/bcp47) of the file
# data.
#
# @return [String]
#
def content_language
@gapi.content_language
end
##
# Updates the [Content-Language](http://tools.ietf.org/html/bcp47) of
# the file data.
#
# @param [String] content_language The Content-Language of the file.
#
def content_language= content_language
@gapi.content_language = content_language
update_gapi! :content_language
end
##
# The [Content-Type](https://tools.ietf.org/html/rfc2616#section-14.17)
# of the file data.
#
# @return [String]
#
def content_type
@gapi.content_type
end
##
# Updates the
# [Content-Type](https://tools.ietf.org/html/rfc2616#section-14.17) of
# the file data.
#
# @param [String] content_type The Content-Type of the file.
#
def content_type= content_type
@gapi.content_type = content_type
update_gapi! :content_type
end
##
# A custom time specified by the user for the file, or `nil`.
#
# @return [DateTime, nil]
#
def custom_time
@gapi.custom_time
end
##
# Updates the custom time specified by the user for the file. Once set,
# custom_time can't be unset, and it can only be changed to a time in the
# future. If custom_time must be unset, you must either perform a rewrite
# operation, or upload the data again and create a new file.
#
# @param [DateTime] custom_time A custom time specified by the user
# for the file.
#
def custom_time= custom_time
@gapi.custom_time = custom_time
update_gapi! :custom_time
end
##
# A hash of custom, user-provided web-safe keys and arbitrary string
# values that will returned with requests for the file as "x-goog-meta-"
# response headers.
#
# @return [Hash(String => String)]
#
def metadata
m = @gapi.metadata
m = m.to_h if m.respond_to? :to_h
m.dup.freeze
end
##
# Updates the hash of custom, user-provided web-safe keys and arbitrary
# string values that will returned with requests for the file as
# "x-goog-meta-" response headers.
#
# @param [Hash(String => String)] metadata The user-provided metadata,
# in key/value pairs.
#
def metadata= metadata
@gapi.metadata = metadata
update_gapi! :metadata
end
##
# An [RFC 4648](https://tools.ietf.org/html/rfc4648#section-4)
# Base64-encoded string of the SHA256 hash of the [customer-supplied
# encryption
# key](https://cloud.google.com/storage/docs/encryption#customer-supplied).
# You can use this SHA256 hash to uniquely identify the AES-256
# encryption key required to decrypt this file.
#
# @return [String, nil] The encoded SHA256 hash, or `nil` if there is
# no customer-supplied encryption key for this file.
#
def encryption_key_sha256
return nil unless @gapi.customer_encryption
Base64.decode64 @gapi.customer_encryption.key_sha256
end
##
# The Cloud KMS encryption key that was used to protect the file, or
# `nil` if none has been configured.
#
# @see https://cloud.google.com/kms/docs/ Cloud Key Management Service
# Documentation
#
# @return [String, nil] A Cloud KMS encryption key, or `nil` if none has
# been configured.
#
def kms_key
@gapi.kms_key_name
end
##
# The file's storage class. This defines how the file is stored and
# determines the SLA and the cost of storage. For more information, see
# [Storage
# Classes](https://cloud.google.com/storage/docs/storage-classes) and
# [Per-Object Storage
# Class](https://cloud.google.com/storage/docs/per-object-storage-class).
#
# @return [String]
#
def storage_class
@gapi.storage_class
end
##
# Rewrites the file with a new storage class, which determines the SLA
# and the cost of storage. Accepted values include:
#
# * `:standard`
# * `:nearline`
# * `:coldline`
# * `:archive`
#
# as well as the equivalent strings returned by {File#storage_class} or
# {Bucket#storage_class}. For more information, see [Storage
# Classes](https://cloud.google.com/storage/docs/storage-classes) and
# [Per-Object Storage
# Class](https://cloud.google.com/storage/docs/per-object-storage-class).
# The default value is the default storage class for the bucket. See
# {Bucket#storage_class}.
#
# @param [Symbol, String] storage_class Storage class of the file.
#
def storage_class= storage_class
@gapi.storage_class = storage_class_for storage_class
update_gapi! :storage_class
end
##
# Whether there is a temporary hold on the file. A temporary hold will
# be enforced on the file as long as this property is `true`, even if
# the bucket-level retention policy would normally allow deletion. When
# the temporary hold is removed, the normal bucket-level policy rules
# once again apply. The default value is `false`.
#
# @return [Boolean] Returns `true` if there is a temporary hold on the
# file, otherwise `false`.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
# file = bucket.file "path/to/my-file.ext"
#
# file.temporary_hold? #=> false
# file.set_temporary_hold!
# file.delete # raises Google::Cloud::PermissionDeniedError
#
def temporary_hold?
!@gapi.temporary_hold.nil? && @gapi.temporary_hold
end
##
# Sets the temporary hold property of the file to `true`. This property
# is used to enforce a temporary hold on a file. While it is set to
# `true`, the file is protected against deletion and overwrites. Once
# removed, the file's `retention_expires_at` date is not changed. The
# default value is `false`.
#
# See {#retention_expires_at}.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
# file = bucket.file "path/to/my-file.ext"
#
# file.temporary_hold? #=> false
# file.set_temporary_hold!
# file.delete # raises Google::Cloud::PermissionDeniedError
#
def set_temporary_hold!
@gapi.temporary_hold = true
update_gapi! :temporary_hold
end
##
# Sets the temporary hold property of the file to `false`. This property
# is used to enforce a temporary hold on a file. While it is set to
# `true`, the file is protected against deletion and overwrites. Once
# removed, the file's `retention_expires_at` date is not changed. The
# default value is `false`.
#
# See {#retention_expires_at}.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
# file = bucket.file "path/to/my-file.ext"
#
# file.temporary_hold? #=> false
# file.set_temporary_hold!
# file.delete # raises Google::Cloud::PermissionDeniedError
#
# file.release_temporary_hold!
# file.delete
#
def release_temporary_hold!
@gapi.temporary_hold = false
update_gapi! :temporary_hold
end
##
# Whether there is an event-based hold on the file. An event-based
# hold will be enforced on the file as long as this property is `true`,
# even if the bucket-level retention policy would normally allow
# deletion. Removing the event-based hold extends the retention duration
# of the file to the current date plus the bucket-level policy duration.
# Removing the event-based hold represents that a retention-related
# event has occurred, and thus the retention clock ticks from the moment
# of the event as opposed to the creation date of the object. The
# default value is configured at the bucket level (which defaults to
# `false`), and is assigned to newly-created objects.
#
# See {#set_event_based_hold!}, {#release_event_based_hold!},
# {Bucket#default_event_based_hold?} and
# {Bucket#default_event_based_hold=}.
#
# If a bucket's retention policy duration is modified after the
# event-based hold flag is unset, the updated retention duration applies
# retroactively to objects that previously had event-based holds. For
# example:
#
# * If the bucket's [unlocked] retention policy is removed, objects with
# event-based holds may be deleted immediately after the hold is
# removed (the duration of a nonexistent policy for the purpose of
# event-based holds is considered to be zero).
# * If the bucket's [unlocked] policy is reduced, objects with
# previously released event-based holds will be have their retention
# expiration dates reduced accordingly.
# * If the bucket's policy is extended, objects with previously released
# event-based holds will have their retention expiration dates
# extended accordingly. However, note that objects with event-based
# holds released prior to the effective date of the new policy may
# have already been deleted by the user.
#
# @return [Boolean] Returns `true` if there is an event-based hold on
# the file, otherwise `false`.
#
# @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.file "path/to/my-file.ext"
#
# file.event_based_hold? #=> false
# file.set_event_based_hold!
# 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 event_based_hold?
!@gapi.event_based_hold.nil? && @gapi.event_based_hold
end
##
# Sets the event-based hold property of the file to `true`. This
# property enforces an event-based hold on the file as long as this
# property is `true`, even if the bucket-level retention policy would
# normally allow deletion. The default value is configured at the
# bucket level (which defaults to `false`), and is assigned to
# newly-created objects.
#
# See {#event_based_hold?}, {#release_event_based_hold!},
# {Bucket#default_event_based_hold?} and
# {Bucket#default_event_based_hold=}.
#
# If a bucket's retention policy duration is modified after the
# event-based hold is removed, the updated retention duration applies
# retroactively to objects that previously had event-based holds. For
# example:
#
# * If the bucket's [unlocked] retention policy is removed, objects with
# event-based holds may be deleted immediately after the hold is
# removed (the duration of a nonexistent policy for the purpose of
# event-based holds is considered to be zero).
# * If the bucket's [unlocked] policy is reduced, objects with
# previously released event-based holds will be have their retention
# expiration dates reduced accordingly.
# * If the bucket's policy is extended, objects with previously released
# event-based holds will have their retention expiration dates
# extended accordingly. However, note that objects with event-based
# holds released prior to the effective date of the new policy may
# have already been deleted by the user.
#
# @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.file "path/to/my-file.ext"
#
# file.event_based_hold? #=> false
# file.set_event_based_hold!
# 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 set_event_based_hold!
@gapi.event_based_hold = true
update_gapi! :event_based_hold
end
##
# Sets the event-based hold property of the file to `false`. Removing
# the event-based hold extends the retention duration of the file to the
# current date plus the bucket-level policy duration. Removing the
# event-based hold represents that a retention-related event has
# occurred, and thus the retention clock ticks from the moment of the
# event as opposed to the creation date of the object. The default value
# is configured at the bucket level (which defaults to `false`), and is
# assigned to newly-created objects.
#
# See {#event_based_hold?}, {#set_event_based_hold!},
# {Bucket#default_event_based_hold?} and
# {Bucket#default_event_based_hold=}.
#
# @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.file "path/to/my-file.ext"
#
# file.event_based_hold? #=> false
# file.set_event_based_hold!
# 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 release_event_based_hold!
@gapi.event_based_hold = false
update_gapi! :event_based_hold
end
##
# The retention expiration time of the file. This field is indirectly
# mutable when the retention policy applicable to the object changes.
# The date represents the earliest time that the object could be
# deleted, assuming no temporary hold is set. (See {#temporary_hold?}.)
# It is provided when both of the following are true:
#
# * There is a retention policy on the bucket.
# * The eventBasedHold flag is unset on the object.
#
# Note that it can be provided even when {#temporary_hold?} is `true`
# (so that the user can reason about policy without having to first
# unset the temporary hold).
#
# @return [DateTime, nil] A DateTime representing the earliest time at
# which the object can be deleted, or `nil` if there are no
# restrictions on deleting the object.
#
def retention_expires_at
@gapi.retention_expiration_time
end
##
# Retrieves a list of versioned files for the current object.
#
# Useful for listing archived versions of the file, restoring the live
# version of the file to an older version, or deleting an archived
# version. You can turn versioning on or off for a bucket at any time
# with {Bucket#versioning=}. Turning versioning off leaves existing file
# versions in place and causes the bucket to stop accumulating new
# archived object versions. (See {Bucket#versioning?} and
# {File#generation})
#
# @see https://cloud.google.com/storage/docs/object-versioning 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"
#
# file = bucket.file "path/to/my-file.ext"
# file.generation #=> 1234567890
# file.generations.each do |versioned_file|
# versioned_file.generation
# end
#
def generations
ensure_service!
gapi = service.list_files bucket, prefix: name,
versions: true,
user_project: user_project
File::List.from_gapi gapi, service, bucket, name, nil, nil, true,
user_project: user_project
end
##
# Updates the file with changes made in the given block in a single
# PATCH request. The following attributes may be set: {#cache_control=},
# {#content_disposition=}, {#content_encoding=}, {#content_language=},
# {#content_type=}, {#custom_time=} and {#metadata=}. The {#metadata} hash
# accessible in the block is completely mutable and will be included in the
# request.
#
# @yield [file] a block yielding a delegate object for updating the file
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# file = bucket.file "path/to/my-file.ext"
#
# file.update do |f|
# f.cache_control = "private, max-age=0, no-cache"
# f.content_disposition = "inline; filename=filename.ext"
# f.content_encoding = "deflate"
# f.content_language = "de"
# f.content_type = "application/json"
# f.custom_time = DateTime.new 2025, 12, 31
# f.metadata["player"] = "Bob"
# f.metadata["score"] = "10"
# end
#
def update
updater = Updater.new gapi
yield updater
updater.check_for_changed_metadata!
update_gapi! updater.updates unless updater.updates.empty?
end
##
# Downloads the file's contents to a local file or an File-like object.
#
# By default, the download is verified by calculating the MD5 digest.
#
# If a [customer-supplied encryption
# key](https://cloud.google.com/storage/docs/encryption#customer-supplied)
# was used with {Bucket#create_file}, the `encryption_key` option must
# be provided.
#
# @param [String, ::File] path The path on the local file system to
# write the data to. The path provided must be writable. Can also be
# an File object, or File-like object such as StringIO. If an file
# object, the object will be written to, not the filesystem. If
# omitted, a new StringIO instance will be written to and returned.
# Optional.
# @param [Symbol] verify The verification algorithm used to ensure the
# downloaded file contents are correct. Default is `:md5`.
#
# Acceptable values are:
#
# * `md5` - Verify file content match using the MD5 hash.
# * `crc32c` - Verify file content match using the CRC32c hash.
# * `all` - Perform all available file content verification.
# * `none` - Don't perform file content verification.
#
# @param [String] encryption_key Optional. The customer-supplied,
# AES-256 encryption key used to encrypt the file, if one was provided
# to {Bucket#create_file}.
#
# @param [Range, String] range Optional. The byte range of the file's
# contents to download or a string header value. Provide this to
# perform a partial download. When a range is provided, no
# verification is performed regardless of the `verify` parameter's
# value.
#
# @param [Boolean] skip_decompress Optional. If `true`, the data for a
# Storage object returning a `Content-Encoding: gzip` response header
# will *not* be automatically decompressed by this client library. The
# default is `nil`. Note that all requests by this client library send
# the `Accept-Encoding: gzip` header, so decompressive transcoding is
# not performed in the Storage service. (See [Transcoding of
# gzip-compressed files](https://cloud.google.com/storage/docs/transcoding))
#
# @return [::File, StringIO] Returns a file object representing the file
# data. This will ordinarily be a `::File` object referencing the
# local file system. However, if the argument to `path` is `nil`, a
# StringIO instance will be returned. If the argument to `path` is an
# File-like object, then that object will be returned.
#
# @example
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# file = bucket.file "path/to/my-file.ext"
# file.download "path/to/downloaded/file.ext"
#
# @example Use the CRC32c digest by passing :crc32c.
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# file = bucket.file "path/to/my-file.ext"
# file.download "path/to/downloaded/file.ext", verify: :crc32c
#
# @example Use the MD5 and CRC32c digests by passing :all.
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# file = bucket.file "path/to/my-file.ext"
# file.download "path/to/downloaded/file.ext", verify: :all
#
# @example Disable the download verification by passing :none.
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# file = bucket.file "path/to/my-file.ext"
# file.download "path/to/downloaded/file.ext", verify: :none
#
# @example Download to an in-memory StringIO object.
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# bucket = storage.bucket "my-bucket"
#
# file = bucket.file "path/to/my-file.ext"
# downloaded = file.download
# downloaded.rewind
# downloaded.read #=> "Hello world!"
#
# @example Download a public file with an unauthenticated client.
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.anonymous
#
# bucket = storage.bucket "public-bucket", skip_lookup: true
# file = bucket.file "path/to/public-file.ext", skip_lookup: true
#
# downloaded = file.download
# downloaded.rewind
# downloaded.read #=> "Hello world!"
#
# @example Upload and download gzip-encoded file data.
# require "zlib"
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
#
# gz = StringIO.new ""
# z = Zlib::GzipWriter.new gz
# z.write "Hello world!"
# z.close
# data = StringIO.new gz.string
#
# bucket = storage.bucket "my-bucket"
#
# bucket.create_file data, "path/to/gzipped.txt",
# content_encoding: "gzip"
#
# file = bucket.file "path/to/gzipped.txt"
#
# # The downloaded data is decompressed by default.
# file.download "path/to/downloaded/hello.txt"
#
# # The downloaded data remains compressed with skip_decompress.
# file.download "path/to/downloaded/gzipped.txt",
# skip_decompress: true
#
# @example Partially download.
#
# require "google/cloud/storage"
#
# storage = Google::Cloud::Storage.new
# bucket = storage.bucket "my-bucket"
# file = bucket.file "path/to/my-file.ext"
#
# downloaded = file.download range: 6..10
# downloaded.rewind
# downloaded.read #=> "world"
#
def download path = nil, verify: :md5, encryption_key: nil, range: nil,
skip_decompress: nil
ensure_service!
if path.nil?
path = StringIO.new
path.set_encoding "ASCII-8BIT"
end
file, resp =
service.download_file bucket, name, path,
generation: generation, key: encryption_key,
range: range, user_project: user_project
# FIX: downloading with encryption key will return nil
file ||= ::File.new path
verify = :none if range
verify_file! file, verify
if !skip_decompress &&
Array(resp.header["Content-Encoding"]).include?("gzip")
file = gzip_decompress file
end
file
end
##
# Copies the file to a new location. Metadata excluding ACL from the source
# object will be copied to the destination object unless a block is provided.
#
# If an optional block for updating is provided, only the updates made in
# this block will appear in the destination object, and other metadata
# fields in the destination object will not be copied. To copy the other
# source file metadata fields while updating destination fields in a
# block, use the `force_copy_metadata: true` flag, and the client library
# will copy metadata from source metadata into the copy request.
#
# If a [customer-supplied encryption
# key](https://cloud.google.com/storage/docs/encryption#customer-supplied)
# was used with {Bucket#create_file}, the `encryption_key` option must
# be provided.
#
# @param [String] dest_bucket_or_path Either the bucket to copy the file
# to, or the path to copy the file to in the current bucket.
# @param [String] dest_path If a bucket was provided in the first
# parameter, this contains the path to copy the file to in the given
# bucket.