Nowadays, storing photos, videos, or other types of files in the cloud is common to all applications. Saving files to the cloud has many benefits, such as files stored in the cloud that will be hard to lose, can be used on multiple platforms, optimize app capacity, easy management, .. In this article, I will show you how to store files on Amazon S3 in Rails 6 using Active Storage.
Create an Amazon account and S3 bucket
Create an Amazon account
Create an Amazon account at the Amazon homepage, note that you must enter your creadit card information to use Amazon services.
After successful registration you log in with the root user account that is the account with the email you just created.
Create a bucket
After logging in you will see a screen like this:
Search for S3 in the AWS Services search bar if you can’t see it in the first place:
Click on the S3 service, you will be taken to a screen like this:
Click on Create bucket to go to the bucket creation page
Go to the next screen:
On the properties screen, you can set bucket settings. For this tutorial we won’t go into these, but you can give it a try. Will leave it with the default values.
Next, on the Permissions screen, we can set the user to have access to this bucket but we will do that later in the Amazon service’s IAM settings, so for the moment, let’s keep Leave it as it is and click Next.
Here is a confirmation screen you can go ahead and click Create Bucket
Add a user policy to grant access to the file
Now we will create a user and grant the necessary permissions to have read / write access to the S3 Bucket.
Click Services and search for IAM
On the IAM service page click on User and click Add User
In the Add User screen enter the user name and check Programmatic access under the Access type.
Then we have to specify S3 Policy Access permissions in the next screen:
Click on Attach Existing and search for the S3 policy and select AmazonS3FullAccess , then click Next: Review.
In the Review screen click on Show in the column Secret access key and see the value. Please store both values: Access key ID and Secret access key in a safe place because we will need it to install Active Storage in Rails.
The first step to setting up Active Storage on Rails is to set up
config/storage.yml . The settings are as follows:
root: <%= Rails.root.join("tmp/storage") %>
root: <%= Rails.root.join("storage") %>
# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
access_key_id: <%= ENV["AWS_ACCESS_KEY_ID"] %>
secret_access_key: <%= ENV["AWS_SECRET_ACCESS_KEY"] %>
region: <%= ENV["AWS_REGION"] %>
bucket: <%= ENV["S3_BUCKET_NAME"] %>
The values AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY , AWS_REGION , S3_BUCKET_NAME you will get from the settings in the previous section to save in the environment variable.
Config production.rb / staging.rb / development.rb environment
config.active_storage.service = :amazon
You run the following command to install active_storage into your project
Now in the project directory you will see the migrate file of active storage as follows:
Then run the command
rails db:migrate to create two tables: active_storage_attachments and active_storage_blobs
To save the image we need to create a Model and add a relationship with Active Storage
has_one_attached :image (nếu chỉ lưu 1 ảnh)
has_many_attached :image (nếu lưu nhiều ảnh)
Then, we need to permit params in the controller to receive the image file from the client and save: For an image:
params.require(:product).permit(:title, :description, :image)
For many photos:
params.require(:product).permit(:title, :description, image: )
Get the photo link
Finally to get the image link and review the image we can use the following syntax:
So through this article I have introduced to you the basic steps to be able to save images to the Amazon S3 service environment through active_storage in Rails. Good luck.