What is Amazon S3?

  • Amazon Simple Storage Service is storage for the internet.
  • A simple web services interface is used to store and retrieve any amount of data from anywhere on the web.
  • Store any type and amount of data that you want; read the same piece of data a million times or only for emergency disaster recovery.

Features of S3:

  • Storage management and monitoring
  • Access management and security
  • Query in place
  • Data transfer

In this article, we will provide a complete Amazon S3 tutorial for implementation using Ruby on Rails. By the end of this, Ruby on Rails developers will learn all the steps to implement Amazon S3 Rails.

How to implement S3 using Rails?

  • Add aws-sdk gem in your Gemfile as shown below:

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

  • bundle the Dependencies

bundle install

  • Create an account on AWS if you haven’t registered on AWS.
  • Get the access_id, secret_key_id of the AWS account if already registered from the IAM section after logging into the AWS console.
AWS management console
  • Edit credentials in rails application for AWS.

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

  • Uncomment the AWS block and add your access key id and secret key id in the file as shown below:

aws:
  access_key_id: ACCESS_KEY_ID
  secret_access_key: SECRET_ACCESS_KEY

  • Log in to the AWS console.
  • Search for Amazon S3 service and create a bucket in the AWS console.
Amazon S3 service and create a bucket
Add bucket name, region for which you want to create a bucket
  • Add bucket name, region for which you want to create a bucket.

Note: If you want to create a bucket with the settings of an existing bucket, then select the bucket from the copy settings dropdown constraining bucket list.

Also Read: AWS S3 File Upload & Access Control Using Boto3 with Django Web Framework

  • Configure options and permissions for the bucket you created.
    • Permission for a bucket can be provided as public/private.
      • For public access, you need to uncheck the Block all public access.
      • For private access, you don’t uncheck the Block all public access.
  • Add the region and bucket name in the credentials.yml as shown below;
EDITOR=nano rails credentials:edit
aws:
access_key_id: ACCESS_KEY_ID
secret_access_key: SECRET_ACCESS_KEY
region: REGION_NAME
bucket: BUCKET_NAME

Note: You can find out the region name in the URL, region as a query parameter in the address bar of the browser.

  • Run the below command to copy active storage migrations.
rails active_storage:install
  • Configurations for staging/production environment to upload on S3.
    • Add the service as amazon in the environment.rb(staging.rb/production.rb)  files.
config.active_storage.service = :amazon
  • Open the model with which you want the image to be attached, like User/Vehicle, etc. 
  • Add the code provided below for attaching the file with the object of that model.
has_one_attached :avatar

Note: Don’t forget to permit the: avatar in params.

  • Try running the application having a file field in the form for the file to be uploaded on Amazon S3 API.
  • If you want to detach the avatar, then use the below code to remove the image from S3 storage.
@user.avatar.purge if @user.avatar.attached?

Similarly, you can attach the PDF/Doc files and display them in the view as per your requirements.

Hurray!! Check out the uploaded file on S3.

Pros:

  • Wide range of cost-effective storage classes.
  • Easily manage data and access control.
  • Query-in-place services for analytics.

Cons: 

  • Permissions can become complex.
  • Searching needs to be improved and is not effective.

Alternatives of AWS S3:

  • Google Cloud Storage
  • Microsoft Azure Storage Service
  • DigitalOcean Storage Service

Code references :

You can find the code references on this repository: https://github.com/HunnyJummani/image_upload_S3 

With the help of expert Ruby on Rails web development services company, you can easily implement Amazon S3 api using Rails. We hope that the above Amazon S3 tutorial provided you clear steps for implementation.

Read more: Upload files to Amazon S3 using Active Storage