Class: Gcloud::Storage::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/storage/project.rb

Overview

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. Bucket objects are created, read, updated, and deleted by Gcloud::Storage::Project.

See Gcloud#storage

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

Instance Method Summary collapse

Instance Method Details

#bucket(bucket_name) ⇒ Gcloud::Storage::Bucket? Also known as: find_bucket

Retrieves bucket by name.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"
puts bucket.name

166
167
168
169
170
171
172
173
174
# File 'lib/gcloud/storage/project.rb', line 166

def bucket bucket_name
  resp = connection.get_bucket bucket_name
  if resp.success?
    Bucket.from_gapi resp.data, connection
  else
    return nil if resp.data["error"]["code"] == 404
    fail ApiError.from_response(resp)
  end
end

#buckets(prefix: nil, token: nil, max: nil) ⇒ Array<Gcloud::Storage::Bucket> Also known as: find_buckets

Retrieves a list of buckets for the given project.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

Retrieve all buckets with names that begin with a given prefix:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

user_buckets = storage.buckets prefix: "user-"

With pagination: (See 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

138
139
140
141
142
143
144
145
146
# File 'lib/gcloud/storage/project.rb', line 138

def buckets prefix: nil, token: nil, max: nil
  options = { prefix: prefix, token: token, max: max }
  resp = connection.list_buckets options
  if resp.success?
    Bucket::List.from_response resp, connection
  else
    fail ApiError.from_response(resp)
  end
end

#create_bucket(bucket_name, acl: nil, default_acl: nil, cors: nil, location: nil, logging_bucket: nil, logging_prefix: nil, retries: nil, storage_class: nil, versioning: nil, website_main: nil, website_404: nil) ⇒ Gcloud::Storage::Bucket

Creates a new bucket with optional attributes. Also accepts a block for defining the CORS configuration for a static website served from the bucket. See Bucket::Cors for details.

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

You can pass website settings for the bucket, including a block that defines CORS rule. See Bucket::Cors for details.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.create_bucket "my-bucket"

Specify the number of retries to attempt:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

Add CORS rules in a block:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

options = {
  website_main: "index.html"
  website_404: "not_found.html"
}
bucket = storage.create_bucket "my-bucket", options do |c|
  c.add_rule ["http://example.org", "https://example.org"],
             "*",
             response_headers: ["X-My-Custom-Header"],
             max_age: 300
end

See Also:


304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/gcloud/storage/project.rb', line 304

def create_bucket bucket_name, acl: nil, default_acl: nil, cors: nil,
                  location: nil, logging_bucket: nil, logging_prefix: nil,
                  retries: nil, storage_class: nil, versioning: nil,
                  website_main: nil, website_404: nil
  opts = { acl: acl_rule(acl), default_acl: acl_rule(default_acl),
           cors: cors, location: location, logging_bucket: logging_bucket,
           logging_prefix: logging_prefix, retries: retries,
           storage_class: storage_class, versioning: versioning,
           website_main: website_main, website_404: website_404 }
  if block_given?
    cors_builder = Bucket::Cors.new
    yield cors_builder
    opts[:cors] = cors_builder if cors_builder.changed?
  end
  insert_bucket bucket_name, opts
end

#projectObject

The Storage project connected to.

Examples:

require "gcloud"

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

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

76
77
78
# File 'lib/gcloud/storage/project.rb', line 76

def project
  connection.project
end