Class: Google::Cloud::Storage::File

Inherits:
Object
  • Object
show all
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.

Examples:

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"

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!"

See Also:

Direct Known Subclasses

Updater

Defined Under Namespace

Classes: Acl, List, Updater

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#user_projectObject

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.

Examples:

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"


94
95
96
# File 'lib/google/cloud/storage/file.rb', line 94

def user_project
  @user_project
end

Instance Method Details

#aclObject

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.

Examples:

Grant access to a user by prepending "user-" to an email:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"
file = bucket.file "avatars/heidi/400x400.png"

email = "heidi@example.net"
file.acl.add_reader "user-#{email}"

Grant access to a group by prepending "group-" to an email:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"
file = bucket.file "avatars/heidi/400x400.png"

email = "authors@example.net"
file.acl.add_reader "group-#{email}"

Or, grant access via a predefined permissions list:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"
file = bucket.file "avatars/heidi/400x400.png"

file.acl.public!

See Also:



967
968
969
# File 'lib/google/cloud/storage/file.rb', line 967

def acl
  @acl ||= File::Acl.new self
end

#api_urlObject

A URL that can be used to access the file using the REST API.



151
152
153
# File 'lib/google/cloud/storage/file.rb', line 151

def api_url
  @gapi.self_link
end

#bucketObject

The name of the Bucket containing this file.



129
130
131
# File 'lib/google/cloud/storage/file.rb', line 129

def bucket
  @gapi.bucket
end

#cache_controlObject

The Cache-Control directive for the file data.



204
205
206
# File 'lib/google/cloud/storage/file.rb', line 204

def cache_control
  @gapi.cache_control
end

#cache_control=(cache_control) ⇒ Object

Updates the Cache-Control directive for the file data.



212
213
214
215
# File 'lib/google/cloud/storage/file.rb', line 212

def cache_control= cache_control
  @gapi.cache_control = cache_control
  update_gapi! :cache_control
end

#content_dispositionObject

The Content-Disposition of the file data.



220
221
222
# File 'lib/google/cloud/storage/file.rb', line 220

def content_disposition
  @gapi.content_disposition
end

#content_disposition=(content_disposition) ⇒ Object

Updates the Content-Disposition of the file data.



227
228
229
230
# File 'lib/google/cloud/storage/file.rb', line 227

def content_disposition= content_disposition
  @gapi.content_disposition = content_disposition
  update_gapi! :content_disposition
end

#content_encodingObject

The Content-Encoding of the file data.



236
237
238
# File 'lib/google/cloud/storage/file.rb', line 236

def content_encoding
  @gapi.content_encoding
end

#content_encoding=(content_encoding) ⇒ Object

Updates the Content-Encoding of the file data.



244
245
246
247
# File 'lib/google/cloud/storage/file.rb', line 244

def content_encoding= content_encoding
  @gapi.content_encoding = content_encoding
  update_gapi! :content_encoding
end

#content_languageObject

The Content-Language of the file data.



252
253
254
# File 'lib/google/cloud/storage/file.rb', line 252

def content_language
  @gapi.content_language
end

#content_language=(content_language) ⇒ Object

Updates the Content-Language of the file data.



259
260
261
262
# File 'lib/google/cloud/storage/file.rb', line 259

def content_language= content_language
  @gapi.content_language = content_language
  update_gapi! :content_language
end

#content_typeObject

The Content-Type of the file data.



267
268
269
# File 'lib/google/cloud/storage/file.rb', line 267

def content_type
  @gapi.content_type
end

#content_type=(content_type) ⇒ Object

Updates the Content-Type of the file data.



275
276
277
278
# File 'lib/google/cloud/storage/file.rb', line 275

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.

Examples:

The file can be copied to a new path in the current bucket:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

file = bucket.file "path/to/my-file.ext"
file.copy "path/to/destination/file.ext"

The file can also be copied to a different bucket:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

file = bucket.file "path/to/my-file.ext"
file.copy "new-destination-bucket",
          "path/to/destination/file.ext"

The file can also be copied by specifying a generation:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

file = bucket.file "path/to/my-file.ext"
file.copy "copy/of/previous/generation/file.ext",
          generation: 123456

The file can be modified during copying:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

file = bucket.file "path/to/my-file.ext"
file.copy "new-destination-bucket",
          "path/to/destination/file.ext" do |f|
  f.["copied_from"] = "#{file.bucket}/#{file.name}"
end

Parameters:

  • dest_bucket_or_path (String)

    Either the bucket to copy the file to, or the path to copy the file to in the current bucket.

  • dest_path (String) (defaults to: nil)

    If a bucket was provided in the first parameter, this contains the path to copy the file to in the given bucket.

  • acl (String)

    A predefined set of access controls to apply to new file.

    Acceptable values are:

    • auth, auth_read, authenticated, authenticated_read, authenticatedRead - File owner gets OWNER access, and allAuthenticatedUsers get READER access.
    • owner_full, bucketOwnerFullControl - File owner gets OWNER access, and project team owners get OWNER access.
    • owner_read, bucketOwnerRead - File owner gets OWNER access, and project team owners get READER access.
    • private - File owner gets OWNER access.
    • project_private, projectPrivate - File owner gets OWNER access, and project team members get access according to their roles.
    • public, public_read, publicRead - File owner gets OWNER access, and allUsers get READER access.
  • generation (Integer)

    Select a specific revision of the file to copy. The default is the latest version.

  • encryption_key (String)

    Optional. The customer-supplied, AES-256 encryption key used to encrypt the file, if one was provided to Bucket#create_file.

Yields:

  • (file)

    a block yielding a delegate object for updating

Returns:



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
# File 'lib/google/cloud/storage/file.rb', line 649

def copy dest_bucket_or_path, dest_path = nil, acl: nil,
         generation: nil, encryption_key: nil
  ensure_service!
  options = { acl: acl, generation: generation, key: encryption_key,
              user_project: user_project }
  dest_bucket, dest_path, options = fix_copy_args dest_bucket_or_path,
                                                  dest_path, options

  copy_gapi = nil
  if block_given?
    updater = Updater.new gapi
    yield updater
    updater.check_for_changed_metadata!
    copy_gapi = gapi_from_attrs(updater.updates) if updater.updates.any?
  end

  resp = service.copy_file bucket, name, dest_bucket, dest_path,
                           copy_gapi, options
  until resp.done
    sleep 1
    resp = service.copy_file bucket, name, dest_bucket, dest_path,
                             copy_gapi,
                             options.merge(token: resp.rewrite_token)
  end
  File.from_gapi resp.resource, service, user_project: user_project
end

#crc32cObject

The CRC32c checksum of the data, as described in RFC 4960, Appendix B. Encoded using base64 in big-endian byte order.



191
192
193
# File 'lib/google/cloud/storage/file.rb', line 191

def crc32c
  @gapi.crc32c
end

#created_atObject

Creation time of the file.



169
170
171
# File 'lib/google/cloud/storage/file.rb', line 169

def created_at
  @gapi.time_created
end

#delete(generation: nil) ⇒ Boolean

Permanently deletes the file.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

file = bucket.file "path/to/my-file.ext"
file.delete

The file's generation can used by passing true:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

file = bucket.file "path/to/my-file.ext"
file.delete generation: true

A generation can also be specified:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

file = bucket.file "path/to/my-file.ext"
file.delete generation: 123456

Parameters:

  • generation (Boolean, Integer)

    Specify a version of the file to delete. When true, it will delete the version returned by #generation. The default behavior is to delete the latest version of the file (regardless of the version to which the file is set, which is the version returned by #generation.)

Returns:

  • (Boolean)

    Returns true if the file was deleted.



775
776
777
778
779
780
781
# File 'lib/google/cloud/storage/file.rb', line 775

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, skip_decompress: nil) ⇒ IO

Download the file's contents to a local file or an IO instance.

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.

Examples:

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"

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

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

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

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!"

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!"

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

Parameters:

  • path (String, IO) (defaults to: nil)

    The path on the local file system to write the data to. The path provided must be writable. Can also be an IO object, or IO-ish object like StringIO. If an IO object, the object will be written to, not the filesystem. If omitted, a new StringIO instance will be written to and returned. Optional.

  • verify (Symbol)

    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.
  • encryption_key (String)

    Optional. The customer-supplied, AES-256 encryption key used to encrypt the file, if one was provided to Bucket#create_file.

  • skip_decompress (Boolean)

    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)

Returns:

  • (IO)

    Returns an IO 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 IO object, then that object will be returned.



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/google/cloud/storage/file.rb', line 545

def download path = nil, verify: :md5, encryption_key: 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, user_project: user_project
  # FIX: downloading with encryption key will return nil
  file ||= ::File.new(path)
  verify_file! file, verify
  if !skip_decompress &&
     Array(resp.header["Content-Encoding"]).include?("gzip")
    file = gzip_decompress file
  end
  file
end

#encryption_key_sha256Object

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.



306
307
308
309
# File 'lib/google/cloud/storage/file.rb', line 306

def encryption_key_sha256
  return nil unless @gapi.customer_encryption
  Base64.decode64 @gapi.customer_encryption.key_sha256
end

#etagObject

HTTP 1.1 Entity tag for the file.



197
198
199
# File 'lib/google/cloud/storage/file.rb', line 197

def etag
  @gapi.etag
end

#exists?Boolean

Determines whether the file exists in the Storage service.

Returns:

  • (Boolean)


1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
# File 'lib/google/cloud/storage/file.rb', line 1023

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

#generationObject

The content generation of this file. Used for object versioning.



136
137
138
# File 'lib/google/cloud/storage/file.rb', line 136

def generation
  @gapi.generation
end

#generationsArray<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)

Examples:

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

Returns:

See Also:



369
370
371
372
373
374
375
376
# File 'lib/google/cloud/storage/file.rb', line 369

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

#idObject

The ID of the file.



117
118
119
# File 'lib/google/cloud/storage/file.rb', line 117

def id
  @gapi.id
end

#kindObject

The kind of item this is. For files, this is always storage#object.



111
112
113
# File 'lib/google/cloud/storage/file.rb', line 111

def kind
  @gapi.kind
end

#md5Object

MD5 hash of the data; encoded using base64.



183
184
185
# File 'lib/google/cloud/storage/file.rb', line 183

def md5
  @gapi.md5_hash
end

#media_urlObject

A URL that can be used to download the file using the REST API.



157
158
159
# File 'lib/google/cloud/storage/file.rb', line 157

def media_url
  @gapi.media_link
end

#metadataObject

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.



284
285
286
287
288
# File 'lib/google/cloud/storage/file.rb', line 284

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.



294
295
296
297
# File 'lib/google/cloud/storage/file.rb', line 294

def metadata= 
  @gapi. = 
  update_gapi! :metadata
end

#metagenerationObject

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.



145
146
147
# File 'lib/google/cloud/storage/file.rb', line 145

def metageneration
  @gapi.metageneration
end

#nameObject

The name of this file.



123
124
125
# File 'lib/google/cloud/storage/file.rb', line 123

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.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"
file = bucket.file "avatars/heidi/400x400.png"
public_url = file.public_url

Generate the URL with a protocol other than HTTPS:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"
file = bucket.file "avatars/heidi/400x400.png"
public_url = file.public_url protocol: "http"

Parameters:

  • protocol (String)

    The protocol to use for the URL. Default is HTTPS.

See Also:



813
814
815
# File 'lib/google/cloud/storage/file.rb', line 813

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.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

file = bucket.file "path/to/my-file.ext"
file.reload!

The file's generation can used by passing true:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

file = bucket.file "path/to/my-file.ext", generation: 123456
file.reload! generation: true

A generation can also be specified:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

file = bucket.file "path/to/my-file.ext", generation: 123456
file.reload! generation: 123457

Parameters:

  • generation (Boolean, Integer)

    Specify a version of the file to reload with. When true, it will reload the version returned by #generation. The default behavior is to reload with the latest version of the file (regardless of the version to which the file is set, which is the version returned by #generation.)



1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/google/cloud/storage/file.rb', line 1010

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

#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.

Examples:

The file will be rewritten with a new encryption key:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new
bucket = storage.bucket "my-bucket"

# Old key was stored securely for later use.
old_key = "y\x03\"\x0E\xB6\xD3\x9B\x0E\xAB*\x19\xFAv\xDEY\xBEI..."

file = bucket.file "path/to/my-file.ext", encryption_key: old_key

# Key generation shown for example purposes only. Write your own.
cipher = OpenSSL::Cipher.new "aes-256-cfb"
cipher.encrypt
new_key = cipher.random_key

file.rotate encryption_key: old_key, new_encryption_key: new_key

Parameters:

  • encryption_key (String, nil)

    Optional. The last customer-supplied, AES-256 encryption key used to encrypt the file, if one was used.

  • new_encryption_key (String, nil)

    Optional. The new customer-supplied, AES-256 encryption key with which to encrypt the file. If nil, the rewritten file will be encrypted using the default server-side encryption, not customer-supplied encryption keys.

Returns:

See Also:



721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/google/cloud/storage/file.rb', line 721

def rotate encryption_key: nil, new_encryption_key: nil
  ensure_service!
  options = { source_key: encryption_key,
              destination_key: new_encryption_key,
              user_project: user_project }
  gapi = service.rewrite_file bucket, name, bucket, name, nil, options
  until gapi.done
    sleep 1
    options[:token] = gapi.rewrite_token
    gapi = service.rewrite_file bucket, name, bucket, name, nil, options
  end
  File.from_gapi gapi.resource, service, user_project: user_project
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) ⇒ 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.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"
file = bucket.file "avatars/heidi/400x400.png"
shared_url = file.signed_url

Any of the option parameters may be specified:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"
file = bucket.file "avatars/heidi/400x400.png"
shared_url = file.signed_url method: "PUT",
                             content_type: "image/png",
                             expires: 300 # 5 minutes from now

Using the issuer and signing_key options:

require "google/cloud/storage"

storage = Google::Cloud.storage

bucket = storage.bucket "my-todo-app"
file = bucket.file "avatars/heidi/400x400.png"
key = OpenSSL::PKey::RSA.new "-----BEGIN PRIVATE KEY-----\n..."
shared_url = file.signed_url issuer: "service-account@gcloud.com",
                             signing_key: key

Using the headers option:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"
file = bucket.file "avatars/heidi/400x400.png"
shared_url = file.signed_url method: "GET",
                             headers: {
                               "x-goog-acl" => "public-read",
                               "x-goog-meta-foo" => "bar,baz"
                             }

Parameters:

  • method (String)

    The HTTP verb to be used with the signed URL. Signed URLs can be used with GET, HEAD, PUT, and DELETE requests. Default is GET.

  • expires (Integer)

    The number of seconds until the URL expires. Default is 300/5 minutes.

  • content_type (String)

    When provided, the client (browser) must send this value in the HTTP header. e.g. text/plain

  • content_md5 (String)

    The MD5 digest value in base64. If you provide this in the string, the client (usually a browser) must provide this HTTP header with this same value in its request.

  • headers (Hash)

    Google extension headers (custom HTTP headers that begin with x-goog-) that must be included in requests that use the signed URL.

  • issuer (String)

    Service Account's Client Email.

  • client_email (String)

    Service Account's Client Email.

  • signing_key (OpenSSL::PKey::RSA, String)

    Service Account's Private Key.

  • private_key (OpenSSL::PKey::RSA, String)

    Service Account's Private Key.

  • query (Hash)

    Query string parameters to include in the signed URL. The given parameters are not verified by the signature.

    Parameters such as response-content-disposition and response-content-type can alter the behavior of the response when using the URL, but only when the file resource is missing the corresponding values. (These values can be permanently set using #content_disposition= and #content_type=.)

See Also:



911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'lib/google/cloud/storage/file.rb', line 911

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

#sizeObject

Content-Length of the data in bytes.



163
164
165
# File 'lib/google/cloud/storage/file.rb', line 163

def size
  @gapi.size.to_i if @gapi.size
end

#storage_classObject

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.



318
319
320
# File 'lib/google/cloud/storage/file.rb', line 318

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. Accepted values include :multi_regional, :regional, :nearline, and :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.

Parameters:

  • storage_class (Symbol, String)

    Storage class of the file.



334
335
336
337
# File 'lib/google/cloud/storage/file.rb', line 334

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.

Examples:

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.["player"] = "Bob"
  f.["score"] = "10"
end

Yields:

  • (file)

    a block yielding a delegate object for updating the file



406
407
408
409
410
411
# File 'lib/google/cloud/storage/file.rb', line 406

def update
  updater = Updater.new gapi
  yield updater
  updater.check_for_changed_metadata!
  update_gapi! updater.updates unless updater.updates.empty?
end

#updated_atObject

The creation or modification time of the file. For buckets with versioning enabled, changing an object's metadata does not change this property.



177
178
179
# File 'lib/google/cloud/storage/file.rb', line 177

def updated_at
  @gapi.updated
end