Class: Google::Cloud::Storage::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/storage/bucket.rb,
lib/google/cloud/storage/bucket/acl.rb,
lib/google/cloud/storage/bucket/cors.rb,
lib/google/cloud/storage/bucket/list.rb

Overview

Bucket

Represents a Storage bucket. Belongs to a Project and has many Files.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

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

Direct Known Subclasses

Updater

Defined Under Namespace

Classes: Acl, Cors, DefaultAcl, List, Updater

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#user_projectObject

A boolean value or a project ID string to indicate the project to be billed for operations on the bucket and its files. If this attribute is set to true, transit costs for operations on the bucket 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 bucket by providing the user_project option to Project#bucket.

See also #requester_pays= and #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
files = bucket.files # Billed to current project
bucket.user_project = "my-other-project"
files = bucket.files # Billed to "my-other-project"


76
77
78
# File 'lib/google/cloud/storage/bucket.rb', line 76

def user_project
  @user_project
end

Instance Method Details

#aclBucket::Acl

The Acl instance used to control access to the bucket.

A bucket 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"

email = "heidi@example.net"
bucket.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"

email = "authors@example.net"
bucket.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"

bucket.acl.public!

Returns:

See Also:



1230
1231
1232
# File 'lib/google/cloud/storage/bucket.rb', line 1230

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

#api_urlString

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

Returns:

  • (String)


119
120
121
# File 'lib/google/cloud/storage/bucket.rb', line 119

def api_url
  @gapi.self_link
end

#compose(sources, destination, acl: nil, encryption_key: nil) {|file| ... } ⇒ Google::Cloud::Storage::File Also known as: compose_file, combine

Concatenates a list of existing files in the bucket into a new file in the bucket. There is a limit (currently 32) to the number of files that can be composed in a single operation.

To compose files encrypted with a customer-supplied encryption key, use the encryption_key option. All source files must have been encrypted with the same key, and the resulting destination file will also be encrypted with the same key.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

sources = ["path/to/my-file-1.ext", "path/to/my-file-2.ext"]

new_file = bucket.compose sources, "path/to/new-file.ext"

Set the properties of the new file in a block:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

sources = ["path/to/my-file-1.ext", "path/to/my-file-2.ext"]

new_file = bucket.compose sources, "path/to/new-file.ext" 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"
end

Specify the generation of source files (but skip retrieval):

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

file_1 = bucket.file "path/to/my-file-1.ext",
                     generation: 1490390259479000, skip_lookup: true
file_2 = bucket.file "path/to/my-file-2.ext",
                     generation: 1490310974144000, skip_lookup: true

new_file = bucket.compose [file_1, file_2], "path/to/new-file.ext"

Parameters:

  • sources (Array<String, Google::Cloud::Storage::File>)

    The list of source file names or objects that will be concatenated into a single file.

  • destination (String)

    The name of the new file.

  • acl (String)

    A predefined set of access controls to apply to this 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.
  • encryption_key (String, nil)

    Optional. The customer-supplied, AES-256 encryption key used to encrypt the source files, if one was used. All source files must have been encrypted with the same key, and the resulting destination file will also be encrypted with the key.

Yields:

  • (file)

    A block yielding a delegate file object for setting the properties of the destination file.

Returns:



948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
# File 'lib/google/cloud/storage/bucket.rb', line 948

def compose sources, destination, acl: nil, encryption_key: nil
  ensure_service!
  sources = Array sources
  if sources.size < 2
    raise ArgumentError, "must provide at least two source files"
  end

  options = { acl: File::Acl.predefined_rule_for(acl),
              key: encryption_key,
              user_project: user_project }
  destination_gapi = nil
  if block_given?
    destination_gapi = Google::Apis::StorageV1::Object.new
    updater = File::Updater.new destination_gapi
    yield updater
    updater.check_for_changed_metadata!
  end
  gapi = service.compose_file name, sources, destination,
                              destination_gapi, options
  File.from_gapi gapi, service, user_project: user_project
end

#cors {|cors| ... } ⇒ Bucket::Cors

Returns the current CORS configuration for a static website served from the bucket.

The return value is a frozen (unmodifiable) array of hashes containing the attributes specified for the Bucket resource field cors.

This method also accepts a block for updating the bucket's CORS rules. See Cors for details.

Examples:

Retrieving the bucket's CORS rules.

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"
bucket.cors.size #=> 2
rule = bucket.cors.first
rule.origin #=> ["http://example.org"]
rule.methods #=> ["GET","POST","DELETE"]
rule.headers #=> ["X-My-Custom-Header"]
rule.max_age #=> 3600

Updating the bucket's CORS rules inside a block.

require "google/cloud/storage"

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

bucket.update do |b|
  b.cors do |c|
    c.add_rule ["http://example.org", "https://example.org"],
               "*",
               headers: ["X-My-Custom-Header"],
               max_age: 3600
  end
end

Yields:

  • (cors)

    a block for setting CORS rules

Yield Parameters:

Returns:

See Also:



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/google/cloud/storage/bucket.rb', line 179

def cors
  cors_builder = Bucket::Cors.from_gapi @gapi.cors_configurations
  if block_given?
    yield cors_builder
    if cors_builder.changed?
      @gapi.cors_configurations = cors_builder.to_gapi
      patch_gapi! :cors_configurations
    end
  end
  cors_builder.freeze # always return frozen objects
end

#create_file(file, path = nil, acl: nil, cache_control: nil, content_disposition: nil, content_encoding: nil, content_language: nil, content_type: nil, crc32c: nil, md5: nil, metadata: nil, storage_class: nil, encryption_key: nil, kms_key: nil) ⇒ Google::Cloud::Storage::File Also known as: upload_file, new_file

Creates a new File object by providing a path to a local file (or any File-like object such as StringIO) to upload, along with the path at which to store it in the bucket.

Customer-supplied encryption keys

By default, Google Cloud Storage manages server-side encryption keys on your behalf. However, a customer-supplied encryption key can be provided with the encryption_key option. If given, the same key must be provided to subsequently download or copy the file. If you use customer-supplied encryption keys, 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:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.create_file "path/to/local.file.ext"

Specifying a destination path:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.create_file "path/to/local.file.ext",
                   "destination/path/file.ext"

Providing a customer-supplied encryption key:

require "google/cloud/storage"

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

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

bucket.create_file "path/to/local.file.ext",
                   "destination/path/file.ext",
                   encryption_key: key

# Store your key and hash securely for later use.
file = bucket.file "destination/path/file.ext",
                   encryption_key: key

Providing a customer-managed Cloud KMS encryption key:

require "google/cloud/storage"

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

# KMS key ring must use the same location as the bucket.
kms_key_name = "projects/a/locations/b/keyRings/c/cryptoKeys/d"

bucket.create_file "path/to/local.file.ext",
                   "destination/path/file.ext",
                   kms_key: kms_key_name

file = bucket.file "destination/path/file.ext"
file.kms_key #=> kms_key_name

Create a file with gzip-encoded 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:

  • file (String, ::File)

    Path of the file on the filesystem to upload. Can be an File object, or File-like object such as StringIO. (If the object does not have path, a path argument must be also be provided.)

  • path (String) (defaults to: nil)

    Path to store the file in Google Cloud Storage.

  • acl (String)

    A predefined set of access controls to apply to this 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.
  • cache_control (String)

    The Cache-Control response header to be returned when the file is downloaded.

  • content_disposition (String)

    The Content-Disposition response header to be returned when the file is downloaded.

  • content_encoding (String)

    The Content-Encoding response header to be returned when the file is downloaded. For example, content_encoding: "gzip" can indicate to clients that the uploaded data is gzip-compressed. However, there is no check to guarantee the specified Content-Encoding has actually been applied to the file data, and incorrectly specifying the file's encoding could lead to unintended behavior on subsequent download requests.

  • content_language (String)

    The Content-Language response header to be returned when the file is downloaded.

  • content_type (String)

    The Content-Type response header to be returned when the file is downloaded.

  • crc32c (String)

    The CRC32c checksum of the file data, as described in RFC 4960, Appendix B. If provided, Cloud Storage will only create the file if the value matches the value calculated by the service. See Validation for more information.

  • md5 (String)

    The MD5 hash of the file data. If provided, Cloud Storage will only create the file if the value matches the value calculated by the service. See Validation for more information.

  • metadata (Hash)

    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.

  • storage_class (Symbol, String)

    Storage class of the file. Determines 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. For more information, see Storage Classes and Per-Object Storage Class. The default value is the default storage class for the bucket.

  • encryption_key (String)

    Optional. A customer-supplied, AES-256 encryption key that will be used to encrypt the file. Do not provide if kms_key is used.

  • kms_key (String)

    Optional. Resource name of the Cloud KMS key, of the form projects/my-prj/locations/kr-loc/keyRings/my-kr/cryptoKeys/my-key, that will be used to encrypt the file. The KMS key ring must use the same location as the bucket.The Service Account associated with your project requires access to this encryption key. Do not provide if encryption_key is used.

Returns:

Raises:

  • (ArgumentError)


837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
# File 'lib/google/cloud/storage/bucket.rb', line 837

def create_file file, path = nil, acl: nil, cache_control: nil,
                content_disposition: nil, content_encoding: nil,
                content_language: nil, content_type: nil,
                crc32c: nil, md5: nil, metadata: nil,
                storage_class: nil, encryption_key: nil, kms_key: nil
  ensure_service!
  options = { acl: File::Acl.predefined_rule_for(acl), md5: md5,
              cache_control: cache_control, content_type: content_type,
              content_disposition: content_disposition, crc32c: crc32c,
              content_encoding: content_encoding, metadata: ,
              content_language: content_language, key: encryption_key,
              kms_key: kms_key,
              storage_class: storage_class_for(storage_class),
              user_project: user_project }
  ensure_io_or_file_exists! file
  path ||= file.path if file.respond_to? :path
  path ||= file if file.is_a? String
  raise ArgumentError, "must provide path" if path.nil?

  gapi = service.insert_file name, file, path, options
  File.from_gapi gapi, service, user_project: user_project
end

#create_notification(topic, custom_attrs: nil, event_types: nil, prefix: nil, payload: nil) ⇒ Google::Cloud::Storage::Notification Also known as: new_notification

Creates a new Pub/Sub notification subscription for the bucket.

Examples:

require "google/cloud/pubsub"
require "google/cloud/storage"

pubsub = Google::Cloud::Pubsub.new
topic = pubsub.create_topic "my-topic"
topic.policy do |p|
  p.add "roles/pubsub.publisher",
        "serviceAccount:my-project" \
        "@gs-project-accounts.iam.gserviceaccount.com"
end

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

notification = bucket.create_notification topic.name

Parameters:

  • topic (String)

    The name of the Cloud PubSub topic to which the notification subscription will publish.

  • custom_attrs (Hash(String => String))

    The custom attributes for the notification. An optional list of additional attributes to attach to each Cloud Pub/Sub message published for the notification subscription.

  • event_types (Symbol, String, Array<Symbol, String>)

    The event types for the notification subscription. If provided, messages will only be sent for the listed event types. If empty, messages will be sent for all event types.

    Acceptable values are:

    • :finalize - Sent when a new object (or a new generation of an existing object) is successfully created in the bucket. This includes copying or rewriting an existing object. A failed upload does not trigger this event.
    • :update - Sent when the metadata of an existing object changes.
    • :delete - Sent when an object has been permanently deleted. This includes objects that are overwritten or are deleted as part of the bucket's lifecycle configuration. For buckets with object versioning enabled, this is not sent when an object is archived (see OBJECT_ARCHIVE), even if archival occurs via the File#delete method.
    • :archive - Only sent when the bucket has enabled object versioning. This event indicates that the live version of an object has become an archived version, either because it was archived or because it was overwritten by the upload of an object of the same name.
  • prefix (String)

    The file name prefix for the notification subscription. If provided, the notification will only be applied to file names that begin with this prefix.

  • payload (Symbol, String, Boolean)

    The desired content of the Pub/Sub message payload. Acceptable values are:

    • :json or true - The Pub/Sub message payload will be a UTF-8 string containing the resource representation of the file's metadata.
    • :none or false - No payload is included with the notification.

    The default value is :json.

Returns:

See Also:



1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
# File 'lib/google/cloud/storage/bucket.rb', line 1534

def create_notification topic, custom_attrs: nil, event_types: nil,
                        prefix: nil, payload: nil
  ensure_service!
  options = { custom_attrs: custom_attrs, event_types: event_types,
              prefix: prefix, payload: payload,
              user_project: user_project }

  gapi = service.insert_notification name, topic, options
  Notification.from_gapi name, gapi, service, user_project: user_project
end

#created_atDateTime

Creation time of the bucket.

Returns:

  • (DateTime)


128
129
130
# File 'lib/google/cloud/storage/bucket.rb', line 128

def created_at
  @gapi.time_created
end

#default_aclBucket::DefaultAcl

The DefaultAcl instance used to control access to the bucket's files.

A bucket's files have 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"

email = "heidi@example.net"
bucket.default_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"

email = "authors@example.net"
bucket.default_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"

bucket.default_acl.public!

Returns:

See Also:



1276
1277
1278
# File 'lib/google/cloud/storage/bucket.rb', line 1276

def default_acl
  @default_acl ||= Bucket::DefaultAcl.new self
end

#default_kms_keyString?

The Cloud KMS encryption key that will be used to protect files. For example: projects/a/locations/b/keyRings/c/cryptoKeys/d

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

# KMS key ring must use the same location as the bucket.
kms_key_name = "projects/a/locations/b/keyRings/c/cryptoKeys/d"
bucket.default_kms_key = kms_key_name

bucket.default_kms_key #=> kms_key_name

Returns:

  • (String, nil)

    A Cloud KMS encryption key, or nil if none has been configured.



450
451
452
# File 'lib/google/cloud/storage/bucket.rb', line 450

def default_kms_key
  @gapi.encryption && @gapi.encryption.default_kms_key_name
end

#default_kms_key=(new_default_kms_key) ⇒ Object

Set the Cloud KMS encryption key that will be used to protect files. For example: projects/a/locations/b/keyRings/c/cryptoKeys/d

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

# KMS key ring must use the same location as the bucket.
kms_key_name = "projects/a/locations/b/keyRings/c/cryptoKeys/d"

bucket.default_kms_key = kms_key_name

Parameters:

  • new_default_kms_key (String)

    New Cloud KMS key name.



472
473
474
475
476
# File 'lib/google/cloud/storage/bucket.rb', line 472

def default_kms_key= new_default_kms_key
  @gapi.encryption = Google::Apis::StorageV1::Bucket::Encryption.new \
    default_kms_key_name: new_default_kms_key
  patch_gapi! :encryption
end

#deleteBoolean

Permanently deletes the bucket. The bucket must be empty before it can be deleted.

The API call to delete the bucket may be retried under certain conditions. See Google::Cloud#storage to control this behavior.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"
bucket.delete

Returns:

  • (Boolean)

    Returns true if the bucket was deleted.



545
546
547
548
549
# File 'lib/google/cloud/storage/bucket.rb', line 545

def delete
  ensure_service!
  service.delete_bucket name, user_project: user_project
  true
end

#exists?Boolean

Determines whether the bucket exists in the Storage service.

Returns:

  • (Boolean)

    true if the bucket exists in the Storage service.



1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
# File 'lib/google/cloud/storage/bucket.rb', line 1563

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

#file(path, generation: nil, skip_lookup: nil, encryption_key: nil) ⇒ Google::Cloud::Storage::File? Also known as: find_file

Retrieves a file matching the path.

If a customer-supplied encryption key was used with #create_file, the encryption_key option must be provided or else the file's CRC32C checksum and MD5 hash will not be returned.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

file = bucket.file "path/to/my-file.ext"
puts file.name

Parameters:

  • path (String)

    Name (path) of the file.

  • generation (Integer)

    When present, selects a specific revision of this object. Default is the latest version.

  • skip_lookup (Boolean)

    Optionally create a Bucket object without verifying the bucket resource exists on the Storage service. Calls made on this object will raise errors if the bucket resource does not exist. Default is false.

  • encryption_key (String)

    Optional. The customer-supplied, AES-256 encryption key used to encrypt the file, if one was provided to #create_file. (Not used if skip_lookup is also set.)

Returns:



643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/google/cloud/storage/bucket.rb', line 643

def file path, generation: nil, skip_lookup: nil, encryption_key: nil
  ensure_service!
  if skip_lookup
    return File.new_lazy name, path, service,
                         generation: generation,
                         user_project: user_project
  end
  gapi = service.get_file name, path, generation: generation,
                                      key: encryption_key,
                                      user_project: user_project
  File.from_gapi gapi, service, user_project: user_project
rescue Google::Cloud::NotFoundError
  nil
end

#files(prefix: nil, delimiter: nil, token: nil, max: nil, versions: nil) ⇒ Array<Google::Cloud::Storage::File> Also known as: find_files

Retrieves a list of files matching the criteria.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"
files = bucket.files
files.each do |file|
  puts file.name
end

Retrieve all files: (See File::List#all)

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"
files = bucket.files
files.all do |file|
  puts file.name
end

Parameters:

  • prefix (String)

    Filter results to files whose names begin with this prefix.

  • delimiter (String)

    Returns results in a directory-like mode. items will contain only objects whose names, aside from the prefix, do not contain delimiter. Objects whose names, aside from the prefix, contain delimiter will have their name, truncated after the delimiter, returned in prefixes. Duplicate prefixes are omitted.

  • token (String)

    A previously-returned page token representing part of the larger set of results to view.

  • max (Integer)

    Maximum number of items plus prefixes to return. As duplicate prefixes are omitted, fewer total results may be returned than requested. The default value of this parameter is 1,000 items.

  • versions (Boolean)

    If true, lists all versions of an object as distinct results. The default is false. For more information, see Object Versioning .

Returns:



598
599
600
601
602
603
604
605
606
607
# File 'lib/google/cloud/storage/bucket.rb', line 598

def files prefix: nil, delimiter: nil, token: nil, max: nil,
          versions: nil
  ensure_service!
  gapi = service.list_files name, prefix: prefix, delimiter: delimiter,
                                  token: token, max: max,
                                  versions: versions,
                                  user_project: user_project
  File::List.from_gapi gapi, service, name, prefix, delimiter, max,
                       versions, user_project: user_project
end

#idString

The ID of the bucket.

Returns:

  • (String)


101
102
103
# File 'lib/google/cloud/storage/bucket.rb', line 101

def id
  @gapi.id
end

#kindString

The kind of item this is. For buckets, this is always storage#bucket.

Returns:

  • (String)


92
93
94
# File 'lib/google/cloud/storage/bucket.rb', line 92

def kind
  @gapi.kind
end

#labelsHash(String => String)

A hash of user-provided labels. The hash is frozen and changes are not allowed.

Returns:

  • (Hash(String => String))


359
360
361
362
363
# File 'lib/google/cloud/storage/bucket.rb', line 359

def labels
  m = @gapi.labels
  m = m.to_h if m.respond_to? :to_h
  m.dup.freeze
end

#labels=(labels) ⇒ Object

Updates the hash of user-provided labels.

Parameters:

  • labels (Hash(String => String))

    The user-provided labels.



370
371
372
373
# File 'lib/google/cloud/storage/bucket.rb', line 370

def labels= labels
  @gapi.labels = labels
  patch_gapi! :labels
end

#locationString

The location of the bucket. Object data for objects in the bucket resides in physical storage within this region. Defaults to US. See the developer's guide for the authoritative list.



201
202
203
# File 'lib/google/cloud/storage/bucket.rb', line 201

def location
  @gapi.location
end

#logging_bucketString

The destination bucket name for the bucket's logs.

Returns:

  • (String)

See Also:



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

def logging_bucket
  @gapi.logging.log_bucket if @gapi.logging
end

#logging_bucket=(logging_bucket) ⇒ Object

Updates the destination bucket for the bucket's logs.

Parameters:

  • logging_bucket (String)

    The bucket to hold the logging output

See Also:



223
224
225
226
227
# File 'lib/google/cloud/storage/bucket.rb', line 223

def logging_bucket= logging_bucket
  @gapi.logging ||= Google::Apis::StorageV1::Bucket::Logging.new
  @gapi.logging.log_bucket = logging_bucket
  patch_gapi! :logging
end

#logging_prefixString

The logging object prefix for the bucket's logs. For more information,

Returns:

  • (String)

See Also:



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

def logging_prefix
  @gapi.logging.log_object_prefix if @gapi.logging
end

#logging_prefix=(logging_prefix) ⇒ Object

Updates the logging object prefix. This prefix will be used to create log object names for the bucket. It can be at most 900 characters and must be a valid object name. By default, the object prefix is the name of the bucket for which the logs are enabled.

Parameters:

  • logging_prefix (String)

    The logging object prefix.

See Also:



252
253
254
255
256
# File 'lib/google/cloud/storage/bucket.rb', line 252

def logging_prefix= logging_prefix
  @gapi.logging ||= Google::Apis::StorageV1::Bucket::Logging.new
  @gapi.logging.log_object_prefix = logging_prefix
  patch_gapi! :logging
end

#nameString

The name of the bucket.

Returns:

  • (String)


110
111
112
# File 'lib/google/cloud/storage/bucket.rb', line 110

def name
  @gapi.name
end

#notification(id) ⇒ Google::Cloud::Storage::Notification? Also known as: find_notification

Retrieves a Pub/Sub notification subscription for the bucket.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

notification = bucket.notification "1"
puts notification.id

Parameters:

  • id (String)

    The Notification ID.

Returns:

See Also:



1456
1457
1458
1459
1460
1461
1462
# File 'lib/google/cloud/storage/bucket.rb', line 1456

def notification id
  ensure_service!
  gapi = service.get_notification name, id, user_project: user_project
  Notification.from_gapi name, gapi, service, user_project: user_project
rescue Google::Cloud::NotFoundError
  nil
end

#notificationsArray<Google::Cloud::Storage::Notification> Also known as: find_notifications

Retrieves the entire list of Pub/Sub notification subscriptions for the bucket.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"
notifications = bucket.notifications
notifications.each do |notification|
  puts notification.id
end

Returns:

See Also:



1425
1426
1427
1428
1429
1430
1431
1432
# File 'lib/google/cloud/storage/bucket.rb', line 1425

def notifications
  ensure_service!
  gapi = service.list_notifications name, user_project: user_project
  Array(gapi.items).map do |gapi_object|
    Notification.from_gapi name, gapi_object, service,
                           user_project: user_project
  end
end

#policy(force: nil) {|policy| ... } ⇒ Policy

Gets and updates the Cloud IAM access control policy for this bucket.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"

policy = bucket.policy

Retrieve the latest policy and update it in a block:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"

bucket.policy do |p|
  p.add "roles/owner", "user:owner@example.com"
end

Parameters:

  • force (Boolean)

    [Deprecated] Force the latest policy to be retrieved from the Storage service when true. Deprecated because the latest policy is now always retrieved. The default is nil.

Yields:

  • (policy)

    A block for updating the policy. The latest policy will be read from the service and passed to the block. After the block completes, the modified policy will be written to the service.

Yield Parameters:

  • policy (Policy)

    the current Cloud IAM Policy for this bucket

Returns:

  • (Policy)

    the current Cloud IAM Policy for this bucket

See Also:



1321
1322
1323
1324
1325
1326
1327
1328
1329
# File 'lib/google/cloud/storage/bucket.rb', line 1321

def policy force: nil
  warn "DEPRECATED: 'force' in Bucket#policy" unless force.nil?
  ensure_service!
  gapi = service.get_bucket_policy name, user_project: user_project
  policy = Policy.from_gapi gapi
  return policy unless block_given?
  yield policy
  update_policy policy
end

#post_object(path, policy: nil, issuer: nil, client_email: nil, signing_key: nil, private_key: nil) ⇒ PostObject

Generate a PostObject that includes the fields and url to upload objects via html forms.

Generating a PostObject 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 post_object.

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"
post = bucket.post_object "avatars/heidi/400x400.png"

post.url #=> "https://storage.googleapis.com"
post.fields[:key] #=> "my-todo-app/avatars/heidi/400x400.png"
post.fields[:GoogleAccessId] #=> "0123456789@gserviceaccount.com"
post.fields[:signature] #=> "ABC...XYZ="
post.fields[:policy] #=> "ABC...XYZ="

Using a policy to define the upload authorization:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

policy = {
  expiration: (Time.now + 3600).iso8601,
  conditions: [
    ["starts-with", "$key", ""],
    {acl: "bucket-owner-read"},
    {bucket: "travel-maps"},
    {success_action_redirect: "http://example.com/success.html"},
    ["eq", "$Content-Type", "image/jpeg"],
    ["content-length-range", 0, 1000000]
  ]
}

bucket = storage.bucket "my-todo-app"
post = bucket.post_object "avatars/heidi/400x400.png",
                           policy: policy

post.url #=> "https://storage.googleapis.com"
post.fields[:key] #=> "my-todo-app/avatars/heidi/400x400.png"
post.fields[:GoogleAccessId] #=> "0123456789@gserviceaccount.com"
post.fields[:signature] #=> "ABC...XYZ="
post.fields[:policy] #=> "ABC...XYZ="

Using the issuer and signing_key options:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"
key = OpenSSL::PKey::RSA.new
post = bucket.post_object "avatars/heidi/400x400.png",
                          issuer: "service-account@gcloud.com",
                          signing_key: key

post.url #=> "https://storage.googleapis.com"
post.fields[:key] #=> "my-todo-app/avatars/heidi/400x400.png"
post.fields[:GoogleAccessId] #=> "0123456789@gserviceaccount.com"
post.fields[:signature] #=> "ABC...XYZ="
post.fields[:policy] #=> "ABC...XYZ="

Parameters:

  • path (String)

    Path to the file in Google Cloud Storage.

  • policy (Hash)

    The security policy that describes what can and cannot be uploaded in the form. When provided, the PostObject fields will include a Signature based on the JSON representation of this Hash and the same policy in Base64 format. If you do not provide a security policy, requests are considered to be anonymous and will only work with buckets that have granted WRITE or FULL_CONTROL permission to anonymous users. See Policy Document for more information.

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

Returns:

See Also:



1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
# File 'lib/google/cloud/storage/bucket.rb', line 1178

def post_object path, policy: nil, issuer: nil,
                client_email: nil, signing_key: nil,
                private_key: nil
  ensure_service!

  signer = File::Signer.from_bucket self, path
  signer.post_object issuer: issuer, client_email: client_email,
                     signing_key: signing_key, private_key: private_key,
                     policy: policy
end

#reload!Object Also known as: refresh!

Reloads the bucket with current data from the Storage service.



1549
1550
1551
1552
1553
1554
1555
# File 'lib/google/cloud/storage/bucket.rb', line 1549

def reload!
  ensure_service!
  @gapi = service.get_bucket name, user_project: user_project
  # If NotFound then lazy will never be unset
  @lazy = nil
  self
end

#requester_paysBoolean? Also known as: requester_pays?

Indicates that a client accessing the bucket or a file it contains must assume the transit costs related to the access. The requester must pass the user_project option to Project#bucket and Project#buckets to indicate the project to which the access costs should be billed.

Returns:

  • (Boolean, nil)

    Returns true if requester pays is enabled for the bucket.



398
399
400
# File 'lib/google/cloud/storage/bucket.rb', line 398

def requester_pays
  @gapi.billing.requester_pays if @gapi.billing
end

#requester_pays=(new_requester_pays) ⇒ Object

Enables requester pays for the bucket. If enabled, a client accessing the bucket or a file it contains must assume the transit costs related to the access. The requester must pass the user_project option to Project#bucket and Project#buckets to indicate the project to which the access costs should be billed.

Examples:

Enable requester pays for a bucket:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.requester_pays = true # API call
# Other projects must now provide `user_project` option when calling
# Project#bucket or Project#buckets to access this bucket.

Parameters:

  • new_requester_pays (Boolean)

    When set to true, requester pays is enabled for the bucket.



424
425
426
427
428
# File 'lib/google/cloud/storage/bucket.rb', line 424

def requester_pays= new_requester_pays
  @gapi.billing ||= Google::Apis::StorageV1::Bucket::Billing.new
  @gapi.billing.requester_pays = new_requester_pays
  patch_gapi! :billing
end

#signed_url(path, 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 identified by path. A URL can be created for paths that do not yet exist. For instance, a URL can be created to PUT file contents to.

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"
shared_url = bucket.signed_url "avatars/heidi/400x400.png"

Any of the option parameters may be specified:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

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

Using the headers option:

require "google/cloud/storage"

storage = Google::Cloud.storage

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

Parameters:

  • path (String)

    Path to the file in Google Cloud Storage.

  • 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 File#content_disposition= and File#content_type=.)

Returns:

  • (String)

See Also:



1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
# File 'lib/google/cloud/storage/bucket.rb', line 1068

def signed_url path, 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_bucket self, path
  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

#storage_classString

The bucket's storage class. This defines how objects in the bucket are stored and determines the SLA and the cost of storage. Values include MULTI_REGIONAL, REGIONAL, NEARLINE, COLDLINE, and DURABLE_REDUCED_AVAILABILITY.

Returns:

  • (String)


266
267
268
# File 'lib/google/cloud/storage/bucket.rb', line 266

def storage_class
  @gapi.storage_class
end

#storage_class=(new_storage_class) ⇒ Object

Updates the bucket's storage class. This defines how objects in the bucket are 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. For more information, see Storage Classes.

Parameters:

  • new_storage_class (Symbol, String)

    Storage class of the bucket.



280
281
282
283
# File 'lib/google/cloud/storage/bucket.rb', line 280

def storage_class= new_storage_class
  @gapi.storage_class = storage_class_for(new_storage_class)
  patch_gapi! :storage_class
end

#test_permissions(*permissions) ⇒ Array<String>

Tests the specified permissions against the Cloud IAM access control policy.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"

permissions = bucket.test_permissions "storage.buckets.get",
                                      "storage.buckets.delete"
permissions.include? "storage.buckets.get"    #=> true
permissions.include? "storage.buckets.delete" #=> false

Parameters:

  • permissions (String, Array<String>)

    The set of permissions against which to check access. Permissions must be of the format storage.resource.capability, where resource is one of buckets or objects.

Returns:

  • (Array<String>)

    The permissions held by the caller.

See Also:



1397
1398
1399
1400
1401
1402
1403
# File 'lib/google/cloud/storage/bucket.rb', line 1397

def test_permissions *permissions
  permissions = Array(permissions).flatten
  ensure_service!
  gapi = service.test_bucket_permissions name, permissions,
                                         user_project: user_project
  gapi.permissions
end

#update {|bucket| ... } ⇒ Object

Updates the bucket with changes made in the given block in a single PATCH request. The following attributes may be set: #cors, #logging_bucket=, #logging_prefix=, #versioning=, #website_main=, #website_404=, and #requester_pays=.

In addition, the #cors configuration accessible in the block is completely mutable and will be included in the request. (See Cors)

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"
bucket.update do |b|
  b.website_main = "index.html"
  b.website_404 = "not_found.html"
  b.cors[0].methods = ["GET","POST","DELETE"]
  b.cors[1].headers << "X-Another-Custom-Header"
end

New CORS rules can also be added in a nested block:

require "google/cloud/storage"

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

bucket.update do |b|
  b.cors do |c|
    c.add_rule ["http://example.org", "https://example.org"],
               "*",
               headers: ["X-My-Custom-Header"],
               max_age: 300
  end
end

Yields:

  • (bucket)

    a block yielding a delegate object for updating the file



519
520
521
522
523
524
525
526
# File 'lib/google/cloud/storage/bucket.rb', line 519

def update
  updater = Updater.new @gapi
  yield updater
  # Add check for mutable cors
  updater.check_for_changed_labels!
  updater.check_for_mutable_cors!
  patch_gapi! updater.updates unless updater.updates.empty?
end

#update_policy(new_policy) ⇒ Policy Also known as: policy=

Updates the Cloud IAM access control policy for this bucket. The policy should be read from #policy. See Policy for an explanation of the policy etag property and how to modify policies.

You can also update the policy by passing a block to #policy, which will call this method internally after the block completes.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"

policy = bucket.policy # API call

policy.add "roles/owner", "user:owner@example.com"

bucket.update_policy policy # API call

Parameters:

  • new_policy (Policy)

    a new or modified Cloud IAM Policy for this bucket

Returns:

  • (Policy)

    The policy returned by the API update operation.

See Also:



1363
1364
1365
1366
1367
1368
# File 'lib/google/cloud/storage/bucket.rb', line 1363

def update_policy new_policy
  ensure_service!
  gapi = service.set_bucket_policy name, new_policy.to_gapi,
                                   user_project: user_project
  Policy.from_gapi gapi
end

#versioning=(new_versioning) ⇒ Object

Updates whether Object Versioning is enabled for the bucket.

Parameters:

  • new_versioning (Boolean)

    true if versioning is to be enabled for the bucket.



304
305
306
307
308
# File 'lib/google/cloud/storage/bucket.rb', line 304

def versioning= new_versioning
  @gapi.versioning ||= Google::Apis::StorageV1::Bucket::Versioning.new
  @gapi.versioning.enabled = new_versioning
  patch_gapi! :versioning
end

#versioning?Boolean

Whether Object Versioning is enabled for the bucket.

Returns:

  • (Boolean)


292
293
294
# File 'lib/google/cloud/storage/bucket.rb', line 292

def versioning?
  @gapi.versioning.enabled? unless @gapi.versioning.nil?
end

#website_404String

The page returned from a static website served from the bucket when a site visitor requests a resource that does not exist.

Returns:

  • (String)

See Also:



349
350
351
# File 'lib/google/cloud/storage/bucket.rb', line 349

def website_404
  @gapi.website.not_found_page if @gapi.website
end

#website_404=(website_404) ⇒ Object

Updates the page returned from a static website served from the bucket when a site visitor requests a resource that does not exist.



382
383
384
385
386
# File 'lib/google/cloud/storage/bucket.rb', line 382

def website_404= website_404
  @gapi.website ||= Google::Apis::StorageV1::Bucket::Website.new
  @gapi.website.not_found_page = website_404
  patch_gapi! :website
end

#website_mainString

The main page suffix for a static website. If the requested object path is missing, the service will ensure the path has a trailing '/', append this suffix, and attempt to retrieve the resulting object. This allows the creation of index.html objects to represent directory pages.

Returns:

  • (String)

    The main page suffix.

See Also:



322
323
324
# File 'lib/google/cloud/storage/bucket.rb', line 322

def website_main
  @gapi.website.main_page_suffix if @gapi.website
end

#website_main=(website_main) ⇒ Object

Updates the main page suffix for a static website.

Parameters:

  • website_main (String)

    The main page suffix.

See Also:



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

def website_main= website_main
  @gapi.website ||= Google::Apis::StorageV1::Bucket::Website.new
  @gapi.website.main_page_suffix = website_main
  patch_gapi! :website
end