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

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

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"

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

Options Hash (options):

  • :multipart_threshold (Integer) — default: 15728640

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

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