Set up Rails Active Storage to use Linode Object Storage
December 30, 2023
Linode provides S3 compatible storage and it's super easy to set up in your Rails app. If you have a Linode account and want to get S3 compatible storage, simply click on Object Storage from the left hand menu from within your account and then create a bucket.
First, add the gem to your Gemfile like below.
gem "aws-sdk-s3", require: false
Then install it.
bundle install
The edit your Rails credentials file
rails credentials:edit
Then add the following to your Rails credentials file.
linode:
access_key_id: YOUR-ACCESS-KEY-ID
secret_access_key: YOUR-ACCESS-SECRET-KEY
You can use your existing key pairs or generate a new one specifically for your app.
Now modify your config/storage.yml to add Linode.
# config/storage.yml
linode:
service: S3
endpoint: https://us-ord-1.linodeobjects.com #make sure to use the endpoint for your region
access_key_id: <%= Rails.application.credentials.dig(:linode, :access_key_id) %>
secret_access_key: <%= Rails.application.credentials.dig(:linode, :secret_access_key) %>
region: default
bucket: your-bucket-name
public: true
Now you can use Linode Object Storage in production and/or development.
To use in production, edit config/environments/production.rb like this.
# config/environments/production.rb
# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :linode
You can change the setting in development, as well, if you want to use your Linode bucket in favor of local storage.
Comments