Class: Google::Cloud::Storage::Bucket
- Inherits:
-
Object
- Object
- Google::Cloud::Storage::Bucket
- Defined in:
- lib/google/cloud/storage/bucket.rb,
lib/google/cloud/storage/bucket/acl.rb,
lib/google/cloud/storage/bucket/cors.rb,
lib/google/cloud/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 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, storage_class: nil, encryption_key: nil) ⇒ Google::Cloud::Storage::File
(also: #upload_file, #new_file)
Creates a new File object by providing a path to a local file (or any IO or IO-ish object) to upload, along with the path at which to store it 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) ⇒ Google::Cloud::Storage::File?
(also: #find_file)
Retrieves a file matching the path.
-
#files(prefix: nil, delimiter: nil, token: nil, max: nil, versions: nil) ⇒ Array<Google::Cloud::Storage::File>
(also: #find_files)
Retrieves a list of files matching the criteria.
-
#id ⇒ Object
The ID of the 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.
-
#policy(force: false) {|policy| ... } ⇒ Policy
Gets and updates the Cloud IAM access control policy for this bucket.
-
#policy=(new_policy) ⇒ Object
Updates the Cloud IAM access control policy for this bucket.
-
#post_object(path, policy: nil, issuer: nil, client_email: nil, signing_key: nil, private_key: nil) ⇒ PostObject
Generate a PostObject that includes the fields and url to upload objects via html forms.
-
#reload! ⇒ Object
(also: #refresh!)
Reloads the bucket with current data from the Storage service.
-
#signed_url(path, method: nil, expires: nil, content_type: nil, content_md5: nil, headers: nil, issuer: nil, client_email: nil, signing_key: nil, private_key: nil) ⇒ Object
Access without authentication can be granted to a File for a specified period of time.
-
#storage_class ⇒ Object
The bucket's storage class.
-
#test_permissions(*permissions) ⇒ Array<String>
Tests the specified permissions against the Cloud IAM access control policy.
-
#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 is enabled for the bucket.
-
#versioning? ⇒ Boolean
Whether 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.
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.
844 845 846 |
# File 'lib/google/cloud/storage/bucket.rb', line 844 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.
77 78 79 |
# File 'lib/google/cloud/storage/bucket.rb', line 77 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.
This method also accepts a block for updating the bucket's CORS rules. See Cors for details.
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/google/cloud/storage/bucket.rb', line 132 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, storage_class: nil, encryption_key: nil) ⇒ Google::Cloud::Storage::File Also known as: upload_file, new_file
Creates a new File object by providing a path to a local file (or any IO or IO-ish object) to upload, along with the path at which to store it 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
can be provided with the encryption_key
option. If given, the same
key 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.
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 |
# File 'lib/google/cloud/storage/bucket.rb', line 576 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, storage_class: nil, encryption_key: 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, metadata: , content_language: content_language, key: encryption_key, storage_class: storage_class_for(storage_class) } ensure_io_or_file_exists! file path ||= file.path if file.respond_to? :path path ||= file if file.is_a? String fail ArgumentError, "must provide path" if path.nil? gapi = service.insert_file name, file, path, File.from_gapi gapi, service end |
#created_at ⇒ Object
Creation time of the bucket.
83 84 85 |
# File 'lib/google/cloud/storage/bucket.rb', line 83 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.
888 889 890 |
# File 'lib/google/cloud/storage/bucket.rb', line 888 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 Google::Cloud#storage to control this behavior.
344 345 346 347 348 |
# File 'lib/google/cloud/storage/bucket.rb', line 344 def delete ensure_service! service.delete_bucket name true end |
#file(path, generation: nil, encryption_key: nil) ⇒ Google::Cloud::Storage::File? Also known as: find_file
Retrieves a file matching the path.
If a customer-supplied encryption
key
was used with #create_file, the encryption_key
option must be
provided or else the file's CRC32C checksum and MD5 hash will not be
returned.
442 443 444 445 446 447 448 449 |
# File 'lib/google/cloud/storage/bucket.rb', line 442 def file path, generation: nil, encryption_key: nil ensure_service! = { generation: generation, key: encryption_key } gapi = service.get_file name, path, File.from_gapi gapi, service rescue Google::Cloud::NotFoundError nil end |
#files(prefix: nil, delimiter: nil, token: nil, max: nil, versions: nil) ⇒ Array<Google::Cloud::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 410 |
# File 'lib/google/cloud/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.
65 66 67 |
# File 'lib/google/cloud/storage/bucket.rb', line 65 def id @gapi.id end |
#kind ⇒ Object
The kind of item this is.
For buckets, this is always storage#bucket
.
59 60 61 |
# File 'lib/google/cloud/storage/bucket.rb', line 59 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.
151 152 153 |
# File 'lib/google/cloud/storage/bucket.rb', line 151 def location @gapi.location end |
#logging_bucket ⇒ Object
The destination bucket name for the bucket's logs.
160 161 162 |
# File 'lib/google/cloud/storage/bucket.rb', line 160 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.
171 172 173 174 175 |
# File 'lib/google/cloud/storage/bucket.rb', line 171 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,
182 183 184 |
# File 'lib/google/cloud/storage/bucket.rb', line 182 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. By default, the object prefix is the name of the bucket for which the logs are enabled.
196 197 198 199 200 |
# File 'lib/google/cloud/storage/bucket.rb', line 196 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.
71 72 73 |
# File 'lib/google/cloud/storage/bucket.rb', line 71 def name @gapi.name end |
#policy(force: false) {|policy| ... } ⇒ Policy
Gets and updates the Cloud IAM access control policy for this bucket.
944 945 946 947 948 949 950 951 952 953 954 955 |
# File 'lib/google/cloud/storage/bucket.rb', line 944 def policy force: false @policy = nil if force || block_given? @policy ||= begin ensure_service! gapi = service.get_bucket_policy name Policy.from_gapi gapi end return @policy unless block_given? p = @policy.deep_dup yield p self.policy = p end |
#policy=(new_policy) ⇒ Object
Updates the Cloud IAM access control
policy for this bucket. The policy should be read from #policy. See
Policy for an explanation of the
policy etag
property and how to modify policies.
You can also update the policy by passing a block to #policy, which will call this method internally after the block completes.
987 988 989 990 991 |
# File 'lib/google/cloud/storage/bucket.rb', line 987 def policy= new_policy ensure_service! gapi = service.set_bucket_policy name, new_policy.to_gapi @policy = Policy.from_gapi gapi end |
#post_object(path, policy: nil, issuer: nil, client_email: nil, signing_key: nil, private_key: nil) ⇒ PostObject
Generate a PostObject that includes the fields and url to upload objects via html forms.
Generating a PostObject requires service account credentials,
either by connecting with a service account when calling
Google::Cloud.storage, or by passing in the service account
issuer
and signing_key
values. Although the private key can
be passed as a string for convenience, creating and storing
an instance of # OpenSSL::PKey::RSA
is more efficient
when making multiple calls to post_object
.
A SignedUrlUnavailable is raised if the service account credentials are missing. Service account credentials are acquired by following the steps in Service Account Authentication.
793 794 795 796 797 798 799 800 801 802 803 |
# File 'lib/google/cloud/storage/bucket.rb', line 793 def post_object path, policy: nil, issuer: nil, client_email: nil, signing_key: nil, private_key: nil ensure_service! = { issuer: issuer, client_email: client_email, signing_key: signing_key, private_key: private_key, policy: policy } signer = File::Signer.from_bucket self, path signer.post_object end |
#reload! ⇒ Object Also known as: refresh!
Reloads the bucket with current data from the Storage service.
1028 1029 1030 1031 |
# File 'lib/google/cloud/storage/bucket.rb', line 1028 def reload! ensure_service! @gapi = service.get_bucket name end |
#signed_url(path, method: nil, expires: nil, content_type: nil, content_md5: nil, headers: nil, issuer: nil, client_email: nil, signing_key: nil, private_key: nil) ⇒ Object
Access without authentication can be granted to a File for a specified
period of time. This URL uses a cryptographic signature of your
credentials to access the file identified by path
. A URL can be
created for paths that do not yet exist. For instance, a URL can be
created to PUT
file contents to.
Generating a URL requires service account credentials, either by
connecting with a service account when calling
Google::Cloud.storage, or by passing in the service account issuer
and signing_key
values. Although the private key can be passed as a
string for convenience, creating and storing an instance of
OpenSSL::PKey::RSA
is more efficient when making multiple calls to
signed_url
.
A SignedUrlUnavailable is raised if the service account credentials are missing. Service account credentials are acquired by following the steps in Service Account Authentication.
685 686 687 688 689 690 691 692 693 694 695 |
# File 'lib/google/cloud/storage/bucket.rb', line 685 def signed_url path, method: nil, expires: nil, content_type: nil, content_md5: nil, headers: nil, issuer: nil, client_email: nil, signing_key: nil, private_key: nil ensure_service! = { method: method, expires: expires, headers: headers, content_type: content_type, content_md5: content_md5, issuer: issuer, client_email: client_email, signing_key: signing_key, private_key: private_key } signer = File::Signer.from_bucket self, path signer.signed_url 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
MULTI_REGIONAL
, REGIONAL
, NEARLINE
, COLDLINE
, STANDARD
,
and DURABLE_REDUCED_AVAILABILITY
.
207 208 209 |
# File 'lib/google/cloud/storage/bucket.rb', line 207 def storage_class @gapi.storage_class end |
#test_permissions(*permissions) ⇒ Array<String>
Tests the specified permissions against the Cloud IAM access control policy.
1019 1020 1021 1022 1023 1024 |
# File 'lib/google/cloud/storage/bucket.rb', line 1019 def * = Array().flatten ensure_service! gapi = service. name, gapi. 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)
319 320 321 322 323 324 325 |
# File 'lib/google/cloud/storage/bucket.rb', line 319 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 is enabled for the bucket.
226 227 228 229 230 |
# File 'lib/google/cloud/storage/bucket.rb', line 226 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 is enabled for the bucket.
215 216 217 |
# File 'lib/google/cloud/storage/bucket.rb', line 215 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.
263 264 265 |
# File 'lib/google/cloud/storage/bucket.rb', line 263 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.
274 275 276 277 278 |
# File 'lib/google/cloud/storage/bucket.rb', line 274 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.
239 240 241 |
# File 'lib/google/cloud/storage/bucket.rb', line 239 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.
250 251 252 253 254 |
# File 'lib/google/cloud/storage/bucket.rb', line 250 def website_main= website_main @gapi.website ||= Google::Apis::StorageV1::Bucket::Website.new @gapi.website.main_page_suffix = website_main patch_gapi! :website end |