You are viewing documentation for version 3 of the AWS SDK for Ruby. Version 2 documentation can be found here.

Class: Aws::S3::Bucket

Inherits:
Object
  • Object
show all
Defined in:
gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/bucket.rb,
gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb

Defined Under Namespace

Classes: Collection

Actions collapse

Associations collapse

Read-Only Attributes collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Bucket #initialize(options = {}) ⇒ Bucket

Returns a new instance of Bucket

Overloads:

  • #initialize(name, options = {}) ⇒ Bucket

    Parameters:

    • name (String)

    Options Hash (options):

  • #initialize(options = {}) ⇒ Bucket

    Options Hash (options):

    • :name (required, String)
    • :client (Client)


19
20
21
22
23
24
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 19

def initialize(*args)
  options = Hash === args.last ? args.pop.dup : {}
  @name = extract_name(args, options)
  @data = options.delete(:data)
  @client = options.delete(:client) || Client.new(options)
end

Instance Method Details

#aclBucketAcl

Returns:



438
439
440
441
442
443
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 438

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

Examples:


bucket.clear!


14
15
16
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/bucket.rb', line 14

def clear!
  object_versions.batch_delete!
end

#clientClient

Returns:



42
43
44
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 42

def client
  @client
end

#corsBucketCors

Returns:



446
447
448
449
450
451
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 446

def cors
  BucketCors.new(
    bucket_name: @name,
    client: @client
  )
end

#create(options = {}) ⇒ Types::CreateBucketOutput

Examples:

Request syntax with placeholder values


bucket.create({
  acl: "private", # accepts private, public-read, public-read-write, authenticated-read
  create_bucket_configuration: {
    location_constraint: "EU", # accepts EU, eu-west-1, us-west-1, us-west-2, ap-south-1, ap-southeast-1, ap-southeast-2, ap-northeast-1, sa-east-1, cn-north-1, eu-central-1
  },
  grant_full_control: "GrantFullControl",
  grant_read: "GrantRead",
  grant_read_acp: "GrantReadACP",
  grant_write: "GrantWrite",
  grant_write_acp: "GrantWriteACP",
  object_lock_enabled_for_bucket: false,
})

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :acl (String)

    The canned ACL to apply to the bucket.

  • :create_bucket_configuration (Types::CreateBucketConfiguration)
  • :grant_full_control (String)

    Allows grantee the read, write, read ACP, and write ACP permissions on the bucket.

  • :grant_read (String)

    Allows grantee to list the objects in the bucket.

  • :grant_read_acp (String)

    Allows grantee to read the bucket ACL.

  • :grant_write (String)

    Allows grantee to create, overwrite, and delete any object in the bucket.

  • :grant_write_acp (String)

    Allows grantee to write the ACL for the applicable bucket.

  • :object_lock_enabled_for_bucket (Boolean)

    Specifies whether you want Amazon S3 object lock to be enabled for the new bucket.

Returns:



248
249
250
251
252
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 248

def create(options = {})
  options = options.merge(bucket: @name)
  resp = @client.create_bucket(options)
  resp.data
end

#creation_dateTime

Date the bucket was created.

Returns:

  • (Time)


35
36
37
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 35

def creation_date
  data[:creation_date]
end

#dataTypes::Bucket

Returns the data for this Aws::S3::Bucket.

Returns:

Raises:



57
58
59
60
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 57

def data
  load unless @data
  @data
end

#data_loaded?Boolean

Returns true if this resource is loaded. Accessing attributes or #data on an unloaded resource will trigger a call to #load.

Returns:

  • (Boolean)

    Returns true if this resource is loaded. Accessing attributes or #data on an unloaded resource will trigger a call to #load.



65
66
67
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 65

def data_loaded?
  !!@data
end

#delete(options = {}) ⇒ EmptyStructure

Examples:

Request syntax with placeholder values


bucket.delete()

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Returns:

  • (EmptyStructure)


259
260
261
262
263
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 259

def delete(options = {})
  options = options.merge(bucket: @name)
  resp = @client.delete_bucket(options)
  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.

Examples:


bucket.delete!

Parameters:

  • options (Hash) (defaults to: { })

    a customizable set of options

Options Hash (options):

  • :max_attempts (Integer) — default: 3

    Maximum number of times to attempt to delete the empty bucket before raising Aws::S3::Errors::BucketNotEmpty.

  • :initial_wait (Float) — default: 1.3

    Seconds to wait before retrying the call to delete the bucket, exponentially increased for each attempt.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/bucket.rb', line 34

def delete! options = { }
  options = {
    initial_wait: 1.3,
    max_attempts: 3,
  }.merge(options)

  attempts = 0
  begin
    clear!
    delete
  rescue Errors::BucketNotEmpty
    attempts += 1
    if attempts >= options[:max_attempts]
      raise
    else
      Kernel.sleep(options[:initial_wait] ** attempts)
      retry
    end
  end
end

#delete_objects(options = {}) ⇒ Types::DeleteObjectsOutput

Examples:

Request syntax with placeholder values


bucket.delete_objects({
  delete: { # required
    objects: [ # required
      {
        key: "ObjectKey", # required
        version_id: "ObjectVersionId",
      },
    ],
    quiet: false,
  },
  mfa: "MFA",
  request_payer: "requester", # accepts requester
  bypass_governance_retention: false,
})

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :delete (required, Types::Delete)
  • :mfa (String)

    The concatenation of the authentication device's serial number, a space, and the value that is displayed on your authentication device.

  • :request_payer (String)

    Confirms that the requester knows that she or he will be charged for the request. Bucket owners need not specify this parameter in their requests. Documentation on downloading objects from requester pays buckets can be found at http://docs.aws.amazon.com/AmazonS3/latest/dev/ObjectsinRequesterPaysBuckets.html

  • :bypass_governance_retention (Boolean)

    Specifies whether you want to delete this object even if it has a Governance-type object lock in place. You must have sufficient permissions to perform this operation.

Returns:



297
298
299
300
301
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 297

def delete_objects(options = {})
  options = options.merge(bucket: @name)
  resp = @client.delete_objects(options)
  resp.data
end

#exists?(options = {}) ⇒ Boolean

Returns true if the Bucket exists.

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Returns:

  • (Boolean)

    Returns true if the Bucket exists.



72
73
74
75
76
77
78
79
80
81
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 72

def exists?(options = {})
  begin
    wait_until_exists(options.merge(max_attempts: 1))
    true
  rescue Aws::Waiters::Errors::UnexpectedError => e
    raise e.error
  rescue Aws::Waiters::Errors::WaiterFailed
    false
  end
end

#lifecycleBucketLifecycle

Returns:



454
455
456
457
458
459
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 454

def lifecycle
  BucketLifecycle.new(
    bucket_name: @name,
    client: @client
  )
end

#lifecycle_configurationBucketLifecycleConfiguration



462
463
464
465
466
467
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 462

def lifecycle_configuration
  BucketLifecycleConfiguration.new(
    bucket_name: @name,
    client: @client
  )
end

#loggingBucketLogging

Returns:



470
471
472
473
474
475
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 470

def logging
  BucketLogging.new(
    bucket_name: @name,
    client: @client
  )
end

#multipart_uploads(options = {}) ⇒ MultipartUpload::Collection

Examples:

Request syntax with placeholder values


multipart_uploads = bucket.multipart_uploads({
  delimiter: "Delimiter",
  encoding_type: "url", # accepts url
  key_marker: "KeyMarker",
  prefix: "Prefix",
  upload_id_marker: "UploadIdMarker",
})

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :delimiter (String)

    Character you use to group keys.

  • :encoding_type (String)

    Requests Amazon S3 to encode the object keys in the response and specifies the encoding method to use. An object key may contain any Unicode character; however, XML 1.0 parser cannot parse some characters, such as characters with an ASCII value from 0 to 10. For characters that are not supported in XML 1.0, you can add this parameter to request that Amazon S3 encode the keys in the response.

  • :key_marker (String)

    Together with upload-id-marker, this parameter specifies the multipart upload after which listing should begin.

  • :prefix (String)

    Lists in-progress uploads only for those keys that begin with the specified prefix.

  • :upload_id_marker (String)

    Together with key-marker, specifies the multipart upload after which listing should begin. If key-marker is not specified, the upload-id-marker parameter is ignored.

Returns:



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 507

def multipart_uploads(options = {})
  batches = Enumerator.new do |y|
    options = options.merge(bucket: @name)
    resp = @client.list_multipart_uploads(options)
    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

#nameString

Returns:

  • (String)


29
30
31
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 29

def name
  @name
end

#notificationBucketNotification

Returns:



529
530
531
532
533
534
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 529

def notification
  BucketNotification.new(
    bucket_name: @name,
    client: @client
  )
end

#object(key) ⇒ Object

Parameters:

  • key (String)

Returns:



538
539
540
541
542
543
544
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 538

def object(key)
  Object.new(
    bucket_name: @name,
    key: key,
    client: @client
  )
end

#object_versions(options = {}) ⇒ ObjectVersion::Collection

Examples:

Request syntax with placeholder values


object_versions = bucket.object_versions({
  delimiter: "Delimiter",
  encoding_type: "url", # accepts url
  key_marker: "KeyMarker",
  prefix: "Prefix",
  version_id_marker: "VersionIdMarker",
})

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :delimiter (String)

    A delimiter is a character you use to group keys.

  • :encoding_type (String)

    Requests Amazon S3 to encode the object keys in the response and specifies the encoding method to use. An object key may contain any Unicode character; however, XML 1.0 parser cannot parse some characters, such as characters with an ASCII value from 0 to 10. For characters that are not supported in XML 1.0, you can add this parameter to request that Amazon S3 encode the keys in the response.

  • :key_marker (String)

    Specifies the key to start with when listing objects in a bucket.

  • :prefix (String)

    Limits the response to keys that begin with the specified prefix.

  • :version_id_marker (String)

    Specifies the object version you want to start listing from.

Returns:



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 572

def object_versions(options = {})
  batches = Enumerator.new do |y|
    options = options.merge(bucket: @name)
    resp = @client.list_object_versions(options)
    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

Examples:

Request syntax with placeholder values


objects = bucket.objects({
  delimiter: "Delimiter",
  encoding_type: "url", # accepts url
  prefix: "Prefix",
  fetch_owner: false,
  start_after: "StartAfter",
  request_payer: "requester", # accepts requester
})

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :delimiter (String)

    A delimiter is a character you use to group keys.

  • :encoding_type (String)

    Encoding type used by Amazon S3 to encode object keys in the response.

  • :prefix (String)

    Limits the response to keys that begin with the specified prefix.

  • :fetch_owner (Boolean)

    The owner field is not present in listV2 by default, if you want to return owner field with each key in the result then set the fetch owner field to true

  • :start_after (String)

    StartAfter is where you want Amazon S3 to start listing from. Amazon S3 starts listing after this specified key. StartAfter can be any key in the bucket

  • :request_payer (String)

    Confirms that the requester knows that she or he will be charged for the list objects request in V2 style. Bucket owners need not specify this parameter in their requests.

Returns:



623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 623

def objects(options = {})
  batches = Enumerator.new do |y|
    options = options.merge(bucket: @name)
    resp = @client.list_objects_v2(options)
    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

#policyBucketPolicy

Returns:



644
645
646
647
648
649
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 644

def policy
  BucketPolicy.new(
    bucket_name: @name,
    client: @client
  )
end

#presigned_post(options = {}) ⇒ PresignedPost

Note:

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.

Options Hash (options):

Returns:

See Also:



91
92
93
94
95
96
97
98
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/bucket.rb', line 91

def presigned_post(options = {})
  PresignedPost.new(
    client.config.credentials,
    client.config.region,
    name,
    {url: url}.merge(options)
  )
end

#put_object(options = {}) ⇒ Object

Examples:

Request syntax with placeholder values


object = bucket.put_object({
  acl: "private", # accepts private, public-read, public-read-write, authenticated-read, aws-exec-read, bucket-owner-read, bucket-owner-full-control
  body: source_file,
  cache_control: "CacheControl",
  content_disposition: "ContentDisposition",
  content_encoding: "ContentEncoding",
  content_language: "ContentLanguage",
  content_length: 1,
  content_md5: "ContentMD5",
  content_type: "ContentType",
  expires: Time.now,
  grant_full_control: "GrantFullControl",
  grant_read: "GrantRead",
  grant_read_acp: "GrantReadACP",
  grant_write_acp: "GrantWriteACP",
  key: "ObjectKey", # required
  metadata: {
    "MetadataKey" => "MetadataValue",
  },
  server_side_encryption: "AES256", # accepts AES256, aws:kms
  storage_class: "STANDARD", # accepts STANDARD, REDUCED_REDUNDANCY, STANDARD_IA, ONEZONE_IA, INTELLIGENT_TIERING, GLACIER, DEEP_ARCHIVE
  website_redirect_location: "WebsiteRedirectLocation",
  sse_customer_algorithm: "SSECustomerAlgorithm",
  sse_customer_key: "SSECustomerKey",
  sse_customer_key_md5: "SSECustomerKeyMD5",
  ssekms_key_id: "SSEKMSKeyId",
  ssekms_encryption_context: "SSEKMSEncryptionContext",
  request_payer: "requester", # accepts requester
  tagging: "TaggingHeader",
  object_lock_mode: "GOVERNANCE", # accepts GOVERNANCE, COMPLIANCE
  object_lock_retain_until_date: Time.now,
  object_lock_legal_hold_status: "ON", # accepts ON, OFF
})

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :acl (String)

    The canned ACL to apply to the object.

  • :body (String, IO)

    Object data.

  • :cache_control (String)

    Specifies caching behavior along the request/reply chain.

  • :content_disposition (String)

    Specifies presentational information for the object.

  • :content_encoding (String)

    Specifies what content encodings have been applied to the object and thus what decoding mechanisms must be applied to obtain the media-type referenced by the Content-Type header field.

  • :content_language (String)

    The language the content is in.

  • :content_length (Integer)

    Size of the body in bytes. This parameter is useful when the size of the body cannot be determined automatically.

  • :content_md5 (String)

    The base64-encoded 128-bit MD5 digest of the part data. This parameter is auto-populated when using the command from the CLI. This parameted is required if object lock parameters are specified.

  • :content_type (String)

    A standard MIME type describing the format of the object data.

  • :expires (Time, DateTime, Date, Integer, String)

    The date and time at which the object is no longer cacheable.

  • :grant_full_control (String)

    Gives the grantee READ, READ_ACP, and WRITE_ACP permissions on the object.

  • :grant_read (String)

    Allows grantee to read the object data and its metadata.

  • :grant_read_acp (String)

    Allows grantee to read the object ACL.

  • :grant_write_acp (String)

    Allows grantee to write the ACL for the applicable object.

  • :key (required, String)

    Object key for which the PUT operation was initiated.

  • :metadata (Hash<String,String>)

    A map of metadata to store with the object in S3.

  • :server_side_encryption (String)

    The Server-side encryption algorithm used when storing this object in S3 (e.g., AES256, aws:kms).

  • :storage_class (String)

    The type of storage to use for the object. Defaults to 'STANDARD'.

  • :website_redirect_location (String)

    If the bucket is configured as a website, redirects requests for this object to another object in the same bucket or to an external URL. Amazon S3 stores the value of this header in the object metadata.

  • :sse_customer_algorithm (String)

    Specifies the algorithm to use to when encrypting the object (e.g., AES256).

  • :sse_customer_key (String)

    Specifies the customer-provided encryption key for Amazon S3 to use in encrypting data. This value is used to store the object and then it is discarded; Amazon does not store the encryption key. The key must be appropriate for use with the algorithm specified in the x-amz-server-side​-encryption​-customer-algorithm header.

  • :sse_customer_key_md5 (String)

    Specifies the 128-bit MD5 digest of the encryption key according to RFC 1321. Amazon S3 uses this header for a message integrity check to ensure the encryption key was transmitted without error.

  • :ssekms_key_id (String)

    Specifies the AWS KMS key ID to use for object encryption. All GET and PUT requests for an object protected by AWS KMS will fail if not made via SSL or using SigV4. Documentation on configuring any of the officially supported AWS SDKs and CLI can be found at http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#specify-signature-version

  • :ssekms_encryption_context (String)

    Specifies the AWS KMS Encryption Context to use for object encryption. The value of this header is a base64-encoded UTF-8 string holding JSON with the encryption context key-value pairs.

  • :request_payer (String)

    Confirms that the requester knows that she or he will be charged for the request. Bucket owners need not specify this parameter in their requests. Documentation on downloading objects from requester pays buckets can be found at http://docs.aws.amazon.com/AmazonS3/latest/dev/ObjectsinRequesterPaysBuckets.html

  • :tagging (String)

    The tag-set for the object. The tag-set must be encoded as URL Query parameters. (For example, "Key1=Value1")

  • :object_lock_mode (String)

    The object lock mode that you want to apply to this object.

  • :object_lock_retain_until_date (Time, DateTime, Date, Integer, String)

    The date and time when you want this object's object lock to expire.

  • :object_lock_legal_hold_status (String)

    The Legal Hold status that you want to apply to the specified object.

Returns:



425
426
427
428
429
430
431
432
433
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 425

def put_object(options = {})
  options = options.merge(bucket: @name)
  resp = @client.put_object(options)
  Object.new(
    bucket_name: @name,
    key: options[:key],
    client: @client
  )
end

#request_paymentBucketRequestPayment



652
653
654
655
656
657
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 652

def request_payment
  BucketRequestPayment.new(
    bucket_name: @name,
    client: @client
  )
end

#taggingBucketTagging

Returns:



660
661
662
663
664
665
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 660

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')
bucket.url(virtual_host: true)
#=> "http://my.bucket.com"

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :virtual_host (Boolean) — default: false

    When true, the bucket name will be used as the host name. This is useful when you have a CNAME configured for this bucket.

Returns:

  • (String)

    the URL for this bucket.



73
74
75
76
77
78
79
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/bucket.rb', line 73

def url(options = {})
  if options[:virtual_host]
    "http://#{name}"
  else
    s3_bucket_url
  end
end

#versioningBucketVersioning

Returns:



668
669
670
671
672
673
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 668

def versioning
  BucketVersioning.new(
    bucket_name: @name,
    client: @client
  )
end

#wait_until(options = {}, &block) ⇒ Resource

Deprecated.

Use [Aws::S3::Client] #wait_until instead

Note:

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(options) {|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

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :max_attempts (Integer) — default: 10

    Maximum number of

  • :delay (Integer) — default: 10

    Delay between each

  • :before_attempt (Proc) — default: nil

    Callback

  • :before_wait (Proc) — default: nil

    Callback

Returns:

  • (Resource)

    if the waiter was successful

Raises:

  • (Aws::Waiters::Errors::FailureStateError)

    Raised when the waiter terminates because the waiter has entered a state that it will not transition out of, preventing success.

    yet successful.

  • (Aws::Waiters::Errors::UnexpectedError)

    Raised when an error is encountered while polling for a resource that is not expected.

  • (NotImplementedError)

    Raised when the resource does not



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 195

def wait_until(options = {}, &block)
  self_copy = self.dup
  attempts = 0
  options[:max_attempts] = 10 unless options.key?(:max_attempts)
  options[:delay] ||= 10
  options[:poller] = Proc.new do
    attempts += 1
    if block.call(self_copy)
      [:success, self_copy]
    else
      self_copy.reload unless attempts == options[:max_attempts]
      :retry
    end
  end
  Aws::Waiters::Waiter.new(options).wait({})
end

#wait_until_exists(options = {}, &block) ⇒ Bucket

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :max_attempts (Integer) — default: 20
  • :delay (Float) — default: 5
  • :before_attempt (Proc)
  • :before_wait (Proc)

Returns:



89
90
91
92
93
94
95
96
97
98
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 89

def wait_until_exists(options = {}, &block)
  options, params = separate_params_and_options(options)
  waiter = Waiters::BucketExists.new(options)
  yield_waiter_and_warn(waiter, &block) if block_given?
  waiter.wait(params.merge(bucket: @name))
  Bucket.new({
    name: @name,
    client: @client
  })
end

#wait_until_not_exists(options = {}, &block) ⇒ Bucket

Parameters:

  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :max_attempts (Integer) — default: 20
  • :delay (Float) — default: 5
  • :before_attempt (Proc)
  • :before_wait (Proc)

Returns:



106
107
108
109
110
111
112
113
114
115
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 106

def wait_until_not_exists(options = {}, &block)
  options, params = separate_params_and_options(options)
  waiter = Waiters::BucketNotExists.new(options)
  yield_waiter_and_warn(waiter, &block) if block_given?
  waiter.wait(params.merge(bucket: @name))
  Bucket.new({
    name: @name,
    client: @client
  })
end

#websiteBucketWebsite

Returns:



676
677
678
679
680
681
# File 'gems/aws-sdk-s3/lib/aws-sdk-s3/bucket.rb', line 676

def website
  BucketWebsite.new(
    bucket_name: @name,
    client: @client
  )
end