A Complete guide for Implementing Amazon S3 using Rails

  • 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?

gem "aws-sdk-s3", require: fals 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.

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

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.

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
access_key_id: ACCESS_KEY_ID
secret_access_key: SECRET_ACCESS_KEY

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

rails active_storage:install

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.


  • 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

Originally published at https://www.botreetechnologies.com on November 2, 2020.

Leading Ruby on Rails, Python — Django & Mobile App Development Company provides custom web application solutions. Site: www.botreetechnologies.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store