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](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.
Direct Known Subclasses
Defined Under Namespace
Modules: Verifier Classes: Acl, List, Signer, Updater
Instance Attribute Summary collapse
Class Method Summary collapse
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](tools.ietf.org/html/rfc7234#section-5.2) directive for the file data.
-
#cache_control=(cache_control) ⇒ Object
Updates the [Cache-Control](tools.ietf.org/html/rfc7234#section-5.2) directive for the file data.
-
#content_disposition ⇒ Object
The [Content-Disposition](tools.ietf.org/html/rfc6266) of the file data.
-
#content_disposition=(content_disposition) ⇒ Object
Updates the [Content-Disposition](tools.ietf.org/html/rfc6266) of the file data.
-
#content_encoding ⇒ Object
The [Content-Encoding ](tools.ietf.org/html/rfc7231#section-3.1.2.2) of the file data.
-
#content_encoding=(content_encoding) ⇒ Object
Updates the [Content-Encoding ](tools.ietf.org/html/rfc7231#section-3.1.2.2) of the file data.
-
#content_language ⇒ Object
The [Content-Language](tools.ietf.org/html/bcp47) of the file data.
-
#content_language=(content_language) ⇒ Object
Updates the [Content-Language](tools.ietf.org/html/bcp47) of the file data.
-
#content_type ⇒ Object
The [Content-Type](tools.ietf.org/html/rfc2616#section-14.17) of the file data.
-
#content_type=(content_type) ⇒ Object
Updates the [Content-Type](tools.ietf.org/html/rfc2616#section-14.17) of the file data.
-
#copy(dest_bucket_or_path, dest_path = nil, acl: nil, generation: nil, encryption_key: nil, encryption_key_sha256: 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](tools.ietf.org/html/rfc4960#appendix-B).
-
#created_at ⇒ Object
Creation time of the file.
-
#delete ⇒ Boolean
Permanently deletes the file.
-
#download(path, verify: :md5, encryption_key: nil, encryption_key_sha256: nil) ⇒ File
Download the file’s contents to a local file.
-
#encryption_key_sha256 ⇒ Object
An [RFC 4648](tools.ietf.org/html/rfc4648#section-4) Base64-encoded string of the SHA256 hash of the [customer-supplied encryption key](cloud.google.com/storage/docs/encryption#customer-supplied).
-
#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.
-
#initialize ⇒ File
constructor
A new instance of 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.
-
#signed_url(method: nil, expires: nil, content_type: nil, content_md5: 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.
-
#to_gs_url ⇒ Object
gs://my-bucket/file-name.json
. -
#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.
Constructor Details
#initialize ⇒ File
Returns a new instance of File.
60 61 62 63 |
# File 'lib/google/cloud/storage/file.rb', line 60 def initialize @service = nil @gapi = Google::Apis::StorageV1::Object.new end |
Instance Attribute Details
#gapi ⇒ Object
56 57 58 |
# File 'lib/google/cloud/storage/file.rb', line 56 def gapi @gapi end |
#service ⇒ Object
52 53 54 |
# File 'lib/google/cloud/storage/file.rb', line 52 def service @service end |
Class Method Details
.from_gapi(gapi, service) ⇒ Object
682 683 684 685 686 687 |
# File 'lib/google/cloud/storage/file.rb', line 682 def self.from_gapi gapi, service new.tap do |f| f.gapi = gapi f.service = service end end |
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.
661 662 663 |
# File 'lib/google/cloud/storage/file.rb', line 661 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.
108 109 110 |
# File 'lib/google/cloud/storage/file.rb', line 108 def api_url @gapi.self_link end |
#bucket ⇒ Object
The name of the Bucket containing this file.
86 87 88 |
# File 'lib/google/cloud/storage/file.rb', line 86 def bucket @gapi.bucket end |
#cache_control ⇒ Object
The [Cache-Control](tools.ietf.org/html/rfc7234#section-5.2) directive for the file data.
161 162 163 |
# File 'lib/google/cloud/storage/file.rb', line 161 def cache_control @gapi.cache_control end |
#cache_control=(cache_control) ⇒ Object
Updates the [Cache-Control](tools.ietf.org/html/rfc7234#section-5.2) directive for the file data.
169 170 171 172 |
# File 'lib/google/cloud/storage/file.rb', line 169 def cache_control= cache_control @gapi.cache_control = cache_control patch_gapi! :cache_control end |
#content_disposition ⇒ Object
The [Content-Disposition](tools.ietf.org/html/rfc6266) of the file data.
177 178 179 |
# File 'lib/google/cloud/storage/file.rb', line 177 def content_disposition @gapi.content_disposition end |
#content_disposition=(content_disposition) ⇒ Object
Updates the [Content-Disposition](tools.ietf.org/html/rfc6266) of the file data.
184 185 186 187 |
# File 'lib/google/cloud/storage/file.rb', line 184 def content_disposition= content_disposition @gapi.content_disposition = content_disposition patch_gapi! :content_disposition end |
#content_encoding ⇒ Object
The [Content-Encoding ](tools.ietf.org/html/rfc7231#section-3.1.2.2) of the file data.
193 194 195 |
# File 'lib/google/cloud/storage/file.rb', line 193 def content_encoding @gapi.content_encoding end |
#content_encoding=(content_encoding) ⇒ Object
Updates the [Content-Encoding ](tools.ietf.org/html/rfc7231#section-3.1.2.2) of the file data.
201 202 203 204 |
# File 'lib/google/cloud/storage/file.rb', line 201 def content_encoding= content_encoding @gapi.content_encoding = content_encoding patch_gapi! :content_encoding end |
#content_language ⇒ Object
The [Content-Language](tools.ietf.org/html/bcp47) of the file data.
209 210 211 |
# File 'lib/google/cloud/storage/file.rb', line 209 def content_language @gapi.content_language end |
#content_language=(content_language) ⇒ Object
Updates the [Content-Language](tools.ietf.org/html/bcp47) of the file data.
216 217 218 219 |
# File 'lib/google/cloud/storage/file.rb', line 216 def content_language= content_language @gapi.content_language = content_language patch_gapi! :content_language end |
#content_type ⇒ Object
The [Content-Type](tools.ietf.org/html/rfc2616#section-14.17) of the file data.
224 225 226 |
# File 'lib/google/cloud/storage/file.rb', line 224 def content_type @gapi.content_type end |
#content_type=(content_type) ⇒ Object
Updates the [Content-Type](tools.ietf.org/html/rfc2616#section-14.17) of the file data.
232 233 234 235 |
# File 'lib/google/cloud/storage/file.rb', line 232 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, encryption_key_sha256: nil) ⇒ Google::Cloud::Storage::File
Copy the file to a new location.
If a [customer-supplied encryption key](cloud.google.com/storage/docs/encryption#customer-supplied) was used with Bucket#create_file, the ‘encryption_key` and `encryption_key_sha256` options must be provided.
461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/google/cloud/storage/file.rb', line 461 def copy dest_bucket_or_path, dest_path = nil, acl: nil, generation: nil, encryption_key: nil, encryption_key_sha256: nil ensure_service! = { acl: acl, generation: generation, key: encryption_key, key_sha256: encryption_key_sha256 } 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](tools.ietf.org/html/rfc4960#appendix-B). Encoded using base64 in big-endian byte order.
148 149 150 |
# File 'lib/google/cloud/storage/file.rb', line 148 def crc32c @gapi.crc32c end |
#created_at ⇒ Object
Creation time of the file.
126 127 128 |
# File 'lib/google/cloud/storage/file.rb', line 126 def created_at @gapi.time_created end |
#delete ⇒ Boolean
Permanently deletes the file.
491 492 493 494 495 |
# File 'lib/google/cloud/storage/file.rb', line 491 def delete ensure_service! service.delete_file bucket, name true end |
#download(path, verify: :md5, encryption_key: nil, encryption_key_sha256: 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](cloud.google.com/storage/docs/encryption#customer-supplied) was used with Bucket#create_file, the ‘encryption_key` and `encryption_key_sha256` options must be provided.
381 382 383 384 385 386 387 388 |
# File 'lib/google/cloud/storage/file.rb', line 381 def download path, verify: :md5, encryption_key: nil, encryption_key_sha256: nil ensure_service! service.download_file \ bucket, name, path, key: encryption_key, key_sha256: encryption_key_sha256 verify_file! ::File.new(path), verify end |
#encryption_key_sha256 ⇒ Object
An [RFC 4648](tools.ietf.org/html/rfc4648#section-4) Base64-encoded string of the SHA256 hash of the [customer-supplied encryption key](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.
263 264 265 266 |
# File 'lib/google/cloud/storage/file.rb', line 263 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.
154 155 156 |
# File 'lib/google/cloud/storage/file.rb', line 154 def etag @gapi.etag end |
#generation ⇒ Object
The content generation of this file. Used for object versioning.
93 94 95 |
# File 'lib/google/cloud/storage/file.rb', line 93 def generation @gapi.generation end |
#id ⇒ Object
The ID of the file.
74 75 76 |
# File 'lib/google/cloud/storage/file.rb', line 74 def id @gapi.id end |
#kind ⇒ Object
The kind of item this is. For files, this is always storage#object.
68 69 70 |
# File 'lib/google/cloud/storage/file.rb', line 68 def kind @gapi.kind end |
#md5 ⇒ Object
MD5 hash of the data; encoded using base64.
140 141 142 |
# File 'lib/google/cloud/storage/file.rb', line 140 def md5 @gapi.md5_hash end |
#media_url ⇒ Object
A URL that can be used to download the file using the REST API.
114 115 116 |
# File 'lib/google/cloud/storage/file.rb', line 114 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.
241 242 243 244 245 |
# File 'lib/google/cloud/storage/file.rb', line 241 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.
251 252 253 254 |
# File 'lib/google/cloud/storage/file.rb', line 251 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.
102 103 104 |
# File 'lib/google/cloud/storage/file.rb', line 102 def @gapi. end |
#name ⇒ Object
The name of this file.
80 81 82 |
# File 'lib/google/cloud/storage/file.rb', line 80 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.
529 530 531 |
# File 'lib/google/cloud/storage/file.rb', line 529 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.
667 668 669 670 |
# File 'lib/google/cloud/storage/file.rb', line 667 def reload! ensure_service! @gapi = service.get_file bucket, name end |
#signed_url(method: nil, expires: nil, content_type: nil, content_md5: 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]( cloud.google.com/storage/docs/authentication#service_accounts).
604 605 606 607 608 609 610 611 612 613 614 |
# File 'lib/google/cloud/storage/file.rb', line 604 def signed_url method: nil, expires: nil, content_type: nil, content_md5: nil, issuer: nil, client_email: nil, signing_key: nil, private_key: nil ensure_service! = { method: method, expires: expires, 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.new self signer.signed_url end |
#size ⇒ Object
Content-Length of the data in bytes.
120 121 122 |
# File 'lib/google/cloud/storage/file.rb', line 120 def size @gapi.size.to_i if @gapi.size end |
#to_gs_url ⇒ Object
gs://my-bucket/file-name.json
.
676 677 678 |
# File 'lib/google/cloud/storage/file.rb', line 676 def to_gs_url "gs://#{bucket}/#{name}" 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.
297 298 299 300 301 302 |
# File 'lib/google/cloud/storage/file.rb', line 297 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.
134 135 136 |
# File 'lib/google/cloud/storage/file.rb', line 134 def updated_at @gapi.updated end |