When deploying your application, it's always better to use a third-party service to host your images, videos files, etc. Using server storage can greatly increase your monthly or yearly server costs also, some services like Heroku do not allow you to host your files persistently.
We are going to implement Aws 3 as our cloud storage for our Rails application using Active storage. There are other services that you can implement and the setup will be very similar with each requiring its own gem and a few other minor changes.
gem "aws-sdk-s3", require: false
Active Record needs two tables to associate with your models,
Run these commands in your terminal making sure that you are in the project directory.
rails active_storage:install rails db:migrate
Inside your storage.yml file, uncomment the amazon section
In rails 5.2 and up, we can now use
config/credentials.yml.enc. This stores your credentials securely with encryption and requires
config/master.key to decrypt in production.
In your terminal, enter
EDITOR="code --wait" rails credentials:edit
If you use another editor like
atom, vim, or webstorm ,replace "code" with your editor's name.
You should now uncomment these lines and get your
access_key and secret_access_key from your
Identity and Access Management (IAM) on
I added a bucket key but you don't have to. You can add your bucket name in the
When finished, save the file.
You can test this by using
rails console and you should get the values that were entered previously.
Don't forget to use the correct region.
config/environments/development, look for this line (shown in the image below ) and change it to ":amazon"
You should now have the ability to test amazon #####aws3 in development.
Go to your Heroku account.
Go to your settings Tab.
Click Reveal Config Vars.
Add a new key RAILS_MASTER_KEY.
Retrieve the arbitrary text in
yourconfig/master.key and put the text as the value for RAILS_MASTER_KEY.
I hope that this helped make a stress-free setup.