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/cors.rb,
lib/gcloud/storage/bucket/list.rb
Overview
# Bucket
Represents a Storage bucket. Belongs to a Project and has many Files.
Defined Under Namespace
Classes: Acl, Cors, DefaultAcl, List, Updater
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#acl ⇒ Object
The Bucket::Acl instance used to control access to the bucket.
-
#api_url ⇒ Object
A URL that can be used to access the bucket using the REST API.
-
#cors {|cors| ... } ⇒ Object
Returns the current CORS configuration for a static website served from the bucket.
-
#cors=(new_cors) ⇒ Object
Updates the CORS configuration for a static website served from the bucket.
-
#create_file(file, path = nil, acl: nil, cache_control: nil, content_disposition: nil, content_encoding: nil, content_language: nil, content_type: nil, chunk_size: nil, crc32c: nil, md5: nil, metadata: nil, encryption_key: nil, encryption_key_sha256: nil) ⇒ Gcloud::Storage::File
(also: #upload_file, #new_file)
Creates a new File object by providing a path to a local file to upload and the path to store it with in the bucket.
-
#created_at ⇒ Object
Creation time of the bucket.
-
#default_acl ⇒ Object
The Bucket::DefaultAcl instance used to control access to the bucket’s files.
-
#delete ⇒ Boolean
Permanently deletes the bucket.
-
#file(path, generation: nil, encryption_key: nil, encryption_key_sha256: nil) ⇒ Gcloud::Storage::File?
(also: #find_file)
Retrieves a file matching the path.
-
#files(prefix: nil, delimiter: nil, token: nil, max: nil, versions: nil) ⇒ Array<Gcloud::Storage::File>
(also: #find_files)
Retrieves a list of files matching the criteria.
-
#id ⇒ Object
The ID of the bucket.
-
#initialize ⇒ Bucket
constructor
A new instance of Bucket.
-
#kind ⇒ Object
The kind of item this is.
-
#location ⇒ Object
The location of the bucket.
-
#logging_bucket ⇒ Object
The destination bucket name for the bucket’s logs.
-
#logging_bucket=(logging_bucket) ⇒ Object
Updates the destination bucket for the bucket’s logs.
-
#logging_prefix ⇒ Object
The logging object prefix for the bucket’s logs.
-
#logging_prefix=(logging_prefix) ⇒ Object
Updates the logging object prefix.
-
#name ⇒ Object
The name of the bucket.
-
#reload! ⇒ Object
(also: #refresh!)
Reloads the bucket with current data from the Storage service.
-
#storage_class ⇒ Object
The bucket’s storage class.
-
#update {|bucket| ... } ⇒ Object
Updates the bucket with changes made in the given block in a single PATCH request.
-
#versioning=(new_versioning) ⇒ Boolean
Updates whether [Object Versioning](cloud.google.com/storage/docs/object-versioning) is enabled for the bucket.
-
#versioning? ⇒ Boolean
Whether [Object Versioning](cloud.google.com/storage/docs/object-versioning) is enabled for the bucket.
-
#website_404 ⇒ Object
The page returned from a static website served from the bucket when a site visitor requests a resource that does not exist.
-
#website_404=(website_404) ⇒ Object
Updates the page returned from a static website served from the bucket when a site visitor requests a resource that does not exist.
-
#website_main ⇒ Object
The index page returned from a static website served from the bucket when a site visitor requests the top level directory.
-
#website_main=(website_main) ⇒ Object
Updates the index page returned from a static website served from the bucket when a site visitor requests the top level directory.
Constructor Details
#initialize ⇒ Bucket
Returns a new instance of Bucket.
49 50 51 52 |
# File 'lib/gcloud/storage/bucket.rb', line 49 def initialize @connection = nil @gapi = {} end |
Instance Attribute Details
#connection ⇒ Object
41 42 43 |
# File 'lib/gcloud/storage/bucket.rb', line 41 def connection @connection end |
#gapi ⇒ Object
45 46 47 |
# File 'lib/gcloud/storage/bucket.rb', line 45 def gapi @gapi end |
Class Method Details
.from_gapi(gapi, conn) ⇒ Object
775 776 777 778 779 780 |
# File 'lib/gcloud/storage/bucket.rb', line 775 def self.from_gapi gapi, conn new.tap do |f| f.gapi = gapi f.connection = conn end end |
Instance Method Details
#acl ⇒ Object
The Bucket::Acl instance used to control access to the bucket.
A bucket has owners, writers, and readers. Permissions can be granted to an individual user’s email address, a group’s email address, as well as many predefined lists.
709 710 711 |
# File 'lib/gcloud/storage/bucket.rb', line 709 def acl @acl ||= Bucket::Acl.new self end |
#api_url ⇒ Object
A URL that can be used to access the bucket using the REST API.
75 76 77 |
# File 'lib/gcloud/storage/bucket.rb', line 75 def api_url @gapi["selfLink"] end |
#cors {|cors| ... } ⇒ Object
Returns the current CORS configuration for a static website served from the bucket.
The return value is a frozen (unmodifiable) array of hashes containing the attributes specified for the Bucket resource field [cors](cloud.google.com/storage/docs/json_api/v1/buckets#cors).
This method also accepts a block for updating the bucket’s CORS rules. See Cors for details.
130 131 132 133 134 135 136 137 |
# File 'lib/gcloud/storage/bucket.rb', line 130 def cors if block_given? cors_builder = Bucket::Cors.new @gapi["cors"] yield cors_builder self.cors = cors_builder if cors_builder.changed? end deep_dup_and_freeze @gapi["cors"] end |
#cors=(new_cors) ⇒ Object
Updates the CORS configuration for a static website served from the bucket.
Accepts an array of hashes containing the attributes specified for the [resource description of cors](cloud.google.com/storage/docs/json_api/v1/buckets#cors).
149 150 151 |
# File 'lib/gcloud/storage/bucket.rb', line 149 def cors= new_cors patch_gapi! cors: new_cors end |
#create_file(file, path = nil, acl: nil, cache_control: nil, content_disposition: nil, content_encoding: nil, content_language: nil, content_type: nil, chunk_size: nil, crc32c: nil, md5: nil, metadata: nil, encryption_key: nil, encryption_key_sha256: nil) ⇒ Gcloud::Storage::File Also known as: upload_file, new_file
Creates a new File object by providing a path to a local file to upload and the path to store it with in the bucket.
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 256KB then it will be lowered to the nearest acceptable value.
#### Customer-supplied encryption keys
By default, Google Cloud Storage manages server-side encryption keys on your behalf. However, a [customer-supplied encryption key](cloud.google.com/storage/docs/encryption#customer-supplied) can be provided with the encryption_key and encryption_key_sha256 options. If given, the same key and SHA256 hash also must be provided to subsequently download or copy the file. If you use customer-supplied encryption keys, you must securely manage your keys and ensure that they are not lost. Also, please note that file metadata is not encrypted, with the exception of the CRC32C checksum and MD5 hash. The names of files and buckets are also not encrypted, and you can read or update the metadata of an encrypted file without providing the encryption key.
#### Troubleshooting large uploads
You may encounter errors while attempting to upload large files. Below are a couple of common cases and their solutions.
##### Handling memory errors
If you encounter a memory error such as NoMemoryError, try performing a resumable upload and setting the chunk_size option to a value that works for your environment, as explained in the final example above.
##### Handling broken pipe errors
To avoid broken pipe (Errno::EPIPE) errors when uploading, add the [httpclient](rubygems.org/gems/httpclient) gem to your project, and the configuration shown below. These lines must execute after you require gcloud but before you make your first gcloud connection. The first statement configures [Faraday](rubygems.org/gems/faraday) to use httpclient. The second statement, which should only be added if you are using a version of Faraday at or above 0.9.2, is a workaround for [this gzip issue](github.com/GoogleCloudPlatform/gcloud-ruby/issues/367).
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 |
# File 'lib/gcloud/storage/bucket.rb', line 644 def create_file file, path = nil, acl: nil, cache_control: nil, content_disposition: nil, content_encoding: nil, content_language: nil, content_type: nil, chunk_size: nil, crc32c: nil, md5: nil, metadata: nil, encryption_key: nil, encryption_key_sha256: nil ensure_connection! = { acl: File::Acl.predefined_rule_for(acl), md5: md5, cache_control: cache_control, content_type: content_type, content_disposition: content_disposition, crc32c: crc32c, content_encoding: content_encoding, chunk_size: chunk_size, content_language: content_language, metadata: , encryption_key: encryption_key, encryption_key_sha256: encryption_key_sha256 } ensure_file_exists! file resumable = resumable_upload?(file) resp = @connection.upload_file resumable, name, file, path, return File.from_gapi(resp.data, connection) if resp.success? fail ApiError.from_response(resp) end |
#created_at ⇒ Object
Creation time of the bucket.
81 82 83 |
# File 'lib/gcloud/storage/bucket.rb', line 81 def created_at @gapi["timeCreated"] end |
#default_acl ⇒ Object
The Bucket::DefaultAcl instance used to control access to the bucket’s files.
A bucket’s files have owners, writers, and readers. Permissions can be granted to an individual user’s email address, a group’s email address, as well as many predefined lists.
756 757 758 |
# File 'lib/gcloud/storage/bucket.rb', line 756 def default_acl @default_acl ||= Bucket::DefaultAcl.new self end |
#delete ⇒ Boolean
Permanently deletes the bucket. The bucket must be empty before it can be deleted.
The API call to delete the bucket may be retried under certain conditions. See Backoff to control this behavior.
342 343 344 345 346 347 348 349 350 |
# File 'lib/gcloud/storage/bucket.rb', line 342 def delete ensure_connection! resp = connection.delete_bucket name if resp.success? true else fail ApiError.from_response(resp) end end |
#file(path, generation: nil, encryption_key: nil, encryption_key_sha256: nil) ⇒ Gcloud::Storage::File? Also known as: find_file
Retrieves a file matching the path.
If a [customer-supplied encryption key](cloud.google.com/storage/docs/encryption#customer-supplied) was used with #create_file, the encryption_key and encryption_key_sha256 options must be provided or else the file’s CRC32C checksum and MD5 hash will not be returned.
450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/gcloud/storage/bucket.rb', line 450 def file path, generation: nil, encryption_key: nil, encryption_key_sha256: nil ensure_connection! = { generation: generation, encryption_key: encryption_key, encryption_key_sha256: encryption_key_sha256 } resp = connection.get_file name, path, if resp.success? File.from_gapi resp.data, connection else fail ApiError.from_response(resp) end end |
#files(prefix: nil, delimiter: nil, token: nil, max: nil, versions: nil) ⇒ Array<Gcloud::Storage::File> Also known as: find_files
Retrieves a list of files matching the criteria.
400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/gcloud/storage/bucket.rb', line 400 def files prefix: nil, delimiter: nil, token: nil, max: nil, versions: nil ensure_connection! = { prefix: prefix, delimiter: delimiter, token: token, max: max, versions: versions } resp = connection.list_files name, fail ApiError.from_response(resp) unless resp.success? File::List.from_response resp, connection, name, prefix, delimiter, max, versions end |
#id ⇒ Object
The ID of the bucket.
63 64 65 |
# File 'lib/gcloud/storage/bucket.rb', line 63 def id @gapi["id"] end |
#kind ⇒ Object
The kind of item this is. For buckets, this is always ‘storage#bucket`.
57 58 59 |
# File 'lib/gcloud/storage/bucket.rb', line 57 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.
160 161 162 |
# File 'lib/gcloud/storage/bucket.rb', line 160 def location @gapi["location"] end |
#logging_bucket ⇒ Object
The destination bucket name for the bucket’s logs.
169 170 171 |
# File 'lib/gcloud/storage/bucket.rb', line 169 def logging_bucket @gapi["logging"]["logBucket"] if @gapi["logging"] end |
#logging_bucket=(logging_bucket) ⇒ Object
Updates the destination bucket for the bucket’s logs.
180 181 182 |
# File 'lib/gcloud/storage/bucket.rb', line 180 def logging_bucket= logging_bucket patch_gapi! logging_bucket: logging_bucket end |
#logging_prefix ⇒ Object
The logging object prefix for the bucket’s logs. For more information,
189 190 191 |
# File 'lib/gcloud/storage/bucket.rb', line 189 def logging_prefix @gapi["logging"]["logObjectPrefix"] if @gapi["logging"] end |
#logging_prefix=(logging_prefix) ⇒ Object
Updates the logging object prefix. This prefix will be used to create log object names for the bucket. It can be at most 900 characters and must be a [valid object name](cloud.google.com/storage/docs/bucket-naming#objectnames). By default, the object prefix is the name of the bucket for which the logs are enabled.
203 204 205 |
# File 'lib/gcloud/storage/bucket.rb', line 203 def logging_prefix= logging_prefix patch_gapi! logging_prefix: logging_prefix end |
#name ⇒ Object
The name of the bucket.
69 70 71 |
# File 'lib/gcloud/storage/bucket.rb', line 69 def name @gapi["name"] end |
#reload! ⇒ Object Also known as: refresh!
Reloads the bucket with current data from the Storage service.
762 763 764 765 766 767 768 769 770 |
# File 'lib/gcloud/storage/bucket.rb', line 762 def reload! ensure_connection! resp = connection.get_bucket name if resp.success? @gapi = resp.data else fail ApiError.from_response(resp) end end |
#storage_class ⇒ Object
The bucket’s storage class. This defines how objects in the bucket are stored and determines the SLA and the cost of storage. Values include STANDARD, NEARLINE, and DURABLE_REDUCED_AVAILABILITY.
211 212 213 |
# File 'lib/gcloud/storage/bucket.rb', line 211 def storage_class @gapi["storageClass"] end |
#update {|bucket| ... } ⇒ Object
Updates the bucket with changes made in the given block in a single PATCH request. The following attributes may be set: #cors=, #logging_bucket=, #logging_prefix=, #versioning=, #website_main=, and #website_404=. In addition, the #cors configuration accessible in the block is completely mutable and will be included in the request. (See Cors)
318 319 320 321 322 |
# File 'lib/gcloud/storage/bucket.rb', line 318 def update updater = Updater.new @gapi["cors"] yield updater patch_gapi! updater.updates unless updater.updates.empty? end |
#versioning=(new_versioning) ⇒ Boolean
Updates whether [Object Versioning](cloud.google.com/storage/docs/object-versioning) is enabled for the bucket.
230 231 232 |
# File 'lib/gcloud/storage/bucket.rb', line 230 def versioning= new_versioning patch_gapi! versioning: new_versioning end |
#versioning? ⇒ Boolean
Whether [Object Versioning](cloud.google.com/storage/docs/object-versioning) is enabled for the bucket.
219 220 221 |
# File 'lib/gcloud/storage/bucket.rb', line 219 def versioning? !@gapi["versioning"].nil? && @gapi["versioning"]["enabled"] end |
#website_404 ⇒ Object
The page returned from a static website served from the bucket when a site visitor requests a resource that does not exist.
263 264 265 |
# File 'lib/gcloud/storage/bucket.rb', line 263 def website_404 @gapi["website"]["notFoundPage"] if @gapi["website"] end |
#website_404=(website_404) ⇒ Object
Updates the page returned from a static website served from the bucket when a site visitor requests a resource that does not exist.
274 275 276 |
# File 'lib/gcloud/storage/bucket.rb', line 274 def website_404= website_404 patch_gapi! website_404: website_404 end |
#website_main ⇒ Object
The index page returned from a static website served from the bucket when a site visitor requests the top level directory.
241 242 243 |
# File 'lib/gcloud/storage/bucket.rb', line 241 def website_main @gapi["website"]["mainPageSuffix"] if @gapi["website"] end |
#website_main=(website_main) ⇒ Object
Updates the index page returned from a static website served from the bucket when a site visitor requests the top level directory.
252 253 254 |
# File 'lib/gcloud/storage/bucket.rb', line 252 def website_main= website_main patch_gapi! website_main: website_main end |