Object — AWS SDK for Ruby V3 (original) (raw)

Constructor Details

#initialize(bucket_name, key, options = {}) ⇒ Object #initialize(options = {}) ⇒ Object

Returns a new instance of Object.

| 24 25 26 27 28 29 30 31 | # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 24 def initialize(*args) options = Hash === args.last ? args.pop.dup : {} @bucket_name = (args, options) @key = (args, options) @data = options.delete(:data) @client = options.delete(:client) || Client.new(options) @waiter_block_warned = false end | | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

Instance Method Details

#accept_ranges ⇒ String

Indicates that a range of bytes was specified.

59 60 61 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 59 def accept_ranges data[:accept_ranges] end

#aclObjectAcl

3401 3402 3403 3404 3405 3406 3407 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 3401 def acl ObjectAcl.new( bucket_name: @bucket_name, object_key: @key, client: @client ) end

#archive_status ⇒ String

The archive state of the head object.

This functionality is not supported for directory buckets.

122 123 124 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 122 def archive_status data[:archive_status] end

#bucketBucket

3410 3411 3412 3413 3414 3415 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 3410 def bucket Bucket.new( name: @bucket_name, client: @client ) end

#bucket_key_enabled ⇒ Boolean

Indicates whether the object uses an S3 Bucket Key for server-side encryption with Key Management Service (KMS) keys (SSE-KMS).

379 380 381 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 379 def bucket_key_enabled data[:bucket_key_enabled] end

#bucket_name ⇒ String

36 37 38 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 36 def bucket_name @bucket_name end

#cache_control ⇒ String

Specifies caching behavior along the request/reply chain.

266 267 268 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 266 def cache_control data[:cache_control] end

#checksum_crc32 ⇒ String

The Base64 encoded, 32-bit CRC32 checksum of the object. This checksum is only be present if the checksum was uploaded with the object. When you use an API operation on an object that was uploaded using multipart uploads, this value may not be a direct checksum value of the full object. Instead, it's a calculation based on the checksum values of each individual part. For more information about how checksums are calculated with multipart uploads, see Checking object integrity in the Amazon S3 User Guide.

151 152 153 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 151 def checksum_crc32 data[:checksum_crc32] end

#checksum_crc32c ⇒ String

The Base64 encoded, 32-bit CRC32C checksum of the object. This checksum is only present if the checksum was uploaded with the object. When you use an API operation on an object that was uploaded using multipart uploads, this value may not be a direct checksum value of the full object. Instead, it's a calculation based on the checksum values of each individual part. For more information about how checksums are calculated with multipart uploads, see Checking object integrity in the Amazon S3 User Guide.

168 169 170 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 168 def checksum_crc32c data[:checksum_crc32c] end

#checksum_crc64nvme ⇒ String

180 181 182 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 180 def checksum_crc64nvme data[:checksum_crc64nvme] end

#checksum_sha1 ⇒ String

The Base64 encoded, 160-bit SHA1 digest of the object. This will only be present if the object was uploaded with the object. When you use the API operation on an object that was uploaded using multipart uploads, this value may not be a direct checksum value of the full object. Instead, it's a calculation based on the checksum values of each individual part. For more information about how checksums are calculated with multipart uploads, see Checking object integrityin the Amazon S3 User Guide.

197 198 199 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 197 def checksum_sha1 data[:checksum_sha1] end

#checksum_sha256 ⇒ String

The Base64 encoded, 256-bit SHA256 digest of the object. This will only be present if the object was uploaded with the object. When you use an API operation on an object that was uploaded using multipart uploads, this value may not be a direct checksum value of the full object. Instead, it's a calculation based on the checksum values of each individual part. For more information about how checksums are calculated with multipart uploads, see Checking object integrityin the Amazon S3 User Guide.

214 215 216 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 214 def checksum_sha256 data[:checksum_sha256] end

#checksum_type ⇒ String

The checksum type, which determines how part-level checksums are combined to create an object-level checksum for multipart objects. You can use this header response to verify that the checksum type that is received is the same checksum type that was specified inCreateMultipartUpload request. For more information, see Checking object integrity in the Amazon S3 User Guide.

229 230 231 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 229 def checksum_type data[:checksum_type] end

#clientClient

546 547 548 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 546 def client @client end

#content_disposition ⇒ String

Specifies presentational information for the object.

272 273 274 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 272 def content_disposition data[:content_disposition] end

#content_encoding ⇒ String

Indicates 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.

280 281 282 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 280 def content_encoding data[:content_encoding] end

#content_language ⇒ String

The language the content is in.

286 287 288 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 286 def content_language data[:content_language] end

#content_length ⇒ Integer Also known as:size

Size of the body in bytes.

134 135 136 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 134 def content_length data[:content_length] end

#content_range ⇒ String

The portion of the object returned in the response for a GETrequest.

299 300 301 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 299 def content_range data[:content_range] end

#content_type ⇒ String

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

292 293 294 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 292 def content_type data[:content_type] end

#copy_from(source, options = {}) ⇒ Object

Copies another object to this object. Use multipart_copy: truefor large objects. This is required for objects that exceed 5GB.

1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 1469 def copy_from(options = {}) options = options.merge( bucket: @bucket_name, key: @key ) resp = Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do @client.copy_object(options) end resp.data end

#copy_to(target, options = {}) ⇒ Object

Note:

If you need to copy to a bucket in a different region, use#copy_from.

Copies this object to another object. Use multipart_copy: truefor large objects. This is required for objects that exceed 5GB.

121 122 123 124 125 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/object.rb', line 121 def copy_to(target, options = {}) Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do ObjectCopier.new(self, options).copy_to(target, options) end end

#dataTypes::HeadObjectOutput

571 572 573 574 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 571 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.

579 580 581 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 579 def data_loaded? !!@data end

#delete(options = {}) ⇒ Types::DeleteObjectOutput

1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 1578 def delete(options = {}) options = options.merge( bucket: @bucket_name, key: @key ) resp = Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do @client.delete_object(options) end resp.data end

#delete_marker ⇒ Boolean

Specifies whether the object retrieved was (true) or was not (false) a Delete Marker. If false, this response header does not appear in the response.

This functionality is not supported for directory buckets.

53 54 55 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 53 def delete_marker data[:delete_marker] end

#download_file(destination, options = {}) ⇒ Boolean

Downloads a file in S3 to a path on disk.

# small files (< 5MB) are downloaded in a single API call
obj.download_file('/path/to/file')

Files larger than 5MB are downloaded using multipart method

# large files are split into parts
# and the parts are downloaded in parallel
obj.download_file('/path/to/very_large_file')

You can provide a callback to monitor progress of the download:

# bytes and part_sizes are each an array with 1 entry per part
# part_sizes may not be known until the first bytes are retrieved
progress = Proc.new do |bytes, part_sizes, file_size|
  puts bytes.map.with_index { |b, i| "Part #{i+1}: #{b} / #{part_sizes[i]}"}.join(' ') + "Total: #{100.0 * bytes.sum / file_size}%" }
end
obj.download_file('/path/to/file', progress_callback: progress)
552 553 554 555 556 557 558 559 560 561 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/object.rb', line 552 def download_file(destination, options = {}) downloader = FileDownloader.new(client: client) Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do downloader.download( destination, options.merge(bucket: bucket_name, key: key) ) end true end

#etag ⇒ String

An entity tag (ETag) is an opaque identifier assigned by a web server to a specific version of a resource found at a URL.

236 237 238 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 236 def etag data[:etag] end

#exists?(options = {}) ⇒ Boolean

Returns true if the Object exists.

586 587 588 589 590 591 592 593 594 595 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 586 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

#expiration ⇒ String

If the object expiration is configured (see PutBucketLifecycleConfiguration ), the response includes this header. It includes the expiry-date and rule-id key-value pairs providing object expiration information. The value of the rule-id is URL-encoded.

Object expiration information is not returned in directory buckets and this header returns the value "NotImplemented" in all responses for directory buckets.

79 80 81 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 79 def expiration data[:expiration] end

#expires ⇒ Time

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

305 306 307 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 305 def expires data[:expires] end

#expires_string ⇒ String

310 311 312 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 310 def expires_string data[:expires_string] end

#get(options = {}, &block) ⇒ Types::GetObjectOutput

1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 1834 def get(options = {}, &block) options = options.merge( bucket: @bucket_name, key: @key ) resp = Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do @client.get_object(options, &block) end resp.data end

#head(options = {}) ⇒ Types::HeadObjectOutput

3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 3387 def head(options = {}) options = options.merge( bucket: @bucket_name, key: @key ) resp = Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do @client.head_object(options) end resp.data end

#initiate_multipart_upload(options = {}) ⇒ MultipartUpload

2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 2418 def initiate_multipart_upload(options = {}) options = options.merge( bucket: @bucket_name, key: @key ) resp = Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do @client.create_multipart_upload(options) end MultipartUpload.new( bucket_name: @bucket_name, object_key: @key, id: resp.data.upload_id, client: @client ) end

#key ⇒ String

41 42 43 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 41 def key @key end

#last_modified ⇒ Time

Date and time when the object was last modified.

128 129 130 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 128 def last_modified data[:last_modified] end

#load ⇒ self Also known as:reload

Loads, or reloads #data for the current Aws::S3::Object. Returns self making it possible to chain methods.

object.reload.data
556 557 558 559 560 561 562 563 564 565 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 556 def load resp = Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do @client.head_object( bucket: @bucket_name, key: @key ) end @data = resp.data self end

#metadata ⇒ Hash<String,String>

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

340 341 342 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 340 def metadata data[:metadata] end

#missing_meta ⇒ Integer

This is set to the number of metadata entries not returned inx-amz-meta headers. This can happen if you create metadata using an API like SOAP that supports more flexible metadata than the REST API. For example, using SOAP, you can create metadata whose values are not legal HTTP headers.

This functionality is not supported for directory buckets.

250 251 252 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 250 def missing_meta data[:missing_meta] end

#move_to(target, options = {}) ⇒ void

This method returns an undefined value.

Copies and deletes the current object. The object will only be deleted if the copy operation succeeds.

135 136 137 138 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/object.rb', line 135 def move_to(target, options = {}) copy_to(target, options) delete end

#multipart_upload(id) ⇒ MultipartUpload

3419 3420 3421 3422 3423 3424 3425 3426 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 3419 def multipart_upload(id) MultipartUpload.new( bucket_name: @bucket_name, object_key: @key, id: id, client: @client ) end

Specifies whether a legal hold is in effect for this object. This header is only returned if the requester has thes3:GetObjectLegalHold permission. This header is not returned if the specified version of this object has never had a legal hold applied. For more information about S3 Object Lock, see Object Lock.

This functionality is not supported for directory buckets.

539 540 541 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 539 def object_lock_legal_hold_status data[:object_lock_legal_hold_status] end

#object_lock_mode ⇒ String

The Object Lock mode, if any, that's in effect for this object. This header is only returned if the requester has thes3:GetObjectRetention permission. For more information about S3 Object Lock, see Object Lock.

This functionality is not supported for directory buckets.

509 510 511 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 509 def object_lock_mode data[:object_lock_mode] end

#object_lock_retain_until_date ⇒ Time

The date and time when the Object Lock retention period expires. This header is only returned if the requester has thes3:GetObjectRetention permission.

This functionality is not supported for directory buckets.

521 522 523 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 521 def object_lock_retain_until_date data[:object_lock_retain_until_date] end

#parts_count ⇒ Integer

The count of parts this object has. This value is only returned if you specify partNumber in your request and the object was uploaded as a multipart upload.

474 475 476 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 474 def parts_count data[:parts_count] end

#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.

See the PresignedPost documentation for more information.

149 150 151 152 153 154 155 156 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/object.rb', line 149 def presigned_post(options = {}) PresignedPost.new( client.config.credentials, client.config.region, bucket_name, { key: key, url: bucket.url }.merge(options) ) end

#presigned_request(method, params = {}) ⇒ String, Hash

Allows you to create presigned URL requests for S3 operations. This method returns a tuple containing the URL and the signed X-amz-* headers to be used with the presigned url.

293 294 295 296 297 298 299 300 301 302 303 304 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/object.rb', line 293 def presigned_request(method, params = {}) presigner = Presigner.new(client: client) if %w(delete head get put).include?(method.to_s) method = "#{method}_object".to_sym end presigner.presigned_request( method.downcase, params.merge(bucket: bucket_name, key: key) ) end

#presigned_url(method, params = {}) ⇒ String

Generates a pre-signed URL for this object.

220 221 222 223 224 225 226 227 228 229 230 231 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/object.rb', line 220 def presigned_url(method, params = {}) presigner = Presigner.new(client: client) if %w(delete head get put).include?(method.to_s) method = "#{method}_object".to_sym end presigner.presigned_url( method.downcase, params.merge(bucket: bucket_name, key: key) ) end

#public_url(options = {}) ⇒ String

Returns the public (un-signed) URL for this object.

s3.bucket('bucket-name').object('obj-key').public_url
#=> "https://bucket-name.s3.amazonaws.com/obj-key"

To use virtual hosted bucket url. Uses https unless secure: false is set. If the bucket name contains dots (.) then you will need to set secure: false.

s3.bucket('my-bucket.com').object('key')
  .public_url(virtual_host: true)
#=> "https://my-bucket.com/key"

| 328 329 330 331 332 333 | # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/object.rb', line 328 def public_url(options = {}) url = URI.parse(bucket.url(options)) url.path += '/' unless url.path[-1] == '/' url.path += key.gsub(/[^\/]+/) { |s| Seahorse::Util.uri_escape(s) } url.to_s end | | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

#put(options = {}) ⇒ Types::PutObjectOutput

3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 3047 def put(options = {}) options = options.merge( bucket: @bucket_name, key: @key ) resp = Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do @client.put_object(options) end resp.data end

#replication_status ⇒ String

Amazon S3 can return this header if your request involves a bucket that is either a source or a destination in a replication rule.

In replication, you have a source bucket on which you configure replication and destination bucket or buckets where Amazon S3 stores object replicas. When you request an object (GetObject) or object metadata (HeadObject) from these buckets, Amazon S3 will return thex-amz-replication-status header in the response as follows:

For more information, see Replication.

This functionality is not supported for directory buckets.

466 467 468 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 466 def replication_status data[:replication_status] end

#request_charged ⇒ String

417 418 419 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 417 def request_charged data[:request_charged] end

#restore ⇒ String

If the object is an archived object (an object whose storage class is GLACIER), the response includes this header if either the archive restoration is in progress (see RestoreObject or an archive copy is already restored.

If an archive copy is already restored, the header value indicates when Amazon S3 is scheduled to delete the object copy. For example:

x-amz-restore: ongoing-request="false", expiry-date="Fri, 21 Dec 2012 00:00:00 GMT"

If the object restoration is in progress, the header returns the valueongoing-request="true".

For more information about archiving objects, see Transitioning Objects: General Considerations.

This functionality is not supported for directory buckets. Directory buckets only support EXPRESS_ONEZONE (the S3 Express One Zone storage class) in Availability Zones and ONEZONE_IA (the S3 One Zone-Infrequent Access storage class) in Dedicated Local Zones.

112 113 114 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 112 def restore data[:restore] end

#restore_object(options = {}) ⇒ Types::RestoreObjectOutput

3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 3188 def restore_object(options = {}) options = options.merge( bucket: @bucket_name, key: @key ) resp = Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do @client.restore_object(options) end resp.data end

#server_side_encryption ⇒ String

The server-side encryption algorithm used when you store this object in Amazon S3 or Amazon FSx.

When accessing data stored in Amazon FSx file systems using S3 access points, the only valid server side encryption option is aws:fsx.

334 335 336 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 334 def server_side_encryption data[:server_side_encryption] end

#sse_customer_algorithm ⇒ String

If server-side encryption with a customer-provided encryption key was requested, the response will include this header to confirm the encryption algorithm that's used.

This functionality is not supported for directory buckets.

352 353 354 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 352 def sse_customer_algorithm data[:sse_customer_algorithm] end

#sse_customer_key_md5 ⇒ String

If server-side encryption with a customer-provided encryption key was requested, the response will include this header to provide the round-trip message integrity verification of the customer-provided encryption key.

This functionality is not supported for directory buckets.

365 366 367 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 365 def sse_customer_key_md5 data[:sse_customer_key_md5] end

#ssekms_key_id ⇒ String

If present, indicates the ID of the KMS key that was used for object encryption.

372 373 374 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 372 def ssekms_key_id data[:ssekms_key_id] end

#storage_class ⇒ String

Provides storage class information of the object. Amazon S3 returns this header for all objects except for S3 Standard storage class objects.

For more information, see Storage Classes.

Directory buckets - Directory buckets only supportEXPRESS_ONEZONE (the S3 Express One Zone storage class) in Availability Zones and ONEZONE_IA (the S3 One Zone-Infrequent Access storage class) in Dedicated Local Zones.

400 401 402 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 400 def storage_class data[:storage_class] end

#tag_count ⇒ Integer

The number of tags, if any, on the object, when you have the relevant permission to read object tags.

You can use GetObjectTagging to retrieve the tag set associated with an object.

This functionality is not supported for directory buckets.

492 493 494 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 492 def tag_count data[:tag_count] end

#upload_file(source, options = {}) {|response| ... } ⇒ Boolean

Uploads a file from disk to the current object in S3.

# small files are uploaded in a single API call
obj.upload_file('/path/to/file')

Files larger than or equal to :multipart_threshold are uploaded using the Amazon S3 multipart upload APIs.

# large files are automatically split into parts
# and the parts are uploaded in parallel
obj.upload_file('/path/to/very_large_file')

The response of the S3 upload API is yielded if a block given.

# API response will have etag value of the file
obj.upload_file('/path/to/file') do |response|
  etag = response.etag
end

You can provide a callback to monitor progress of the upload:

# bytes and totals are each an array with 1 entry per part
progress = Proc.new do |bytes, totals|
  puts bytes.map.with_index { |b, i| "Part #{i+1}: #{b} / #{totals[i]}"}.join(' ') + "Total: #{100.0 * bytes.sum / totals.sum }%" }
end
obj.upload_file('/path/to/file', progress_callback: progress)
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/object.rb', line 470 def upload_file(source, options = {}) uploading_options = options.dup uploader = FileUploader.new( multipart_threshold: uploading_options.delete(:multipart_threshold), client: client ) response = Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do uploader.upload( source, uploading_options.merge(bucket: bucket_name, key: key) ) end yield response if block_given? true end

#upload_stream(options = {}, &block) ⇒ Boolean

Uploads a stream in a streaming fashion to the current object in S3.

Passed chunks automatically split into multipart upload parts and the parts are uploaded in parallel. This allows for streaming uploads that never touch the disk.

Note that this is known to have issues in JRuby until jruby-9.1.15.0, so avoid using this with older versions of JRuby.

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/object.rb', line 385 def upload_stream(options = {}, &block) uploading_options = options.dup uploader = MultipartStreamUploader.new( client: client, thread_count: uploading_options.delete(:thread_count), tempfile: uploading_options.delete(:tempfile), part_size: uploading_options.delete(:part_size) ) Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do uploader.upload( uploading_options.merge(bucket: bucket_name, key: key), &block ) end true end

#version(id) ⇒ ObjectVersion

3430 3431 3432 3433 3434 3435 3436 3437 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 3430 def version(id) ObjectVersion.new( bucket_name: @bucket_name, object_key: @key, id: id, client: @client ) end

#version_id ⇒ String

Version ID of the object.

This functionality is not supported for directory buckets.

260 261 262 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 260 def version_id data[:version_id] end

#wait_until(options = {}) {|resource| ... } ⇒ 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) do |instance|
  instance.state.name == 'running'
end

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

| 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 719 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::Plugins::UserAgent.metric('RESOURCE_MODEL') do Aws::Waiters::Waiter.new(options).wait({}) end end | | ----------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

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

603 604 605 606 607 608 609 610 611 612 613 614 615 616 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 603 def wait_until_exists(options = {}, &block) options, params = separate_params_and_options(options) waiter = Waiters::ObjectExists.new(options) yield_waiter_and_warn(waiter, &block) if block_given? Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do waiter.wait(params.merge(bucket: @bucket_name, key: @key)) end Object.new({ bucket_name: @bucket_name, key: @key, client: @client }) end

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

624 625 626 627 628 629 630 631 632 633 634 635 636 637 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 624 def wait_until_not_exists(options = {}, &block) options, params = separate_params_and_options(options) waiter = Waiters::ObjectNotExists.new(options) yield_waiter_and_warn(waiter, &block) if block_given? Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do waiter.wait(params.merge(bucket: @bucket_name, key: @key)) end Object.new({ bucket_name: @bucket_name, key: @key, client: @client }) end

#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.

This functionality is not supported for directory buckets.

322 323 324 # File 'gems/aws-sdk-s3/lib/aws-sdk-s3/object.rb', line 322 def website_redirect_location data[:website_redirect_location] end