Gcloud::Storage::Project

Project

Represents the project that storage buckets and files belong to. All data in Google Cloud Storage belongs inside a project. A project consists of a set of users, a set of APIs, billing, authentication, and monitoring settings for those APIs.

Gcloud::Storage::Project is the main object for interacting with Google Storage. Gcloud::Storage::Bucket objects are created, read, updated, and deleted by Gcloud::Storage::Project.

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

See Gcloud#storage

Methods

Public Instance Methods

bucket(bucket_name)

Retrieves bucket by name.

Parameters

bucket_name

Name of a bucket. (String)

Returns

Gcloud::Storage::Bucket or nil if bucket does not exist

Example

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"
puts bucket.name
Also aliased as: find_bucket

buckets(options = {})

Retrieves a list of buckets for the given project.

Parameters

options

An optional Hash for controlling additional behavior. (Hash)

options[:prefix]

Filter results to buckets 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 buckets to return. (Integer)

Returns

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

Examples

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

buckets = storage.buckets
buckets.each do |bucket|
  puts bucket.name
end

You can also retrieve all buckets whose names begin with a prefix using the :prefix option:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

user_buckets = storage.buckets prefix: "user-"

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

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

all_buckets = []
tmp_buckets = storage.buckets
while tmp_buckets.any? do
  tmp_buckets.each do |bucket|
    all_buckets << bucket
  end
  # break loop if no more buckets available
  break if tmp_buckets.token.nil?
  # get the next group of buckets
  tmp_buckets = storage.buckets token: tmp_buckets.token
end
Also aliased as: find_buckets

create_bucket(bucket_name, options = {})

Creates a new bucket.

Parameters

bucket_name

Name of a bucket. (String)

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

Gcloud::Storage::Bucket

Examples

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.create_bucket "my-bucket"

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

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.create_bucket "my-bucket", retries: 5

find_bucket(bucket_name)

Alias for: bucket

find_buckets(options = {})

Alias for: buckets

project()

The Storage project connected to.

Example

require "gcloud"

gcloud = Gcloud.new "my-todo-project",
                    "/path/to/keyfile.json"
storage = gcloud.storage

storage.project #=> "my-todo-project"