Class: Aws::S3::Object

Inherits:
Object
  • Object
show all
Defined in:
aws-sdk-resources/lib/aws-sdk-resources/services/s3/object.rb

Instance Method Summary collapse

Instance Method Details

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

Generates a pre-signed URL for this object.

Examples:

Pre-signed GET URL, valid for one hour


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

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?..."

Parameters:

  • http_method (Symbol)

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

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

    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

Options Hash (params):

  • :exipres_in (Integer) — default: 900

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

Returns:

  • (String)

Raises:

  • (ArgumentError)

    Raised if :expires_in exceeds one week (604800 seconds).


45
46
47
48
49
50
51
# File 'aws-sdk-resources/lib/aws-sdk-resources/services/s3/object.rb', line 45

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_urlString

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"

Returns:

  • (String)

59
60
61
62
63
64
65
66
# File 'aws-sdk-resources/lib/aws-sdk-resources/services/s3/object.rb', line 59

def public_url
  PublicUrl.build(
    endpoint: client.config.endpoint,
    bucket_name: bucket_name,
    object_key: key,
    force_path_style: client.config.force_path_style
  )
end

#upload_file(source, options = {}) ⇒ 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 :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')

Parameters:

  • source (String, Pathname, File, Tempfile)

    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.

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

    a customizable set of options

Options Hash (options):

  • :multipart_threshold (Integer) — default: 15728640

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

Returns:

  • (Boolean)

    Returns true when the object is uploaded without any errors.

Raises:

  • (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.


98
99
100
101
102
103
104
# File 'aws-sdk-resources/lib/aws-sdk-resources/services/s3/object.rb', line 98

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