Uploading Files to S3
Table of Contents
Heroku runs on Amazon Web Services in the US East Region, which means that transfers to the S3 US Standard Region are completely free.
Simple Storage Service (usually known as S3) is a service provided by Amazon to store data. It’s great to store uploaded files from your app since it offers better scalability, reliability, and speed than just storing files on the filesystem.
The most convenient way to send files to S3 is to use an existing plugin like Paperclip or CarrierWave. If you need to support upload for very large files, however, we recommend uploading directly to S3
Concepts
- Bucket: It’s a container for your files, like a directory. All files sent to S3 must belong to a bucket. Buckets must be unique across the whole Amazon system. More about Buckets.
- Access Key Id: Identifies your S3 account. provided by Amazon.
- Secret Access Key: It’s like your S3 password, also provided by Amazon.
Configuring Paperclip
Paperclip is one of the most popular Rails plugins for handling file attachments. Configuring it to use S3 is simple.
Check the Paperclip documentation for other configuration options, including :path, :s3_headers, and more.
Add aws-s3 to your gem manifest or Gemfile, set the storage backend to :s3 and add your S3 credentials as config vars:
class User < ActiveRecord::Base
has_attached_file :photo,
:storage => :s3,
:bucket => 'mybucket',
:s3_credentials => {
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
}
end
Direct Upload
This is the preferred approach if you’re working with file uploads bigger than 4MB. The idea is to skip the hop to your dyno, making a connection from the end user browser directly to S3.
This is done with the S3 SWF Upload Plugin, initially created by ELC Tech and enhanced by Martin Gregory.
Updating Your S3 Credentials
If you find that you need to update your S3 access credentials, start by visiting your Security Credentials page in the AWS portal. In the Access Credentials section, you’ll see a tabbed panel.

AWS allows you to have two access keys at a time, so (assuming you’ve only got one, as in the screenshot here) go ahead and create a new one. Update your Heroku application with the new credentials, and be sure to test that they work. Then, you’re free to return to the AWS portal and deactivate (and delete) your older key.