Integrating AWS S3 with Ruby on Rails

Total Blog Views: 81

Blog Status: publish

Created By: swaz_ahmed Created at: 06-20-2024

Tags: ror AWS S3

AWS S3 (Simple Storage Service) is a scalable storage solution provided by Amazon Web Services, ideal for storing and retrieving any amount of data from anywhere on the web. Integrating S3 with a Ruby on Rails application can provide a robust way to manage file uploads and storage. In this blog post, we'll walk through the process of setting up AWS S3 with a Rails application.

Prerequisites
Before we get started, ensure you have the following:

  1. An AWS account.
  2. AWS CLI installed and configured on your local machine.
  3. A Rails application.
  4. The aws-sdk-s3 gem installed in your Rails application.

Step 1: Setting Up Your S3 Bucket

Create a Bucket:

  1. Sign in to the AWS Management Console.
  2. Navigate to the S3 service.
  3. Click "Create bucket."
  4. Enter a unique bucket name and select a region.
  5. Leave the rest of the settings as default (you can modify these later based on your needs).
  6. Click "Create bucket."

Configure Bucket Permissions:

  1. Select your newly created bucket.
  2. Go to the "Permissions" tab.
  3. Adjust the bucket policy and CORS configuration if needed. For basic use, the default settings should suffice.

Step 2: Configuring AWS Credentials

Generate AWS Access Keys:

  1. Go to the IAM (Identity and Access Management) service in the AWS Management Console.
  2. Create a new user with programmatic access.
  3. Attach the "AmazonS3FullAccess" policy to the user.
  4. Save the access key ID and secret access key.

Store Credentials Securely:

  • Add your AWS credentials to your Rails application using encrypted credentials or environment variables.

Step 3: Installing and Configuring the aws-sdk-s3 Gem

Add the Gem to Your Gemfile:

 

     gem 'aws-sdk-s3', '~> 1.96'

 

Bundle Install:

 

bundle install

 

Step 4: Configuring Rails to Use S3 

Set Up Initializer:

Create a new initializer file config/initializers/aws.rb and add the following configuration:

 

Aws.config.update({
  region: ENV['AWS_REGION'],
  credentials: Aws::Credentials.new(
    ENV['AWS_ACCESS_KEY_ID'],
    ENV['AWS_SECRET_ACCESS_KEY']
  )
})
S3_BUCKET = Aws::S3::Resource.new.bucket(ENV['S3_BUCKET_NAME'])

 

 

Configure Active Storage (if using Active Storage for file uploads): 

Run the following command to install Active Storage:

 

rails active_storage:install

 

Migrate the database: 

 

rails db:migrate

 

In your config/storage.yml, add the S3 configuration:

 

amazon:
  service: S3
  access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
  secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
  region: <%= Rails.application.credentials.dig(:aws, :region) %>
  bucket: <%= Rails.application.credentials.dig(:aws, :bucket) %>

 

Update config/environments/production.rb to use S3 for Active Storage:

 

config.active_storage.service = :amazon

 

Step 5: Implementing File Uploads 

Add File Upload to a Model:
Let's assume you have a Post model and you want to add an image upload:

 

class Post < ApplicationRecord
  has_one_attached :image
end

 

Update the Form to Allow File Upload: 

 

<%= form_with(model: @post, local: true) do |form| %>
  <!-- other fields -->
  <div class="field">
    <%= form.label :image %>
    <%= form.file_field :image %>
  </div>
  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

 

Display the Uploaded Image: 

 

 <% if @post.image.attached? %>
  <%= image_tag @post.image %>
<% end %>

 

Conclusion: 
Integrating AWS S3 with your Ruby on Rails application provides a scalable and reliable solution for handling file uploads. With S3, you can ensure your application can handle large amounts of data efficiently. By following the steps outlined in this post, you can easily set up S3 in your Rails application and start leveraging the power of cloud storage.


swaz_ahmed

I am swaz_ahmed blogger on shadbox. I am influencer,content writer,author and publisher. Feel free to ask me any question and suggestions.



Comments



Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads