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.
Direct Known Subclasses
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.
-
#create_file(file, path = nil, acl: nil, cache_control: nil, content_disposition: nil, content_encoding: nil, content_language: nil, content_type: 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 @service = nil @gapi = Google::Apis::StorageV1::Bucket.new end |
Instance Attribute Details
#gapi ⇒ Object
45 46 47 |
# File 'lib/gcloud/storage/bucket.rb', line 45 def gapi @gapi end |
#service ⇒ Object
41 42 43 |
# File 'lib/gcloud/storage/bucket.rb', line 41 def service @service end |
Class Method Details
.from_gapi(gapi, conn) ⇒ Object
703 704 705 706 707 708 |
# File 'lib/gcloud/storage/bucket.rb', line 703 def self.from_gapi gapi, conn new.tap do |f| f.gapi = gapi f.service = 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.
642 643 644 |
# File 'lib/gcloud/storage/bucket.rb', line 642 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.self_link 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 138 139 140 |
# File 'lib/gcloud/storage/bucket.rb', line 130 def cors cors_builder = Bucket::Cors.from_gapi @gapi.cors_configurations if block_given? yield cors_builder if cors_builder.changed? @gapi.cors_configurations = cors_builder.to_gapi patch_gapi! :cors_configurations end end cors_builder.freeze # always return frozen objects end |
#create_file(file, path = nil, acl: nil, cache_control: nil, content_disposition: nil, content_encoding: nil, content_language: nil, content_type: 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.
#### 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.
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 |
# File 'lib/gcloud/storage/bucket.rb', line 579 def create_file file, path = nil, acl: nil, cache_control: nil, content_disposition: nil, content_encoding: nil, content_language: nil, content_type: nil, crc32c: nil, md5: nil, metadata: nil, encryption_key: nil, encryption_key_sha256: nil ensure_service! = { 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, content_language: content_language, metadata: , key: encryption_key, key_sha256: encryption_key_sha256 } ensure_file_exists! file # TODO: Handle file as an IO and path is missing more gracefully path ||= Pathname(file).to_path gapi = service.insert_file name, file, path, File.from_gapi gapi, service end |
#created_at ⇒ Object
Creation time of the bucket.
81 82 83 |
# File 'lib/gcloud/storage/bucket.rb', line 81 def created_at @gapi.time_created 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.
689 690 691 |
# File 'lib/gcloud/storage/bucket.rb', line 689 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 Gcloud#storage to control this behavior.
343 344 345 346 347 |
# File 'lib/gcloud/storage/bucket.rb', line 343 def delete ensure_service! service.delete_bucket name true 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.
446 447 448 449 450 451 452 453 454 455 |
# File 'lib/gcloud/storage/bucket.rb', line 446 def file path, generation: nil, encryption_key: nil, encryption_key_sha256: nil ensure_service! = { generation: generation, key: encryption_key, key_sha256: encryption_key_sha256 } gapi = service.get_file name, path, File.from_gapi gapi, service rescue Gcloud::NotFoundError nil 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.
397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/gcloud/storage/bucket.rb', line 397 def files prefix: nil, delimiter: nil, token: nil, max: nil, versions: nil ensure_service! = { prefix: prefix, delimiter: delimiter, token: token, max: max, versions: versions } gapi = service.list_files name, File::List.from_gapi gapi, service, 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.
149 150 151 |
# File 'lib/gcloud/storage/bucket.rb', line 149 def location @gapi.location end |
#logging_bucket ⇒ Object
The destination bucket name for the bucket’s logs.
158 159 160 |
# File 'lib/gcloud/storage/bucket.rb', line 158 def logging_bucket @gapi.logging.log_bucket if @gapi.logging end |
#logging_bucket=(logging_bucket) ⇒ Object
Updates the destination bucket for the bucket’s logs.
169 170 171 172 173 |
# File 'lib/gcloud/storage/bucket.rb', line 169 def logging_bucket= logging_bucket @gapi.logging ||= Google::Apis::StorageV1::Bucket::Logging.new @gapi.logging.log_bucket = logging_bucket patch_gapi! :logging end |
#logging_prefix ⇒ Object
The logging object prefix for the bucket’s logs. For more information,
180 181 182 |
# File 'lib/gcloud/storage/bucket.rb', line 180 def logging_prefix @gapi.logging.log_object_prefix 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.
194 195 196 197 198 |
# File 'lib/gcloud/storage/bucket.rb', line 194 def logging_prefix= logging_prefix @gapi.logging ||= Google::Apis::StorageV1::Bucket::Logging.new @gapi.logging.log_object_prefix = logging_prefix patch_gapi! :logging 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.
695 696 697 698 |
# File 'lib/gcloud/storage/bucket.rb', line 695 def reload! ensure_service! @gapi = service.get_bucket name 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`.
204 205 206 |
# File 'lib/gcloud/storage/bucket.rb', line 204 def storage_class @gapi.storage_class 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)
317 318 319 320 321 322 323 |
# File 'lib/gcloud/storage/bucket.rb', line 317 def update updater = Updater.new @gapi yield updater # Add check for mutable cors updater.check_for_mutable_cors! 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.
223 224 225 226 227 |
# File 'lib/gcloud/storage/bucket.rb', line 223 def versioning= new_versioning @gapi.versioning ||= Google::Apis::StorageV1::Bucket::Versioning.new @gapi.versioning.enabled = new_versioning patch_gapi! :versioning end |
#versioning? ⇒ Boolean
Whether [Object Versioning](cloud.google.com/storage/docs/object-versioning) is enabled for the bucket.
212 213 214 |
# File 'lib/gcloud/storage/bucket.rb', line 212 def versioning? @gapi.versioning.enabled? unless @gapi.versioning.nil? 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.
260 261 262 |
# File 'lib/gcloud/storage/bucket.rb', line 260 def website_404 @gapi.website.not_found_page 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.
271 272 273 274 275 |
# File 'lib/gcloud/storage/bucket.rb', line 271 def website_404= website_404 @gapi.website ||= Google::Apis::StorageV1::Bucket::Website.new @gapi.website.not_found_page = website_404 patch_gapi! :website 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.
236 237 238 |
# File 'lib/gcloud/storage/bucket.rb', line 236 def website_main @gapi.website.main_page_suffix 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.
247 248 249 250 251 |
# File 'lib/gcloud/storage/bucket.rb', line 247 def website_main= website_main @gapi.website ||= Google::Apis::StorageV1::Bucket::Website.new @gapi.website.main_page_suffix = website_main patch_gapi! :website end |