Class: Gcloud::Storage::Bucket
- Inherits:
-
Object
- Object
- Gcloud::Storage::Bucket
- Defined in:
- lib/gcloud/storage/bucket.rb,
lib/gcloud/storage/bucket/acl.rb,
lib/gcloud/storage/bucket/list.rb
Overview
Represents a Bucket. Belongs to a Project and has many Files.
Defined Under Namespace
Classes: Acl, DefaultAcl, List
Instance Attribute Summary collapse
-
#connection ⇒ Object
The Connection object.
-
#gapi ⇒ Object
The Google API Client object.
Class Method Summary collapse
-
.from_gapi(gapi, conn) ⇒ Object
New Bucket from a Google API Client object.
Instance Method Summary collapse
-
#acl ⇒ Object
Access Control List.
-
#create_file(file, path = nil, options = {}) ⇒ Object
Create a new Gcloud::Storeage::File object by providing a File object to upload and the path to store it with.
-
#created_at ⇒ Object
Creation time of the bucket.
-
#default_acl ⇒ Object
Default Access Control List.
-
#delete(options = {}) ⇒ Object
Permenently deletes the bucket.
-
#files(options = {}) ⇒ Object
Retrieves a list of files matching the criteria.
-
#find_file(path, options = {}) ⇒ Object
Retrieves a file matching the path.
-
#id ⇒ Object
The ID of the bucket.
-
#initialize ⇒ Bucket
constructor
Create an empty Bucket object.
-
#kind ⇒ Object
The kind of item this is.
-
#location ⇒ Object
The location of the bucket.
-
#name ⇒ Object
The name of the bucket.
-
#url ⇒ Object
The URI of this bucket.
Constructor Details
#initialize ⇒ Bucket
Create an empty Bucket object.
34 35 36 37 |
# File 'lib/gcloud/storage/bucket.rb', line 34 def initialize @connection = nil @gapi = {} end |
Instance Attribute Details
#connection ⇒ Object
The Connection object.
26 27 28 |
# File 'lib/gcloud/storage/bucket.rb', line 26 def connection @connection end |
#gapi ⇒ Object
The Google API Client object.
30 31 32 |
# File 'lib/gcloud/storage/bucket.rb', line 30 def gapi @gapi end |
Class Method Details
.from_gapi(gapi, conn) ⇒ Object
New Bucket from a Google API Client object.
197 198 199 200 201 202 |
# File 'lib/gcloud/storage/bucket.rb', line 197 def self.from_gapi gapi, conn #:nodoc: new.tap do |f| f.gapi = gapi f.connection = conn end end |
Instance Method Details
#acl ⇒ Object
Access Control List
185 186 187 |
# File 'lib/gcloud/storage/bucket.rb', line 185 def acl @acl ||= Bucket::Acl.new self end |
#create_file(file, path = nil, options = {}) ⇒ Object
Create a new Gcloud::Storeage::File object by providing a File object to upload and the path to store it with.
storage = Gcloud.storage
bucket = storage.find_bucket "my-bucket"
bucket.create_file "path/to/local.file.ext"
Additionally, a destination path can be specified.
storage = Gcloud.storage
bucket = storage.find_bucket "my-bucket"
bucket.create_file "path/to/local.file.ext",
"destination/path/file.ext"
A chunk_size value can be provided in the options to be used in resumable uploads. This value is the number of bytes per chunk and must be divisible by 256KB. If it is not divisible by 265KB then it will be lowered to the nearest acceptible value.
bucket.create_file "path/to/local.file.ext",
"destination/path/file.ext",
chunk_size: 1024*1024 # 1 MB chunk
See Gcloud::Storage::File
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/gcloud/storage/bucket.rb', line 168 def create_file file, path = nil, = {} ensure_connection! # TODO: Raise if file doesn't exist # ensure_file_exists! fail unless ::File.file? file [:acl] = File::Acl.predefined_rule_for [:acl] if resumable_upload? file upload_resumable file, path, [:chunk_size], else upload_multipart file, path, end end |
#created_at ⇒ Object
Creation time of the bucket.
77 78 79 |
# File 'lib/gcloud/storage/bucket.rb', line 77 def created_at @gapi["timeCreated"] end |
#default_acl ⇒ Object
Default Access Control List
191 192 193 |
# File 'lib/gcloud/storage/bucket.rb', line 191 def default_acl @default_acl ||= Bucket::DefaultAcl.new self end |
#delete(options = {}) ⇒ Object
Permenently deletes the bucket. The bucket must be empty.
bucket.delete
The API call to delete the bucket may be retried under certain conditions. See Gcloud::Backoff to control this behavior, or specify the wanted behavior in the call:
bucket.delete retries: 5
92 93 94 95 96 97 98 99 100 |
# File 'lib/gcloud/storage/bucket.rb', line 92 def delete = {} ensure_connection! resp = connection.delete_bucket name, if resp.success? true else ApiError.from_response(resp) end end |
#files(options = {}) ⇒ Object
Retrieves a list of files matching the criteria.
storage = Gcloud.storage
bucket = storage.find_bucket "my-bucket"
files = bucket.files
files.each do |file|
puts file.name
end
See Gcloud::Storage::File
113 114 115 116 117 118 119 120 121 |
# File 'lib/gcloud/storage/bucket.rb', line 113 def files = {} ensure_connection! resp = connection.list_files name, if resp.success? File::List.from_resp resp, connection else fail ApiError.from_response(resp) end end |
#find_file(path, options = {}) ⇒ Object
Retrieves a file matching the path.
storage = Gcloud.storage
bucket = storage.find_bucket "my-bucket"
file = bucket.find_file "path/to/my-file.ext"
puts file.name
See Gcloud::Storage::File
132 133 134 135 136 137 138 139 140 |
# File 'lib/gcloud/storage/bucket.rb', line 132 def find_file path, = {} ensure_connection! resp = connection.get_file name, path, if resp.success? File.from_gapi resp.data, connection else fail ApiError.from_response(resp) end end |
#id ⇒ Object
The ID of the bucket.
48 49 50 |
# File 'lib/gcloud/storage/bucket.rb', line 48 def id @gapi["id"] end |
#kind ⇒ Object
The kind of item this is. For buckets, this is always storage#bucket.
42 43 44 |
# File 'lib/gcloud/storage/bucket.rb', line 42 def kind @gapi["kind"] end |
#location ⇒ Object
The location of the bucket. Object data for objects in the bucket resides in physical storage within this region. Defaults to US. See the developer’s guide for the authoritative list.
71 72 73 |
# File 'lib/gcloud/storage/bucket.rb', line 71 def location @gapi["location"] end |
#name ⇒ Object
The name of the bucket.
54 55 56 |
# File 'lib/gcloud/storage/bucket.rb', line 54 def name @gapi["name"] end |
#url ⇒ Object
The URI of this bucket.
60 61 62 |
# File 'lib/gcloud/storage/bucket.rb', line 60 def url @gapi["selfLink"] end |