Total Blog Views: 81
Blog Status: publish
Created By: swaz_ahmed Created at: 06-20-2024
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:
aws-sdk-s3
gem installed in your Rails application.Step 1: Setting Up Your S3 Bucket
Create a Bucket:
Configure Bucket Permissions:
Step 2: Configuring AWS Credentials
Generate AWS Access Keys:
Store Credentials Securely:
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.
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.
© Copyright Shadbox. All Rights Reserved
Rate Blog :
Share on :
Do you have any blog suggestion? please click on the link