AWS Developer Tools Blog
Uploading Files to Amazon S3
I blogged previously about downloading objects from Amazon S3 using the version 2 AWS SDK for Ruby. It was requested that I write about uploading objects as well.
Managed File Uploads
The simplest and most common task is upload a file from disk to a bucket in Amazon S3. This is very straightforward when using the resource interface for Amazon S3:
s3 = Aws::S3::Resource.new s3.bucket('bucket-name').object('key').upload_file('/source/file/path')
You can pass additional options to the Resource
constructor and to #upload_file
. This expanded example demonstrates configuring the resource client, uploading a public object and then generating a URL that can be used to download the object from a browser.
s3 = Aws::S3::Resource.new( credentials: Aws::Credentials.new('akid', 'secret'), region: 'us-west-1' ) obj = s3.bucket('bucket-name').object('key') obj.upload_file('/source/file/path', acl:'public-read') obj.public_url #=> "https://bucket-name.s3-us-west-1.amazonaws.com/key"
This is the recommended method of using the SDK to upload files to a bucket. Using this approach has the following benefits:
- Manages multipart uploads for objects larger than 15MB.
- Correctly opens files in binary mode to avoid encoding issues.
- Uses multiple threads for uploading parts of large objects in parallel.
Other Methods
In addition to Aws::S3::Object#upload_file
, you can upload an object using #put
or using the multipart upload APIs.
PUT Object
For smaller objects, you may choose to use #put
instead. The #put
method accepts an optional body, which can be a string or any IO object.
obj = s3.bucket('bucket-name').object('key') # from a string obj.put(body:'Hello World!') # from an IO object File.open('/source/file', 'rb') do |file| obj.put(body:file) end
Multipart APIs
I recommend you use #upload_file
whenever possible. If you need to manage large object copies, then you will need to use the multipart interfaces. There are restrictions on the minimum file, and part sizes you should be aware of. Typically these are reserved for advanced use cases.
Feedback
I’d love to hear feedback. If you find the AWS SDK for Ruby lacks a utility for working with Amazon S3, I’d love to hear about it. Please feel free to open a GitHub issue or drop into our Gitter channel.