Class: Google::Cloud::Storage::File
- Inherits:
-
Object
- Object
- Google::Cloud::Storage::File
- Defined in:
- lib/google/cloud/storage/file.rb,
lib/google/cloud/storage/file/acl.rb,
lib/google/cloud/storage/file/list.rb,
lib/google/cloud/storage/file/signer.rb,
lib/google/cloud/storage/file/verifier.rb
Overview
File
Represents a File (Object) 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.
Direct Known Subclasses
Defined Under Namespace
Instance Attribute Summary collapse
-
#user_project ⇒ Object
If this attribute is set to
true
, transit costs for operations on the file will be billed to the current project for this client.
Instance Method Summary collapse
-
#acl ⇒ File::Acl
The Acl instance used to control access to the file.
-
#api_url ⇒ String
A URL that can be used to access the file using the REST API.
-
#bucket ⇒ String
The name of the Bucket containing this file.
-
#cache_control ⇒ String
The Cache-Control directive for the file data.
-
#cache_control=(cache_control) ⇒ Object
Updates the Cache-Control directive for the file data.
-
#content_disposition ⇒ String
The Content-Disposition of the file data.
-
#content_disposition=(content_disposition) ⇒ Object
Updates the Content-Disposition of the file data.
-
#content_encoding ⇒ String
The Content-Encoding of the file data.
-
#content_encoding=(content_encoding) ⇒ Object
Updates the Content-Encoding of the file data.
-
#content_language ⇒ String
The Content-Language of the file data.
-
#content_language=(content_language) ⇒ Object
Updates the Content-Language of the file data.
-
#content_type ⇒ String
The Content-Type of the file data.
-
#content_type=(content_type) ⇒ Object
Updates the Content-Type of the file data.
-
#copy(dest_bucket_or_path, dest_path = nil, acl: nil, generation: nil, encryption_key: nil) {|file| ... } ⇒ Google::Cloud::Storage::File
Copy the file to a new location.
-
#crc32c ⇒ String
The CRC32c checksum of the data, as described in RFC 4960, Appendix B.
-
#created_at ⇒ DateTime
Creation time of the file.
-
#delete(generation: nil) ⇒ Boolean
Permanently deletes the file.
-
#download(path = nil, verify: :md5, encryption_key: nil, range: nil, skip_decompress: nil) ⇒ ::File, StringIO
Download the file's contents to a local file or an File-like object.
-
#encryption_key_sha256 ⇒ String
An RFC 4648 Base64-encoded string of the SHA256 hash of the customer-supplied encryption key.
-
#etag ⇒ String
HTTP 1.1 Entity tag for the file.
-
#exists? ⇒ Boolean
Determines whether the file exists in the Storage service.
-
#generation ⇒ Fixnum
The content generation of this file.
-
#generations ⇒ Array<Google::Cloud::Storage::File>
Retrieves a list of versioned files for the current object.
-
#id ⇒ String
The ID of the file.
-
#kind ⇒ String
The kind of item this is.
-
#kms_key ⇒ String?
The Cloud KMS encryption key that was used to protect the file, or
nil
if none has been configured. -
#md5 ⇒ String
MD5 hash of the data; encoded using base64.
-
#media_url ⇒ String
A URL that can be used to download the file using the REST API.
-
#metadata ⇒ Hash(String => String)
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.
-
#metadata=(metadata) ⇒ Object
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.
-
#metageneration ⇒ Fixnum
The version of the metadata for this file at this generation.
-
#name ⇒ String
The name of this file.
-
#public_url(protocol: :https) ⇒ String
(also: #url)
Public URL to access the file.
-
#reload!(generation: nil) ⇒ Object
(also: #refresh!)
Reloads the file with current data from the Storage service.
-
#rewrite(dest_bucket_or_path, dest_path = nil, acl: nil, generation: nil, encryption_key: nil, new_encryption_key: nil, new_kms_key: nil) {|file| ... } ⇒ Google::Cloud::Storage::File
Rewrites the file to a new location.
-
#rotate(encryption_key: nil, new_encryption_key: nil, new_kms_key: nil) ⇒ Google::Cloud::Storage::File
Rewrites the file to the same #bucket and #name with a new customer-supplied encryption key.
-
#signed_url(method: nil, expires: nil, content_type: nil, content_md5: nil, headers: nil, issuer: nil, client_email: nil, signing_key: nil, private_key: nil, query: nil) ⇒ String
Access without authentication can be granted to a File for a specified period of time.
-
#size ⇒ Integer
Content-Length of the data in bytes.
-
#storage_class ⇒ String
The file's storage class.
-
#storage_class=(storage_class) ⇒ Object
Rewrites the file with a new storage class, which determines the SLA and the cost of storage.
-
#update {|file| ... } ⇒ Object
Updates the file with changes made in the given block in a single PATCH request.
-
#updated_at ⇒ DateTime
The creation or modification time of the file.
Instance Attribute Details
#user_project ⇒ Object
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.
96 97 98 |
# File 'lib/google/cloud/storage/file.rb', line 96 def user_project @user_project end |
Instance Method Details
#acl ⇒ File::Acl
The Acl instance used to control access to the file.
A file has owners, writers, and readers. Permissions can be granted to an individual user's email address, a group's email address, as well as many predefined lists.
1279 1280 1281 |
# File 'lib/google/cloud/storage/file.rb', line 1279 def acl @acl ||= File::Acl.new self end |
#api_url ⇒ String
A URL that can be used to access the file using the REST API.
174 175 176 |
# File 'lib/google/cloud/storage/file.rb', line 174 def api_url @gapi.self_link end |
#bucket ⇒ String
The name of the Bucket containing this file.
143 144 145 |
# File 'lib/google/cloud/storage/file.rb', line 143 def bucket @gapi.bucket end |
#cache_control ⇒ String
The Cache-Control
directive for the file data. If omitted, and the file is accessible
to all anonymous users, the default will be public, max-age=3600
.
252 253 254 |
# File 'lib/google/cloud/storage/file.rb', line 252 def cache_control @gapi.cache_control end |
#cache_control=(cache_control) ⇒ Object
Updates the
Cache-Control
directive for the file data. If omitted, and the file is accessible
to all anonymous users, the default will be public, max-age=3600
.
264 265 266 267 |
# File 'lib/google/cloud/storage/file.rb', line 264 def cache_control= cache_control @gapi.cache_control = cache_control update_gapi! :cache_control end |
#content_disposition ⇒ String
The Content-Disposition of the file data.
275 276 277 |
# File 'lib/google/cloud/storage/file.rb', line 275 def content_disposition @gapi.content_disposition end |
#content_disposition=(content_disposition) ⇒ Object
Updates the Content-Disposition of the file data.
286 287 288 289 |
# File 'lib/google/cloud/storage/file.rb', line 286 def content_disposition= content_disposition @gapi.content_disposition = content_disposition update_gapi! :content_disposition end |
#content_encoding ⇒ String
The Content-Encoding of the file data.
298 299 300 |
# File 'lib/google/cloud/storage/file.rb', line 298 def content_encoding @gapi.content_encoding end |
#content_encoding=(content_encoding) ⇒ Object
Updates the Content-Encoding of the file data.
309 310 311 312 |
# File 'lib/google/cloud/storage/file.rb', line 309 def content_encoding= content_encoding @gapi.content_encoding = content_encoding update_gapi! :content_encoding end |
#content_language ⇒ String
The Content-Language of the file data.
320 321 322 |
# File 'lib/google/cloud/storage/file.rb', line 320 def content_language @gapi.content_language end |
#content_language=(content_language) ⇒ Object
Updates the Content-Language of the file data.
330 331 332 333 |
# File 'lib/google/cloud/storage/file.rb', line 330 def content_language= content_language @gapi.content_language = content_language update_gapi! :content_language end |
#content_type ⇒ String
The Content-Type of the file data.
341 342 343 |
# File 'lib/google/cloud/storage/file.rb', line 341 def content_type @gapi.content_type end |
#content_type=(content_type) ⇒ Object
Updates the Content-Type of the file data.
352 353 354 355 |
# File 'lib/google/cloud/storage/file.rb', line 352 def content_type= content_type @gapi.content_type = content_type update_gapi! :content_type end |
#copy(dest_bucket_or_path, dest_path = nil, acl: nil, generation: nil, encryption_key: nil) {|file| ... } ⇒ Google::Cloud::Storage::File
Copy the file to a new location.
If a customer-supplied encryption
key
was used with Bucket#create_file, the encryption_key
option must
be provided.
783 784 785 786 787 788 789 790 791 |
# File 'lib/google/cloud/storage/file.rb', line 783 def copy dest_bucket_or_path, dest_path = nil, acl: nil, generation: nil, encryption_key: nil rewrite dest_bucket_or_path, dest_path, acl: acl, generation: generation, encryption_key: encryption_key, new_encryption_key: encryption_key do |updater| yield updater if block_given? end end |
#crc32c ⇒ String
The CRC32c checksum of the data, as described in RFC 4960, Appendix B. Encoded using base64 in big-endian byte order.
232 233 234 |
# File 'lib/google/cloud/storage/file.rb', line 232 def crc32c @gapi.crc32c end |
#created_at ⇒ DateTime
Creation time of the file.
201 202 203 |
# File 'lib/google/cloud/storage/file.rb', line 201 def created_at @gapi.time_created end |
#delete(generation: nil) ⇒ Boolean
Permanently deletes the file.
1081 1082 1083 1084 1085 1086 1087 |
# File 'lib/google/cloud/storage/file.rb', line 1081 def delete generation: nil generation = self.generation if generation == true ensure_service! service.delete_file bucket, name, generation: generation, user_project: user_project true end |
#download(path = nil, verify: :md5, encryption_key: nil, range: nil, skip_decompress: nil) ⇒ ::File, StringIO
Download 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
was used with Bucket#create_file, the encryption_key
option must
be provided.
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
# File 'lib/google/cloud/storage/file.rb', line 676 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, 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 |
#encryption_key_sha256 ⇒ String
An RFC 4648 Base64-encoded string of the SHA256 hash of the customer-supplied encryption key. You can use this SHA256 hash to uniquely identify the AES-256 encryption key required to decrypt this file.
393 394 395 396 |
# File 'lib/google/cloud/storage/file.rb', line 393 def encryption_key_sha256 return nil unless @gapi.customer_encryption Base64.decode64 @gapi.customer_encryption.key_sha256 end |
#etag ⇒ String
HTTP 1.1 Entity tag for the file.
241 242 243 |
# File 'lib/google/cloud/storage/file.rb', line 241 def etag @gapi.etag end |
#exists? ⇒ Boolean
Determines whether the file exists in the Storage service.
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 |
# File 'lib/google/cloud/storage/file.rb', line 1335 def exists? # Always true if we have a grpc object return true unless lazy? # If we have a value, return it return @exists unless @exists.nil? ensure_gapi! @exists = true rescue Google::Cloud::NotFoundError @exists = false end |
#generation ⇒ Fixnum
The content generation of this file. Used for object versioning.
153 154 155 |
# File 'lib/google/cloud/storage/file.rb', line 153 def generation @gapi.generation end |
#generations ⇒ Array<Google::Cloud::Storage::File>
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 #generation)
480 481 482 483 484 485 486 487 |
# File 'lib/google/cloud/storage/file.rb', line 480 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 |
#id ⇒ String
The ID of the file.
125 126 127 |
# File 'lib/google/cloud/storage/file.rb', line 125 def id @gapi.id end |
#kind ⇒ String
The kind of item this is. For files, this is always storage#object.
116 117 118 |
# File 'lib/google/cloud/storage/file.rb', line 116 def kind @gapi.kind end |
#kms_key ⇒ String?
The Cloud KMS encryption key that was used to protect the file, or
nil
if none has been configured.
408 409 410 |
# File 'lib/google/cloud/storage/file.rb', line 408 def kms_key @gapi.kms_key_name end |
#md5 ⇒ String
MD5 hash of the data; encoded using base64.
221 222 223 |
# File 'lib/google/cloud/storage/file.rb', line 221 def md5 @gapi.md5_hash end |
#media_url ⇒ String
A URL that can be used to download the file using the REST API.
183 184 185 |
# File 'lib/google/cloud/storage/file.rb', line 183 def media_url @gapi.media_link end |
#metadata ⇒ Hash(String => String)
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.
364 365 366 367 368 |
# File 'lib/google/cloud/storage/file.rb', line 364 def m = @gapi. m = m.to_h if m.respond_to? :to_h m.dup.freeze end |
#metadata=(metadata) ⇒ Object
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.
378 379 380 381 |
# File 'lib/google/cloud/storage/file.rb', line 378 def @gapi. = update_gapi! :metadata end |
#metageneration ⇒ Fixnum
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.
165 166 167 |
# File 'lib/google/cloud/storage/file.rb', line 165 def @gapi. end |
#name ⇒ String
The name of this file.
134 135 136 |
# File 'lib/google/cloud/storage/file.rb', line 134 def name @gapi.name end |
#public_url(protocol: :https) ⇒ String Also known as: url
Public URL to access the file. If the file is not public, requests to the URL will return an error. (See Google::Cloud::Storage::File::Acl#public! and Bucket::DefaultAcl#public!) To share a file that is not public see #signed_url.
1121 1122 1123 |
# File 'lib/google/cloud/storage/file.rb', line 1121 def public_url protocol: :https "#{protocol}://storage.googleapis.com/#{bucket}/#{name}" end |
#reload!(generation: nil) ⇒ Object Also known as: refresh!
Reloads the file with current data from the Storage service.
1322 1323 1324 1325 1326 1327 1328 1329 1330 |
# File 'lib/google/cloud/storage/file.rb', line 1322 def reload! generation: nil generation = self.generation if generation == true ensure_service! @gapi = service.get_file bucket, name, generation: generation, user_project: user_project # If NotFound then lazy will never be unset @lazy = nil self end |
#rewrite(dest_bucket_or_path, dest_path = nil, acl: nil, generation: nil, encryption_key: nil, new_encryption_key: nil, new_kms_key: nil) {|file| ... } ⇒ Google::Cloud::Storage::File
Rewrites the file to a new location. Or the same location can be provided to rewrite the file in place.
If a customer-supplied encryption
key
was used with Bucket#create_file, the encryption_key
option must
be provided. Unlike #copy, separate encryption keys are used to read
(encryption_key) and to write (new_encryption_key) file contents.
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 |
# File 'lib/google/cloud/storage/file.rb', line 937 def rewrite dest_bucket_or_path, dest_path = nil, acl: nil, generation: nil, encryption_key: nil, new_encryption_key: nil, new_kms_key: nil ensure_service! dest_bucket, dest_path = fix_rewrite_args dest_bucket_or_path, dest_path update_gapi = nil if block_given? updater = Updater.new gapi yield updater updater. if updater.updates.any? update_gapi = gapi_from_attrs updater.updates end end new_gapi = rewrite_gapi bucket, name, update_gapi, new_bucket: dest_bucket, new_name: dest_path, acl: acl, generation: generation, encryption_key: encryption_key, new_encryption_key: new_encryption_key, new_kms_key: new_kms_key, user_project: user_project File.from_gapi new_gapi, service, user_project: user_project end |
#rotate(encryption_key: nil, new_encryption_key: nil, new_kms_key: nil) ⇒ Google::Cloud::Storage::File
Rewrites the file to the same #bucket and #name with a new customer-supplied encryption key.
If a new key is provided to this method, the new key must be used to subsequently download or copy the file. You must securely manage your keys and ensure that they are not lost. Also, please note that file metadata is not encrypted, with the exception of the CRC32C checksum and MD5 hash. The names of files and buckets are also not encrypted, and you can read or update the metadata of an encrypted file without providing the encryption key.
1034 1035 1036 1037 1038 1039 |
# File 'lib/google/cloud/storage/file.rb', line 1034 def rotate encryption_key: nil, new_encryption_key: nil, new_kms_key: nil rewrite bucket, name, encryption_key: encryption_key, new_encryption_key: new_encryption_key, new_kms_key: new_kms_key end |
#signed_url(method: nil, expires: nil, content_type: nil, content_md5: nil, headers: nil, issuer: nil, client_email: nil, signing_key: nil, private_key: nil, query: nil) ⇒ String
Access without authentication can be granted to a File for a specified period of time. This URL uses a cryptographic signature of your credentials to access the file.
Generating a URL requires service account credentials, either by
connecting with a service account when calling
Google::Cloud.storage, or by passing in the service account issuer
and signing_key
values. Although the private key can be passed as a
string for convenience, creating and storing an instance of
OpenSSL::PKey::RSA
is more efficient when making multiple calls to
signed_url
.
A SignedUrlUnavailable is raised if the service account credentials are missing. Service account credentials are acquired by following the steps in Service Account Authentication.
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 |
# File 'lib/google/cloud/storage/file.rb', line 1221 def signed_url method: nil, expires: nil, content_type: nil, content_md5: nil, headers: nil, issuer: nil, client_email: nil, signing_key: nil, private_key: nil, query: nil ensure_service! signer = File::Signer.from_file self signer.signed_url method: method, expires: expires, headers: headers, content_type: content_type, content_md5: content_md5, issuer: issuer, client_email: client_email, signing_key: signing_key, private_key: private_key, query: query end |
#size ⇒ Integer
Content-Length of the data in bytes.
192 193 194 |
# File 'lib/google/cloud/storage/file.rb', line 192 def size @gapi.size.to_i if @gapi.size end |
#storage_class ⇒ String
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 and Per-Object Storage Class.
422 423 424 |
# File 'lib/google/cloud/storage/file.rb', line 422 def storage_class @gapi.storage_class end |
#storage_class=(storage_class) ⇒ Object
Rewrites the file with a new storage class, which determines the SLA and the cost of storage. Accepted values include:
:multi_regional
:regional
:nearline
:coldline
as well as the equivalent strings returned by #storage_class or Bucket#storage_class. For more information, see Storage Classes and Per-Object Storage Class. The default value is the default storage class for the bucket. See Bucket#storage_class.
445 446 447 448 |
# File 'lib/google/cloud/storage/file.rb', line 445 def storage_class= storage_class @gapi.storage_class = storage_class_for(storage_class) update_gapi! :storage_class end |
#update {|file| ... } ⇒ Object
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=, and #metadata=. The #metadata hash accessible in the block is completely mutable and will be included in the request.
517 518 519 520 521 522 |
# File 'lib/google/cloud/storage/file.rb', line 517 def update updater = Updater.new gapi yield updater updater. update_gapi! updater.updates unless updater.updates.empty? end |
#updated_at ⇒ DateTime
The creation or modification time of the file. For buckets with versioning enabled, changing an object's metadata does not change this property.
212 213 214 |
# File 'lib/google/cloud/storage/file.rb', line 212 def updated_at @gapi.updated end |