Setting up Rails 5 Active Storage with Amazon S3
On this tutorial we are going to see all the necessary steps to setup an S3 Amazon Bucket and use it in Rails Active Storage.
Amazon S3 Setup
Creating the bucket
The first thing we need to do is to create a Bucket in S3. Although is pretty simple to do it it could be a little bit tricky if you are doing it for the first time.
Let’s Sign in on our AWS account and go for your S3 management. To accomplish that we need to visit https://aws.amazon.com/ and click on My Account > AWS Management Console on the Top left Corner:
Them provide your credentials on the Sign in page:
and you should see a screen like this:
Search for S3 on the AWS Services search bar if you can’t see it at first glance:
Click on the service and should get a screen like this with an empty list of buckets:
Click on Create bucket and walk through the Wizard:
On the name and region screen the best recommended practice is to name it after the domain of your application if possible.
On the properties screen you can set a group of settings for the bucket. For this tutorial we are not going to dig in on these but you can play around with it. Will leave it with the default values.
On the permissions’ screen we can set users to get access to this bucket but we are going to do it later in IAM’s Amazon Service later so for the moment leave it right how it is and click Next.
This is the confirmation screen you could go ahead and click on Create Bucket.
Adding user policy to grant access to files
Now we are going to create an user and give him the necessary permissions to have read/write access to the S3 Bucket.
Let’s click on Services and search for IAM:
On the IAM service page click on Users and there click on Add User:
In the Add User screen type in a User name and be sure to check the Programmatic access checkbox y Access type section.
After that we must assign the S3 Policy Access in the next screen:
Click on Attach Existing policies directly and search for S3 policies and select AmazonS3FullAccess and click on Next: Review.
On the review screen click on Show in Secret access key column to see the value. Store both of these values: Access key ID and Secret access key in a safe place because we are going to need it to setup Active Storage on Rails later.
Rails Setup
Config storage.yml
The first step to Setup Active Storage on Rails is to setup config/storage.yml
. You should end up with something pretty similar to this:
As you can see we had setup the local and test environments to use local storage on disk on tmp/storage
folder.
Config production.rb/staging.rb environment
In config/environments/production.rb
add the following line:
Using S3 bucket
After all this setup is time to use our bucket to store something in it. For example let’s say that you want to store the user profile picture on Amazon’s S3 bucket we need to add the following line on the User Model:
After this when we need to store the profile picture we should use the following syntax in method Model that we need it, assuming that your pass a parameter of profile_picture
:
Plus: Config Heroku ENV variables
One of the fastest way to test your application in a production like environment is using Heroku. Setting up a new Rails project on Heroku is out of the scope of this tutorial but is pretty straigh forward, check out the Getting Started with Rails 5.x on Heroku guide to see how.
To keep the Amazon’s keys out of your code repository you could store them on the Heroku ENV variables and set your application to use them.
Go to your Heroku instance and click on Reveal Config Vars and set the corresponding keys:
We are going to use these three keys on Rails Active Storage to connect our application to the S3 bucket.
Wrapping Up
As you can see is pretty simple to store file on Amazon’s S3 Buckets with Active Storage in Rails 5, it’s simply painless. Let’s just wrap up the necessary steps:
- Creating the Amazon’s S3 Bucket.
- Add user policy to grant access to files.
- Setup environment variables on Heroku.
- Config
storage.yml
. - Config environments
production.rb
orstaging.rb
. - Use Active Storage to store files on Amazon’s S3 Bucket.
And that’s all folks. I hope this tutorial was helpful and you use Active Storage on your next Rails project.
If you have any question let me know or leave a comment.
Author: Eng. Alberto Aragón Alvarez
- https://www.linkedin.com/in/albertoaragonalvarez/
- alberto@alturasoluciones.com
- https://twitter.com/aragonalvarez85
If you like it, clap. For more histories like this one follow our publication on Medium or subscribe to our mailing list.
If you want to collaborate with us please visit our web site: