Class: Gcloud::Storage::Project

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

Overview

Represents the Project that the Buckets and Files belong to.

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.

storage = Gcloud.storage "my-todo-project",
                         "/path/to/keyfile.json"
bucket = storage.find_bucket "my-bucket"
file = bucket.find_file "path/to/my-file.ext"

See Gcloud.storage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, credentials) ⇒ Project

Creates a new Project instance.

See Gcloud.storage



45
46
47
# File 'lib/gcloud/storage/project.rb', line 45

def initialize project, credentials #:nodoc:
  @connection = Connection.new project, credentials
end

Instance Attribute Details

#connectionObject

The Connection object.



39
40
41
# File 'lib/gcloud/storage/project.rb', line 39

def connection
  @connection
end

Instance Method Details

#buckets(options = {}) ⇒ Object

Retrieves a list of buckets for the given project.

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

See Gcloud::Storage::Bucket



65
66
67
68
69
70
71
72
# File 'lib/gcloud/storage/project.rb', line 65

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

#create_bucket(bucket_name, options = {}) ⇒ Object

Creates a new bucket.

bucket = project.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:

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

See Gcloud::Storage::Bucket



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gcloud/storage/project.rb', line 104

def create_bucket bucket_name, options = {}
  options[:acl] = Bucket::Acl.predefined_rule_for options[:acl]
  default_acl = options[:default_acl]
  default_acl = Bucket::DefaultAcl.predefined_rule_for default_acl
  options[:default_acl] = default_acl

  resp = connection.insert_bucket bucket_name, options
  if resp.success?
    Bucket.from_gapi resp.data, connection
  else
    fail ApiError.from_response(resp)
  end
end

#find_bucket(bucket_name) ⇒ Object

Retrieves bucket by name.

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

See Gcloud::Storage::Bucket



82
83
84
85
86
87
88
89
90
# File 'lib/gcloud/storage/project.rb', line 82

def find_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

#projectObject

The project identifier.



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

def project
  connection.project
end