Gcloud::Storage::Bucket

Bucket

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

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

Methods

Public Instance Methods

acl()

The Bucket::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. See the Access Control guide for more.

Examples

Access to a bucket can be granted to a user by appending +“user-”+ to the email address:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

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

Access to a bucket can be granted to a group by appending +“group-”+ to the email address:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

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

Access to a bucket can also be granted to a predefined list of permissions:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

bucket.acl.public!

create_file(file, path = nil, options = {})

Create a new File object by providing a path to a local file to upload and the path to store it with in the bucket.

Parameters

file

Path of the file on the filesystem to upload. (String)

path

Path to store the file in Google Cloud Storage. (String)

options

An optional Hash for controlling additional behavior. (Hash)

options[:acl]

A predefined set of access controls to apply to this file. (String)

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.

Returns

Gcloud::Storage::File

Examples

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

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

Additionally, a destination path can be specified.

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

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

A chunk_size value can be provided in the options to be used in resumable uploads. This value is the number of bytes per chunk and must be divisible by 256KB. If it is not divisible by 265KB then it will be lowered to the nearest acceptible value.

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

bucket.create_file "path/to/local.file.ext",
                   "destination/path/file.ext",
                   chunk_size: 1024*1024 # 1 MB chunk
Also aliased as: upload_file, new_file

created_at()

Creation time of the bucket.

default_acl()

The Bucket::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. See the Access Control guide for more.

Examples

Access to a bucket's files can be granted to a user by appending +“user-”+ to the email address:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

email = "heidi@example.net"
bucket.default_acl.add_reader "user-#{email}"

Access to a bucket's files can be granted to a group by appending +“group-”+ to the email address:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

email = "authors@example.net"
bucket.default_acl.add_reader "group-#{email}"

Access to a bucket's files can also be granted to a predefined list of permissions:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

bucket.default_acl.public!

delete(options = {})

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

Parameters

options

An optional Hash for controlling additional behavior. (Hash)

options[:retries]

The number of times the API call should be retried. Default is Gcloud::Backoff.retries. (Integer)

Returns

true if the bucket was deleted.

Examples

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

The API call to delete the bucket may be retried under certain conditions. See Gcloud::Backoff to control this behavior, or specify the wanted behavior in the call:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"
bucket.delete retries: 5

file(path, options = {})

Retrieves a file matching the path.

Parameters

path

Name (path) of the file. (String)

Returns

Gcloud::Storage::File or nil if file does not exist

Example

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

file = bucket.file "path/to/my-file.ext"
puts file.name
Also aliased as: find_file

files(options = {})

Retrieves a list of files matching the criteria.

Parameters

options

An optional Hash for controlling additional behavior. (Hash)

options[:prefix]

Filter results to files whose names begin with this prefix. (String)

options[:token]

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

options[:max]

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. (Integer)

options[:versions]

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

options[:max]

Maximum number of buckets to return. (Integer)

Returns

Array of Gcloud::Storage::File (Gcloud::Storage::File::List)

Examples

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

If you have a significant number of files, you may need to paginate through them: (See Gcloud::Storage::File::List#token)

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

all_files = []
tmp_files = bucket.files
while tmp_files.any? do
  tmp_files.each do |file|
    all_files << file
  end
  # break loop if no more buckets available
  break if tmp_files.token.nil?
  # get the next group of files
  tmp_files = bucket.files token: tmp_files.token
end
Also aliased as: find_files

find_file(path, options = {})

Alias for: file

find_files(options = {})

Alias for: files

id()

The ID of the bucket.

kind()

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

location()

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.

cloud.google.com/storage/docs/concepts-techniques

name()

The name of the bucket.

new_file(file, path = nil, options = {})

Alias for: create_file

upload_file(file, path = nil, options = {})

Alias for: create_file

url()

The URI of this bucket.