Using AWS S3 Buckets with Heroku and Rails
Upon graduating Flatiron School’s Software Engineering Bootcamp, I wanted to deploy my capstone project that I was so proud of. I thought ahead and initialized my database with Postgresql so it would be compatible with Heroku. What I didn’t take in to account how much storage 78 images takes, let alone all the other data. So what now? Looking at Heroku’s docs, I see that ASW S3 (simple storage solution) Buckets suit my needs.
AWS Setup
You can have up to two access keys to make AWS API calls. Access keys can be found in My Account → Security Credentials. If you don’t already have one, make one. use heroku config:set AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY heroku config:set AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=xxx
to set both keys.
Bucket Setup
- Open the AWS S3 Console
- Create a bucket, the name must be globally unique across all AWS buckets. Make sure to check name constrictions.
- Set object ownership
- Set or block public accessibility.
- Choose your region and create.
File Uploads
For this project, I did direct uploads (not through my app). Because the images can not be modified. Eventually, I want the admin users to be able to use their own images, but this was the best solution for the sake of saving time.
- Add
gem 'aws-sdk', '~> 2'
andgem 'image_processing', '~> 1.2'
to your gemfile andbundle install
. - Make sure you have a .env file to hide your
S3_BUCKET
,AWS_ACCESS_KEY_ID
, andAWS_SECRET_ACCESS_KEY
values. - Set Active Storage services in
config/storage.yml
.


config/environments/production