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 Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ Project



58
59
60
# File 'lib/gcloud/storage/project.rb', line 58

def initialize service
  @service = service
end

Instance Attribute Details

#serviceObject



52
53
54
# File 'lib/gcloud/storage/project.rb', line 52

def service
  @service
end

Class Method Details

.default_projectObject



80
81
82
83
84
85
# File 'lib/gcloud/storage/project.rb', line 80

def self.default_project
  ENV["STORAGE_PROJECT"] ||
    ENV["GCLOUD_PROJECT"] ||
    ENV["GOOGLE_CLOUD_PROJECT"] ||
    Gcloud::GCE.project_id
end

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


156
157
158
159
160
161
# File 'lib/gcloud/storage/project.rb', line 156

def bucket bucket_name
  gapi = service.get_bucket bucket_name
  Bucket.from_gapi gapi, service
rescue Gcloud::NotFoundError
  nil
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 buckets with names that begin with a given prefix:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

user_buckets = storage.buckets prefix: "user-"
user_buckets.each do |bucket|
  puts bucket.name
end

Retrieve all buckets: (See Bucket::List#all)

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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


132
133
134
135
136
# File 'lib/gcloud/storage/project.rb', line 132

def buckets prefix: nil, token: nil, max: nil
  options = { prefix: prefix, token: token, max: max }
  gapi = service.list_buckets options
  Bucket::List.from_gapi gapi, service, prefix, max
end

#create_bucket(bucket_name, acl: nil, default_acl: nil, location: nil, storage_class: nil, logging_bucket: nil, logging_prefix: nil, website_main: nil, website_404: nil, versioning: nil) {|bucket| ... } ⇒ 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 Gcloud#storage to control this behavior.

You can pass [website settings](cloud.google.com/storage/docs/website-configuration) 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"

Configure the bucket in a block:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

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

Yields:

  • (bucket)

    a block for configuring the bucket before it is created

Yield Parameters:

  • cors (Bucket)

    the bucket object to be configured

See Also:



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/gcloud/storage/project.rb', line 278

def create_bucket bucket_name, acl: nil, default_acl: nil,
                  location: nil, storage_class: nil, logging_bucket: nil,
                  logging_prefix: nil, website_main: nil,
                  website_404: nil, versioning: nil
  new_bucket = Google::Apis::StorageV1::Bucket.new({
    name: bucket_name,
    location: location,
    storage_class: storage_class_for(storage_class)
  }.delete_if { |_, v| v.nil? })
  updater = Bucket::Updater.new(new_bucket).tap do |b|
    b.logging_bucket = logging_bucket unless logging_bucket.nil?
    b.logging_prefix = logging_prefix unless logging_prefix.nil?
    b.website_main = website_main unless website_main.nil?
    b.website_404 = website_404 unless website_404.nil?
    b.versioning = versioning unless versioning.nil?
  end
  yield updater if block_given?
  updater.check_for_mutable_cors!
  gapi = service.insert_bucket \
    new_bucket, acl: acl_rule(acl), default_acl: acl_rule(default_acl)
  Bucket.from_gapi gapi, service
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"


74
75
76
# File 'lib/gcloud/storage/project.rb', line 74

def project
  service.project
end