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/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 Method Summary collapse
-
#acl ⇒ Object
The Acl instance used to control access to the file.
-
#api_url ⇒ Object
A URL that can be used to access the file using the REST API.
-
#bucket ⇒ Object
The name of the Bucket containing this file.
-
#cache_control ⇒ Object
The Cache-Control directive for the file data.
-
#cache_control=(cache_control) ⇒ Object
Updates the Cache-Control directive for the file data.
-
#content_disposition ⇒ Object
The Content-Disposition of the file data.
-
#content_disposition=(content_disposition) ⇒ Object
Updates the Content-Disposition of the file data.
-
#content_encoding ⇒ Object
The Content-Encoding of the file data.
-
#content_encoding=(content_encoding) ⇒ Object
Updates the Content-Encoding of the file data.
-
#content_language ⇒ Object
The Content-Language of the file data.
-
#content_language=(content_language) ⇒ Object
Updates the Content-Language of the file data.
-
#content_type ⇒ Object
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) ⇒ Google::Cloud::Storage::File
Copy the file to a new location.
-
#crc32c ⇒ Object
The CRC32c checksum of the data, as described in RFC 4960, Appendix B.
-
#created_at ⇒ Object
Creation time of the file.
-
#delete ⇒ Boolean
Permanently deletes the file.
-
#download(path, verify: :md5, encryption_key: nil) ⇒ File
Download the file's contents to a local file.
-
#encryption_key_sha256 ⇒ Object
An RFC 4648 Base64-encoded string of the SHA256 hash of the customer-supplied encryption key.
-
#etag ⇒ Object
HTTP 1.1 Entity tag for the file.
-
#generation ⇒ Object
The content generation of this file.
-
#id ⇒ Object
The ID of the file.
-
#kind ⇒ Object
The kind of item this is.
-
#md5 ⇒ Object
MD5 hash of the data; encoded using base64.
-
#media_url ⇒ Object
A URL that can be used to download the file using the REST API.
-
#metadata ⇒ Object
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 ⇒ Object
The version of the metadata for this file at this generation.
-
#name ⇒ Object
The name of this file.
-
#public_url(protocol: :https) ⇒ Object
(also: #url)
Public URL to access the file.
-
#reload! ⇒ Object
(also: #refresh!)
Reloads the file with current data from the Storage service.
-
#rotate(encryption_key: nil, new_encryption_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) ⇒ Object
Access without authentication can be granted to a File for a specified period of time.
-
#size ⇒ Object
Content-Length of the data in bytes.
-
#storage_class ⇒ Object
The file's storage class.
-
#storage_class=(storage_class) ⇒ Object
Updates how the file is stored and 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 ⇒ Object
The creation or modification time of the file.
Instance Method Details
#acl ⇒ Object
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.
745 746 747 |
# File 'lib/google/cloud/storage/file.rb', line 745 def acl @acl ||= File::Acl.new self end |
#api_url ⇒ Object
A URL that can be used to access the file using the REST API.
110 111 112 |
# File 'lib/google/cloud/storage/file.rb', line 110 def api_url @gapi.self_link end |
#bucket ⇒ Object
The name of the Bucket containing this file.
88 89 90 |
# File 'lib/google/cloud/storage/file.rb', line 88 def bucket @gapi.bucket end |
#cache_control ⇒ Object
The Cache-Control directive for the file data.
163 164 165 |
# File 'lib/google/cloud/storage/file.rb', line 163 def cache_control @gapi.cache_control end |
#cache_control=(cache_control) ⇒ Object
Updates the Cache-Control directive for the file data.
171 172 173 174 |
# File 'lib/google/cloud/storage/file.rb', line 171 def cache_control= cache_control @gapi.cache_control = cache_control patch_gapi! :cache_control end |
#content_disposition ⇒ Object
The Content-Disposition of the file data.
179 180 181 |
# File 'lib/google/cloud/storage/file.rb', line 179 def content_disposition @gapi.content_disposition end |
#content_disposition=(content_disposition) ⇒ Object
Updates the Content-Disposition of the file data.
186 187 188 189 |
# File 'lib/google/cloud/storage/file.rb', line 186 def content_disposition= content_disposition @gapi.content_disposition = content_disposition patch_gapi! :content_disposition end |
#content_encoding ⇒ Object
The Content-Encoding of the file data.
195 196 197 |
# File 'lib/google/cloud/storage/file.rb', line 195 def content_encoding @gapi.content_encoding end |
#content_encoding=(content_encoding) ⇒ Object
Updates the Content-Encoding of the file data.
203 204 205 206 |
# File 'lib/google/cloud/storage/file.rb', line 203 def content_encoding= content_encoding @gapi.content_encoding = content_encoding patch_gapi! :content_encoding end |
#content_language ⇒ Object
The Content-Language of the file data.
211 212 213 |
# File 'lib/google/cloud/storage/file.rb', line 211 def content_language @gapi.content_language end |
#content_language=(content_language) ⇒ Object
Updates the Content-Language of the file data.
218 219 220 221 |
# File 'lib/google/cloud/storage/file.rb', line 218 def content_language= content_language @gapi.content_language = content_language patch_gapi! :content_language end |
#content_type ⇒ Object
The Content-Type of the file data.
226 227 228 |
# File 'lib/google/cloud/storage/file.rb', line 226 def content_type @gapi.content_type end |
#content_type=(content_type) ⇒ Object
Updates the Content-Type of the file data.
234 235 236 237 |
# File 'lib/google/cloud/storage/file.rb', line 234 def content_type= content_type @gapi.content_type = content_type patch_gapi! :content_type end |
#copy(dest_bucket_or_path, dest_path = nil, acl: nil, generation: nil, encryption_key: nil) ⇒ 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.
481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/google/cloud/storage/file.rb', line 481 def copy dest_bucket_or_path, dest_path = nil, acl: nil, generation: nil, encryption_key: nil ensure_service! = { acl: acl, generation: generation, key: encryption_key } dest_bucket, dest_path, = fix_copy_args dest_bucket_or_path, dest_path, gapi = service.copy_file bucket, name, dest_bucket, dest_path, File.from_gapi gapi, service end |
#crc32c ⇒ Object
The CRC32c checksum of the data, as described in RFC 4960, Appendix B. Encoded using base64 in big-endian byte order.
150 151 152 |
# File 'lib/google/cloud/storage/file.rb', line 150 def crc32c @gapi.crc32c end |
#created_at ⇒ Object
Creation time of the file.
128 129 130 |
# File 'lib/google/cloud/storage/file.rb', line 128 def created_at @gapi.time_created end |
#delete ⇒ Boolean
Permanently deletes the file.
566 567 568 569 570 |
# File 'lib/google/cloud/storage/file.rb', line 566 def delete ensure_service! service.delete_file bucket, name true end |
#download(path, verify: :md5, encryption_key: nil) ⇒ File
Download the file's contents to a local file.
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.
402 403 404 405 406 407 408 |
# File 'lib/google/cloud/storage/file.rb', line 402 def download path, verify: :md5, encryption_key: nil ensure_service! service.download_file \ bucket, name, path, key: encryption_key verify_file! ::File.new(path), verify end |
#encryption_key_sha256 ⇒ Object
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.
265 266 267 268 |
# File 'lib/google/cloud/storage/file.rb', line 265 def encryption_key_sha256 return nil unless @gapi.customer_encryption Base64.decode64 @gapi.customer_encryption.key_sha256 end |
#etag ⇒ Object
HTTP 1.1 Entity tag for the file.
156 157 158 |
# File 'lib/google/cloud/storage/file.rb', line 156 def etag @gapi.etag end |
#generation ⇒ Object
The content generation of this file. Used for object versioning.
95 96 97 |
# File 'lib/google/cloud/storage/file.rb', line 95 def generation @gapi.generation end |
#id ⇒ Object
The ID of the file.
76 77 78 |
# File 'lib/google/cloud/storage/file.rb', line 76 def id @gapi.id end |
#kind ⇒ Object
The kind of item this is. For files, this is always storage#object.
70 71 72 |
# File 'lib/google/cloud/storage/file.rb', line 70 def kind @gapi.kind end |
#md5 ⇒ Object
MD5 hash of the data; encoded using base64.
142 143 144 |
# File 'lib/google/cloud/storage/file.rb', line 142 def md5 @gapi.md5_hash end |
#media_url ⇒ Object
A URL that can be used to download the file using the REST API.
116 117 118 |
# File 'lib/google/cloud/storage/file.rb', line 116 def media_url @gapi.media_link end |
#metadata ⇒ Object
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.
243 244 245 246 247 |
# File 'lib/google/cloud/storage/file.rb', line 243 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.
253 254 255 256 |
# File 'lib/google/cloud/storage/file.rb', line 253 def @gapi. = patch_gapi! :metadata end |
#metageneration ⇒ Object
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.
104 105 106 |
# File 'lib/google/cloud/storage/file.rb', line 104 def @gapi. end |
#name ⇒ Object
The name of this file.
82 83 84 |
# File 'lib/google/cloud/storage/file.rb', line 82 def name @gapi.name end |
#public_url(protocol: :https) ⇒ Object 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.
602 603 604 |
# File 'lib/google/cloud/storage/file.rb', line 602 def public_url protocol: :https "#{protocol}://storage.googleapis.com/#{bucket}/#{name}" end |
#reload! ⇒ Object Also known as: refresh!
Reloads the file with current data from the Storage service.
751 752 753 754 |
# File 'lib/google/cloud/storage/file.rb', line 751 def reload! ensure_service! @gapi = service.get_file bucket, name end |
#rotate(encryption_key: nil, new_encryption_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.
539 540 541 542 543 544 545 546 547 548 549 |
# File 'lib/google/cloud/storage/file.rb', line 539 def rotate encryption_key: nil, new_encryption_key: nil ensure_service! = { source_key: encryption_key, destination_key: new_encryption_key } gapi = service.rewrite_file bucket, name, bucket, name, until gapi.done [:token] = gapi.rewrite_token gapi = service.rewrite_file bucket, name, bucket, name, end File.from_gapi gapi.resource, service 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) ⇒ Object
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.
691 692 693 694 695 696 697 698 699 700 701 |
# File 'lib/google/cloud/storage/file.rb', line 691 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 ensure_service! = { 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 } signer = File::Signer.from_file self signer.signed_url end |
#size ⇒ Object
Content-Length of the data in bytes.
122 123 124 |
# File 'lib/google/cloud/storage/file.rb', line 122 def size @gapi.size.to_i if @gapi.size end |
#storage_class ⇒ Object
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.
277 278 279 |
# File 'lib/google/cloud/storage/file.rb', line 277 def storage_class @gapi.storage_class end |
#storage_class=(storage_class) ⇒ Object
Updates how the file is stored and determines the SLA and the cost of
storage. Values include :multi_regional
, :regional
, :nearline
,
:coldline
, :standard
, and :dra
(Durable Reduced Availability),
as well as the 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.
293 294 295 296 297 |
# File 'lib/google/cloud/storage/file.rb', line 293 def storage_class= storage_class resp = service.update_file_storage_class \ bucket, name, storage_class_for(storage_class) @gapi = resp.resource 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.
327 328 329 330 331 332 |
# File 'lib/google/cloud/storage/file.rb', line 327 def update updater = Updater.new gapi yield updater updater. patch_gapi! updater.updates unless updater.updates.empty? end |
#updated_at ⇒ Object
The creation or modification time of the file. For buckets with versioning enabled, changing an object's metadata does not change this property.
136 137 138 |
# File 'lib/google/cloud/storage/file.rb', line 136 def updated_at @gapi.updated end |