Upload an Object Using the AWS SDK for Ruby
The AWS SDK for Ruby - Version 3 has two ways of uploading an object to Amazon S3. The first uses a managed file uploader, which makes it easy to upload files of any size from disk. To use the managed file uploader method:
-
Create an instance of the
Aws::S3::Resource
class. -
Reference the target object by bucket name and key. Objects live in a bucket and have unique keys that identify each object.
-
Call
#upload_file
on the object.
require 'aws-sdk-s3' s3 = Aws::S3::Resource.new(region:'us-west-2') obj = s3.bucket('bucket-name').object('key') obj.upload_file('/path/to/source/file')
The second way that AWS SDK for Ruby - Version 3 can upload an object uses the
#put
method of Aws::S3::Object
. This is useful if the object
is a string or an I/O object that is not a file on disk. To use this method:
-
Create an instance of the
Aws::S3::Resource
class. -
Reference the target object by bucket name and key.
-
Call
#put
, passing in the string or I/O object.
require 'aws-sdk-s3' s3 = Aws::S3::Resource.new(region:'us-west-2') obj = s3.bucket('bucket-name').object('key') # I/O object File.open('/path/to/source.file', 'rb') do |file| obj.put(body: file) end