January 20, 2021
This blog is part of our Rails 6.1 series.
Rails 6.1 allows environment-specific configuration files to set up Active Storage.
In development, the config/storage/development.yml
file
will take precedence over the config/storage.yml
file.
Similarly, in production, the config/storage/production.yml
file
will take precedence.
If an environment-specific configuration is not present,
Rails will fall back to the configuration declared in config/storage.yml
.
Before Rails 6.1, all storage services were defined in one file,
each environment could set its preferred service in config.active_storage.service
,
and that service would be used for all attachments.
Now we can override the default application-wide storage service for any attachment, like this:
class User < ApplicationModel
has_one_attached :avatar, service: :amazon_s3
end
And we can declare a custom amazon_s3
service in the config/storage.yml
file:
amazon_s3:
service: S3
bucket: "..."
access_key_id: "..."
secret_access_key: "..."
But we are still using the same service for storing avatars in both production and development environments.
To use a separate service per environment, Rails allows the creation of configuration files for each.
Let's change the service to something more generic in the User model:
class User < ApplicationModel
has_one_attached :avatar, service: :store_avatars
end
And add some environment configurations:
For production we'll add config/storage/production.yml
:
store_avatars:
service: S3
bucket: "..."
access_key_id: "..."
secret_access_key: "..."
And for development we'll add config/storage/development.yml
:
store_avatars:
service: Disk
root: <%= Rails.root.join("storage") %>
This will ensure that Rails will store the avatars differently per environment.
Check out the pull request to learn more.
If this blog was helpful, check out our full blog archive.