Steps for Using Active Storage to Upload Files to S3 with Ruby on Rails

Feature thumb active storage
Starting in Ruby on Rails 5.2, you now have the ability to leverage internal tools for managing file uploads. You can host the images on your server or on ourside services such as S3. This set of steps will walk through how to configure ActiveStorage with S3. The guide assumes that you already have Ruby on Rails 5.2+, a Rails app to work with, and an AWS account.

 

Step 1 - Build Database Tables

Run the command:

rails active_storage:install

Followed by:

rails db:migrate

 

Step 2 - Update Storage Parameters

In the config/storage.yml file, uncomment the 'amazon' configuration options. Make sure to add your region and bucket name.

 

Step 3 - Add AWS Credentials

You can reference this cheat sheet for how to add your AWS credentials to the encrypted credentials file. The encrypted files already have 'aws' setup as an example, so you can simply remove their demo values and replace them with your own.

 

Step 4 - Create a S3 Bucket

In your AWS dashboard, navigate to S3 and create the bucket that you want to store the files in. This bucket name and region should be what you used in the config/storage.yml file.

 

Step 5 - Update Environment Files

Since we're using the local Disk when in development mode and S3 for production, we need to ensure our environment files match our storage.yml file options.

In the config/environments/development.rb there should be a line that says:

config.active_storage.service = :local

And in the config/environments/production.rb you'll need to remove the line:

config.active_storage.service = :local

And add the line:

config.active_storage.service = :amazon

This tells Rails that we're going to use S3 to store the files instead of the server when on production.


Step 6 - Add the AWS S3 Gem

In the Gemfile, add the code:

gem "aws-sdk-s3", require: false

And then run 'bundle install'

 

Step 7 - Configure the Model

You don't need to add a column in the model to store a reference to the image (like you previously had to with tools such as Carrierwave). The tables that were created with the active storage install command will store the references. You simply need to update your model file by letting it know what name should be used to store the image link:

has_one_attached :featured_image

 

Step 8 - Call from the View

Ensure that you have added the new column to the list of controller strong params, and then you can add the file upload element to the view, with code such as:

<%= form.file_field :featured_image %>

 

Step 9 - Rendering from an API

If you happen to need to render the URL from a Rails API application (which is my typical use case), you can create a virtual attribute in the model with code like this:

  def featured_image_url
    if self.featured_image.attachment
      self.featured_image.attachment.service_url
    end
  end

 

For more ways that you can use active storage, the comprehensive guide is below: