Class: Gcloud::Storage::Project
- Inherits:
-
Object
- Object
- Gcloud::Storage::Project
- 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. 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
Instance Attribute Summary collapse
-
#connection ⇒ Object
The Connection object.
Class Method Summary collapse
-
.default_project ⇒ Object
Default project.
Instance Method Summary collapse
-
#bucket(bucket_name) ⇒ Object
(also: #find_bucket)
Retrieves bucket by name.
-
#buckets(options = {}) ⇒ Object
(also: #find_buckets)
Retrieves a list of buckets for the given project.
-
#create_bucket(bucket_name, options = {}) ⇒ Object
Creates a new bucket with optional attributes.
-
#initialize(project, credentials) ⇒ Project
constructor
Creates a new Project instance.
-
#project ⇒ Object
The Storage project connected to.
Constructor Details
#initialize(project, credentials) ⇒ Project
Creates a new Project instance.
See Gcloud#storage
56 57 58 59 60 |
# File 'lib/gcloud/storage/project.rb', line 56 def initialize project, credentials #:nodoc: project = project.to_s # Always cast to a string fail ArgumentError, "project is missing" if project.empty? @connection = Connection.new project, credentials end |
Instance Attribute Details
#connection ⇒ Object
The Connection object.
50 51 52 |
# File 'lib/gcloud/storage/project.rb', line 50 def connection @connection end |
Class Method Details
.default_project ⇒ Object
Default project.
81 82 83 84 85 86 |
# File 'lib/gcloud/storage/project.rb', line 81 def self.default_project #:nodoc: ENV["STORAGE_PROJECT"] || ENV["GCLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"] || Gcloud::GCE.project_id end |
Instance Method Details
#bucket(bucket_name) ⇒ Object Also known as: find_bucket
182 183 184 185 186 187 188 189 190 |
# File 'lib/gcloud/storage/project.rb', line 182 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(options = {}) ⇒ Object Also known as: find_buckets
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 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
150 151 152 153 154 155 156 157 |
# File 'lib/gcloud/storage/project.rb', line 150 def buckets = {} resp = connection.list_buckets if resp.success? Bucket::List.from_response resp, connection else fail ApiError.from_response(resp) end end |
#create_bucket(bucket_name, options = {}) ⇒ Object
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. For more information about configuring buckets as static websites, see How to Host a Static Website . For more information about CORS, see Cross-Origin Resource Sharing (CORS).
Parameters
bucket_name-
Name of a bucket. (
String) options-
An optional Hash for controlling additional behavior. (
Hash) options[:cors]-
The CORS rules for the bucket. Accepts an array of hashes containing the attributes specified for the resource description of cors.
options[:location]-
The location of the bucket. Object data for objects in the bucket resides in physical storage within this region. Possible values include
ASIA,EU, andUS.(See the developer’s guide for the authoritative list. The default value isUS. (String) options[:logging_bucket]-
The destination bucket for the bucket’s logs. For more information, see Access Logs. (
String) options[:logging_prefix]-
The prefix used to create log object names for the bucket. It can be at most 900 characters and must be a valid object name . By default, the object prefix is the name of the bucket for which the logs are enabled. For more information, see Access Logs. (
String) options[:retries]-
The number of times the API call should be retried. Default is Gcloud::Backoff.retries. (
Integer) options[:storage_class]-
Defines how objects in the bucket are stored and determines the SLA and the cost of storage. Values include
:standard,:nearline, and:dra(Durable Reduced Availability), as well as the strings returned by Bucket#storage_class. For more information, see Storage Classes. The default value is:standard. (SymbolorString) options[:versioning]-
Whether Object Versioning is to be enabled for the bucket. The default value is
false. (Boolean) options[:website_main]-
The index page returned from a static website served from the bucket when a site visitor requests the top level directory. For more information, see How to Host a Static Website .
options[:website_404]-
The page returned from a static website served from the bucket when a site visitor requests a resource that does not exist. For more information, see How to Host a Static Website .
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
You can pass website settings for the bucket, including a block that defines CORS rule. See Bucket::Cors for details.
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
301 302 303 304 305 306 307 308 309 310 |
# File 'lib/gcloud/storage/project.rb', line 301 def create_bucket bucket_name, = {} [:acl] = acl_rule [:acl] [:default_acl] = acl_rule [:default_acl] if block_given? cors_builder = Bucket::Cors.new yield cors_builder [:cors] = cors_builder if cors_builder.changed? end insert_bucket bucket_name, end |