Menu
Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)

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 is a managed file uploader, which makes it easy to upload files of any size from disk.

Uploading a File

1

Create an instance of the Aws::S3::Resource class.

2

Reference the target object by bucket name and key. Objects live in a bucket and have unique keys that identify the object.

2

Call#upload_file on the object.

Copy
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 is to use the #put method of Aws::S3::Object. This is useful if the object is a string or an IO object that is not a file on disk.

Put Object

1

Create an instance of the Aws::S3::Resource class.

2

Reference the target object by bucket name and key.

2

Call#put passing in the string or IO object.

Copy
require 'aws-sdk-s3' s3 = Aws::S3::Resource.new(region:'us-west-2') obj = s3.bucket('bucket-name').object('key') # string data obj.put(body: 'Hello World!') # IO object File.open('source', 'rb') do |file| obj.put(body: file) end