class Aws::S3::Object

Public Instance Methods

presigned_post(options = {}) click to toggle source

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. @note The `:key` is populated by {#key}. Do not specify

the `:key` or `:key_starts_with` options.

@option (see PresignedPost#initialize) @return [PresignedPost] @see PresignedPost

# File lib/aws-sdk-resources/services/s3/object.rb, line 130
def presigned_post(options = {})
  PresignedPost.new(
    client.config.credentials,
    client.config.region,
    bucket_name,
    options.merge(key: key))
end
presigned_url(http_method, params = {}) click to toggle source

Generates a pre-signed URL for this object.

@example Pre-signed GET URL, valid for one hour

obj.presigned_url(:get, expires_in: 3600)
#=> "https://bucket-name.s3.amazonaws.com/object-key?..."

@example Pre-signed PUT with a canned ACL

# the object uploaded using this URL will be publicly accessible
obj.presigned_url(:put, acl: 'public-read')
#=> "https://bucket-name.s3.amazonaws.com/object-key?..."

@param [Symbol] http_method

The HTTP method to generate a presigned URL for. Valid values
are `:get`, `:put`, `:head`, and `:delete`.

@param [Hash] params

Additional request parameters to use when generating the pre-signed
URL. See the related documentation in {Client} for accepted
params.

| HTTP Method   | Client Method          |
|---------------|------------------------|
| `:get`        | {Client#get_object}    |
| `:put`        | {Client#put_object}    |
| `:head`       | {Client#head_object}   |
| `:delete`     | {Client#delete_object} |

@option params [Boolean] :virtual_host (false) When `true` the

presigned URL will use the bucket name as a virtual host.

  bucket = Aws::S3::Bucket.new('my.bucket.com')
  bucket.object('key').presigned_url(virtual_host: true)
  #=> "http://my.bucket.com/key?..."

@option params [Integer] :expires_in (900) Number of seconds before

the pre-signed URL expires. This may not exceed one week (604800
seconds).

@raise [ArgumentError] Raised if `:expires_in` exceeds one week

(604800 seconds).

@return [String]

# File lib/aws-sdk-resources/services/s3/object.rb, line 52
def presigned_url(http_method, params = {})
  presigner = Presigner.new(client: client)
  presigner.presigned_url("#{http_method.downcase}_object", params.merge(
    bucket: bucket_name,
    key: key,
  ))
end
public_url(options = {}) click to toggle source

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 (disables https):

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

@option options [Boolean] :virtual_host (false) When `true`, the bucket

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

@return [String]

# File lib/aws-sdk-resources/services/s3/object.rb, line 75
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
upload_file(source, options = {}) click to toggle source

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 `: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')

@param [String,Pathname,File,Tempfile] source A file or path to a file

on the local file system that should be uploaded to this object.
If you pass an open file object, then it is your responsibility
to close the file object once the upload completes.

@option options [Integer] :multipart_threshold (15728640) Files larger

than `:multipart_threshold` are uploaded using the S3 multipart APIs.
Default threshold is 15MB.

@raise [MultipartUploadError] If an object is being uploaded in

parts, and the upload can not be completed, then the upload is
aborted and this error is raised.  The raised error has a `#errors`
method that returns the failures that caused the upload to be
aborted.

@return [Boolean] Returns `true` when the object is uploaded

without any errors.
# File lib/aws-sdk-resources/services/s3/object.rb, line 112
def upload_file(source, options = {})
  uploader = FileUploader.new(
    multipart_threshold: options.delete(:multipart_threshold),
    client: client)
  uploader.upload(source, options.merge(bucket: bucket_name, key: key))
  true
end