Class: Aws::S3::Bucket
- Inherits:
-
Object
- Object
- Aws::S3::Bucket
- Extended by:
- Deprecations
- Defined in:
- lib/aws-sdk-s3/customizations/bucket.rb,
lib/aws-sdk-s3/bucket.rb
Defined Under Namespace
Classes: Collection
Read-Only Attributes collapse
-
#creation_date ⇒ Time
Date the bucket was created.
- #name ⇒ String
Actions collapse
- #create(options = {}) ⇒ Types::CreateBucketOutput
- #delete(options = {}) ⇒ EmptyStructure
- #delete_objects(options = {}) ⇒ Types::DeleteObjectsOutput
- #put_object(options = {}) ⇒ Object
Associations collapse
- #acl ⇒ BucketAcl
- #cors ⇒ BucketCors
- #identifiers ⇒ Object deprecated private Deprecated.
- #lifecycle ⇒ BucketLifecycle
- #lifecycle_configuration ⇒ BucketLifecycleConfiguration
- #logging ⇒ BucketLogging
- #multipart_uploads(options = {}) ⇒ MultipartUpload::Collection
- #notification ⇒ BucketNotification
- #object(key) ⇒ Object
- #object_versions(options = {}) ⇒ ObjectVersion::Collection
- #objects(options = {}) ⇒ ObjectSummary::Collection
- #policy ⇒ BucketPolicy
- #request_payment ⇒ BucketRequestPayment
- #tagging ⇒ BucketTagging
- #versioning ⇒ BucketVersioning
- #website ⇒ BucketWebsite
Instance Method Summary collapse
-
#clear! ⇒ void
Deletes all objects and versioned objects from this bucket.
- #client ⇒ Client
-
#data ⇒ Types::Bucket
Returns the data for this Bucket.
-
#data_loaded? ⇒ Boolean
Returns ‘true` if this resource is loaded.
-
#delete!(options = { }) ⇒ void
Deletes all objects and versioned objects from this bucket and then deletes the bucket.
-
#exists?(options = {}) ⇒ Boolean
Returns ‘true` if the Bucket exists.
-
#initialize(*args) ⇒ Bucket
constructor
A new instance of Bucket.
- #load ⇒ Object (also: #reload) private
-
#presigned_post(options = {}) ⇒ PresignedPost
Creates a PresignedPost that makes it easy to upload a file from a web browser direct to Amazon S3 using an HTML post form with a file field.
-
#url(options = {}) ⇒ String
Returns a public URL for this bucket.
-
#wait_until(options = {}, &block) ⇒ Resource
deprecated
Deprecated.
Use [Aws::S3::Client] #wait_until instead
- #wait_until_exists(options = {}) ⇒ Bucket
- #wait_until_not_exists(options = {}) ⇒ Bucket
Constructor Details
#initialize(name, options = {}) ⇒ Bucket #initialize(options = {}) ⇒ Bucket
Returns a new instance of Bucket.
19 20 21 22 23 24 |
# File 'lib/aws-sdk-s3/bucket.rb', line 19 def initialize(*args) = Hash === args.last ? args.pop.dup : {} @name = extract_name(args, ) @data = .delete(:data) @client = .delete(:client) || Client.new() end |
Instance Method Details
#acl ⇒ BucketAcl
413 414 415 416 417 418 |
# File 'lib/aws-sdk-s3/bucket.rb', line 413 def acl BucketAcl.new( bucket_name: @name, client: @client ) end |
#clear! ⇒ void
This method returns an undefined value.
Deletes all objects and versioned objects from this bucket
14 15 16 |
# File 'lib/aws-sdk-s3/customizations/bucket.rb', line 14 def clear! object_versions.batch_delete! end |
#cors ⇒ BucketCors
421 422 423 424 425 426 |
# File 'lib/aws-sdk-s3/bucket.rb', line 421 def cors BucketCors.new( bucket_name: @name, client: @client ) end |
#create(options = {}) ⇒ Types::CreateBucketOutput
244 245 246 247 248 |
# File 'lib/aws-sdk-s3/bucket.rb', line 244 def create( = {}) = .merge(bucket: @name) resp = @client.create_bucket() resp.data end |
#creation_date ⇒ Time
Date the bucket was created.
35 36 37 |
# File 'lib/aws-sdk-s3/bucket.rb', line 35 def creation_date data[:creation_date] end |
#data ⇒ Types::Bucket
Returns the data for this Aws::S3::Bucket.
57 58 59 60 |
# File 'lib/aws-sdk-s3/bucket.rb', line 57 def data load unless @data @data end |
#data_loaded? ⇒ Boolean
65 66 67 |
# File 'lib/aws-sdk-s3/bucket.rb', line 65 def data_loaded? !!@data end |
#delete(options = {}) ⇒ EmptyStructure
255 256 257 258 259 |
# File 'lib/aws-sdk-s3/bucket.rb', line 255 def delete( = {}) = .merge(bucket: @name) resp = @client.delete_bucket() resp.data end |
#delete!(options = { }) ⇒ void
This method returns an undefined value.
Deletes all objects and versioned objects from this bucket and then deletes the bucket.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/aws-sdk-s3/customizations/bucket.rb', line 34 def delete! = { } = { initial_wait: 1.3, max_attempts: 3, }.merge() attempts = 0 begin clear! delete rescue Errors::BucketNotEmpty attempts += 1 if attempts >= [:max_attempts] raise else Kernel.sleep([:initial_wait] ** attempts) retry end end end |
#delete_objects(options = {}) ⇒ Types::DeleteObjectsOutput
288 289 290 291 292 |
# File 'lib/aws-sdk-s3/bucket.rb', line 288 def delete_objects( = {}) = .merge(bucket: @name) resp = @client.delete_objects() resp.data end |
#exists?(options = {}) ⇒ Boolean
Returns ‘true` if the Bucket exists.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/aws-sdk-s3/bucket.rb', line 72 def exists?( = {}) begin wait_until_exists(.merge(max_attempts: 1)) true rescue Aws::Waiters::Errors::UnexpectedError => e raise e.error rescue Aws::Waiters::Errors::WaiterFailed false end end |
#identifiers ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
655 656 657 |
# File 'lib/aws-sdk-s3/bucket.rb', line 655 def identifiers { name: @name } end |
#lifecycle ⇒ BucketLifecycle
429 430 431 432 433 434 |
# File 'lib/aws-sdk-s3/bucket.rb', line 429 def lifecycle BucketLifecycle.new( bucket_name: @name, client: @client ) end |
#lifecycle_configuration ⇒ BucketLifecycleConfiguration
437 438 439 440 441 442 |
# File 'lib/aws-sdk-s3/bucket.rb', line 437 def lifecycle_configuration BucketLifecycleConfiguration.new( bucket_name: @name, client: @client ) end |
#load ⇒ Object Also known as: reload
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
101 102 103 104 105 |
# File 'lib/aws-sdk-s3/customizations/bucket.rb', line 101 def load @data = client.list_buckets.buckets.find { |b| b.name == name } raise "unable to load bucket #{name}" if @data.nil? self end |
#logging ⇒ BucketLogging
445 446 447 448 449 450 |
# File 'lib/aws-sdk-s3/bucket.rb', line 445 def logging BucketLogging.new( bucket_name: @name, client: @client ) end |
#multipart_uploads(options = {}) ⇒ MultipartUpload::Collection
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/aws-sdk-s3/bucket.rb', line 482 def multipart_uploads( = {}) batches = Enumerator.new do |y| = .merge(bucket: @name) resp = @client.list_multipart_uploads() resp.each_page do |page| batch = [] page.data.uploads.each do |u| batch << MultipartUpload.new( bucket_name: @name, object_key: u.key, id: u.upload_id, data: u, client: @client ) end y.yield(batch) end end MultipartUpload::Collection.new(batches) end |
#name ⇒ String
29 30 31 |
# File 'lib/aws-sdk-s3/bucket.rb', line 29 def name @name end |
#notification ⇒ BucketNotification
504 505 506 507 508 509 |
# File 'lib/aws-sdk-s3/bucket.rb', line 504 def notification BucketNotification.new( bucket_name: @name, client: @client ) end |
#object(key) ⇒ Object
513 514 515 516 517 518 519 |
# File 'lib/aws-sdk-s3/bucket.rb', line 513 def object(key) Object.new( bucket_name: @name, key: key, client: @client ) end |
#object_versions(options = {}) ⇒ ObjectVersion::Collection
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'lib/aws-sdk-s3/bucket.rb', line 547 def object_versions( = {}) batches = Enumerator.new do |y| = .merge(bucket: @name) resp = @client.list_object_versions() resp.each_page do |page| batch = [] page.data.versions_delete_markers.each do |v| batch << ObjectVersion.new( bucket_name: @name, object_key: v.key, id: v.version_id, data: v, client: @client ) end y.yield(batch) end end ObjectVersion::Collection.new(batches) end |
#objects(options = {}) ⇒ ObjectSummary::Collection
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/aws-sdk-s3/bucket.rb', line 593 def objects( = {}) batches = Enumerator.new do |y| = .merge(bucket: @name) resp = @client.list_objects() resp.each_page do |page| batch = [] page.data.contents.each do |c| batch << ObjectSummary.new( bucket_name: @name, key: c.key, data: c, client: @client ) end y.yield(batch) end end ObjectSummary::Collection.new(batches) end |
#policy ⇒ BucketPolicy
614 615 616 617 618 619 |
# File 'lib/aws-sdk-s3/bucket.rb', line 614 def policy BucketPolicy.new( bucket_name: @name, client: @client ) end |
#presigned_post(options = {}) ⇒ PresignedPost
You must specify ‘:key` or `:key_starts_with`. All other options are optional.
Creates a PresignedPost that makes it easy to upload a file from a web browser direct to Amazon S3 using an HTML post form with a file field.
See the PresignedPost documentation for more information.
91 92 93 94 95 96 97 98 |
# File 'lib/aws-sdk-s3/customizations/bucket.rb', line 91 def presigned_post( = {}) PresignedPost.new( client.config.credentials, client.config.region, name, {url: url}.merge() ) end |
#put_object(options = {}) ⇒ Object
400 401 402 403 404 405 406 407 408 |
# File 'lib/aws-sdk-s3/bucket.rb', line 400 def put_object( = {}) = .merge(bucket: @name) resp = @client.put_object() Object.new( bucket_name: @name, key: [:key], client: @client ) end |
#request_payment ⇒ BucketRequestPayment
622 623 624 625 626 627 |
# File 'lib/aws-sdk-s3/bucket.rb', line 622 def request_payment BucketRequestPayment.new( bucket_name: @name, client: @client ) end |
#tagging ⇒ BucketTagging
630 631 632 633 634 635 |
# File 'lib/aws-sdk-s3/bucket.rb', line 630 def tagging BucketTagging.new( bucket_name: @name, client: @client ) end |
#url(options = {}) ⇒ String
Returns a public URL for this bucket.
bucket = s3.bucket('bucket-name')
bucket.url
#=> "https://bucket-name.s3.amazonaws.com"
You can pass ‘virtual_host: true` to use the bucket name as the host name.
bucket = s3.bucket('my.bucket.com', virtual_host: true)
bucket.url
#=> "http://my.bucket.com"
73 74 75 76 77 78 79 |
# File 'lib/aws-sdk-s3/customizations/bucket.rb', line 73 def url( = {}) if [:virtual_host] "http://#{name}" else s3_bucket_url end end |
#versioning ⇒ BucketVersioning
638 639 640 641 642 643 |
# File 'lib/aws-sdk-s3/bucket.rb', line 638 def versioning BucketVersioning.new( bucket_name: @name, client: @client ) end |
#wait_until(options = {}, &block) ⇒ Resource
Use [Aws::S3::Client] #wait_until instead
The waiting operation is performed on a copy. The original resource remains unchanged
Waiter polls an API operation until a resource enters a desired state.
## Basic Usage
Waiter will polls until it is successful, it fails by entering a terminal state, or until a maximum number of attempts are made.
# polls in a loop until condition is true
resource.wait_until() {|resource| condition}
## Example
instance.wait_until(max_attempts:10, delay:5) {|instance| instance.state.name == 'running' }
## Configuration
You can configure the maximum number of polling attempts, and the delay (in seconds) between each polling attempt. The waiting condition is set by passing a block to #wait_until:
# poll for ~25 seconds
resource.wait_until(max_attempts:5,delay:5) {|resource|...}
## Callbacks
You can be notified before each polling attempt and before each delay. If you throw ‘:success` or `:failure` from these callbacks, it will terminate the waiter.
started_at = Time.now
# poll for 1 hour, instead of a number of attempts
proc = Proc.new do |attempts, response|
throw :failure if Time.now - started_at > 3600
end
# disable max attempts
instance.wait_until(before_wait:proc, max_attempts:nil) {...}
## Handling Errors
When a waiter is successful, it returns the Resource. When a waiter fails, it raises an error.
begin
resource.wait_until(...)
rescue Aws::Waiters::Errors::WaiterFailed
# resource did not enter the desired state in time
end
attempts attempt in seconds invoked before each attempt invoked before each wait
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/aws-sdk-s3/bucket.rb', line 195 def wait_until( = {}, &block) self_copy = self.dup attempts = 0 [:max_attempts] = 10 unless .key?(:max_attempts) [:delay] ||= 10 [:poller] = Proc.new do attempts += 1 if block.call(self_copy) [:success, self_copy] else self_copy.reload unless attempts == [:max_attempts] :retry end end Aws::Waiters::Waiter.new().wait({}) end |
#wait_until_exists(options = {}) ⇒ Bucket
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/aws-sdk-s3/bucket.rb', line 89 def wait_until_exists( = {}) , params = () waiter = Waiters::BucketExists.new() yield_waiter_and_warn(waiter, &Proc.new) if block_given? waiter.wait(params.merge(bucket: @name)) Bucket.new({ name: @name, client: @client }) end |
#wait_until_not_exists(options = {}) ⇒ Bucket
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/aws-sdk-s3/bucket.rb', line 106 def wait_until_not_exists( = {}) , params = () waiter = Waiters::BucketNotExists.new() yield_waiter_and_warn(waiter, &Proc.new) if block_given? waiter.wait(params.merge(bucket: @name)) Bucket.new({ name: @name, client: @client }) end |
#website ⇒ BucketWebsite
646 647 648 649 650 651 |
# File 'lib/aws-sdk-s3/bucket.rb', line 646 def website BucketWebsite.new( bucket_name: @name, client: @client ) end |