How to use ActiveStorage in Rails 6 to upload files to s3

Kieran Andrews
3 min readFeb 29, 2020

--

Active storage is a layer inside of rails that helps you connect with cloud services like AWS. Its also connects to other providers like Azure and Google cloud. You can use it to run tasks as well, but in this guide, I will focus on uploading a file to s3 and displaying it inside your rails app.

Note: Have you thought about the environmental impact of the servers you use? Check out this link to look at the environmental impact of the cloud servers you use.

In order to use Active Storage, you will need to set up a database. ActiveStorage should be installed with rails. You can use the rake task bin/rails active_storage:install to generate some migrations that you can run on your database. Run these with bin/rails db:migrate. This will create two tables, active_storage_blobs and active_storage_attachments where the file information and attachments are stored.

One this is installed and run against your database, you will need the s3 gem. You can install this by adding gem "aws-sdk-s3", require: false to your gem file.

Rails should have automatically generated a storage configuration file for you with some examples. You will need to modify the example configuration file at config/storage.yml and uncomment the amazon section, I also commented out the local: section:

You will now need to get your own access key and secret key. You can get these details by logging into Amazon and follow this guide: https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html When you have these you can put them into your secrets for your application.

Rails 6 has a great way of storing credentials. You can use this command to add your secret key and access key: rails credentials:edit --environment production. This should open your credentials in your editor. You can add them like so:

If you want to test this in your local development environment, you will need to change the configuration for your development environment like so:

Now we will generate the models needed that will have the images attached to them. First, start by creating a profile model. We can use the rails generators to create a scaffold to help us get up and running.

rails g scaffold profile name:string description:text

Now, in your profile model, add an attachment. This will create a relationship to those tables we added before

We will also need to allow the controller to accept this new parameter, so in the controller strong params add:

Now, let's add the image to the view. We can do this by adding it to the form to allow for the image to be uploaded:

and then to display the image after it has been created or shown:

That should be everything you need! Now you can run rails s and go to http://localhost:3000/profiles/new in your browser and create a new profile with an image.

Under the hood, Rails uses ActiveStorage::BlobsController and ActiveStorage::DiskController to serve the files up when displaying them in the browser. This controller takes the URL that is requested in the controller and redirects it the asset stored on s3.

You can find my example code at: https://github.com/TigerWolf/rails6_activestorage

If you are looking for a more in depth exploration of Active Storage, you can check out the chapter in my new book, Active Rails

--

--

Kieran Andrews

I enjoy building software, making web applications, surfing, snowboarding and cooking!