Logo

Home

Services

Products

Projects

Who We Are

Blogs

Contact Us


Ajay Joshi

13 mins to read

2025-01-18

Master Rails Performance Optimization with DEFX Caching Solutions

Introduction

Amazon S3 is a leading cloud storage solution renowned for its simplicity and reliability in managing data. For Rails applications, managing file uploads and downloads often necessitates generating pre-signed URLs to provide temporary access to private files stored in S3. These URLs are secure and time-limited, ideal for maintaining file confidentiality and controlled access. This comprehensive guide will walk you through the process of setting up and utilizing pre-signed URLs in a Rails application, ensuring both security and accessibility for your files.


Prerequisites

Before you begin, ensure you have the following prerequisites:

  • AWS Account: An active AWS account with an S3 bucket.
  • AWS SDK for Ruby: Installed in your Rails application. (https://aws.amazon.com/sdk-for-ruby/)
  • AWS Credentials: Properly configured, either via environment variables or the AWS credentials file.

Setting Up the AWS S3 Client

To interact with Amazon S3, you need to configure the AWS S3 client in your Rails application. This involves setting up an initializer file to specify the AWS region and credentials and defining a constant for your S3 bucket.

Example Configuration:

# config/initializers/aws.rb
Aws.config.update({
 region: 'your-region',
 credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
})
S3_BUCKET = Aws::S3::Resource.new.bucket('your-bucket-name')

Replace 'your-region' and 'your-bucket-name' with your specific AWS region and S3 bucket name. Ensure that your AWS credentials are correctly set in your environment variables.

Generating Pre-signed URLs

Next, create a service class to generate pre-signed URLs. This class will accept parameters such as filename and content type, and generate a URL that expires after a specified period.

Service Class Example:

class PresignedUrlService
  def initialize(filename, content_type)
    @filename = filename
    @content_type = content_type
  end
  def generate_presigned_url
    obj = S3_BUCKET.object(@filename)
    obj.presigned_url(:put, expires_in: 3600, content_type: @content_type)
  end
end

In this example, the generate_presigned_url method creates a pre-signed URL with a 1-hour expiration time.


Using the Service in a Controller

Create a controller action to generate and return the pre-signed URL. This action will receive parameters such as filename and content type, call the service class, and return the URL in a JSON response.

Controller Example:

class UploadsController < ApplicationController
  def generate_presigned_url
    service = PresignedUrlService.new(params[:filename], params[:content_type])
    url = service.generate_presigned_url   render json: { url: url }
  end
end

Routes Configuration

Add the corresponding route in your routes.rb file:

post 'generate_presigned_url', to: 'uploads#generate_presigned_url'

Testing the Endpoint

Test the endpoint using tools like Postman or cURL. Make a POST request to the /generate_presigned_url endpoint with filename and content_type as parameters. The response should include the pre-signed URL in JSON format.

Example cURL Request:

curl -X POST "http://localhost:3000/generate_presigned_url" \
     -d "filename=my-file.txt&content_type=text/plain"

Uploading a File Using the Pre-signed URL

Once you have the pre-signed URL, you can upload a file directly to S3. Use a tool like Postman or cURL to make a PUT request to the pre-signed URL, including the file and the appropriate content type header.

Example cURL Request for Upload:

curl -X PUT "presigned-url" \
     -H "Content-Type: text/plain" \

Replace "presigned-url" with the URL received from the previous step and my-file.txt with the path to your file.

Conclusion

Discover how to seamlessly generate secure pre-signed S3 URLs for your Rails apps to manage file uploads and downloads efficiently.

At DEFX, we specialize in cutting-edge software solutions, blending Rails expertise with AWS integrations for secure, high-performance projects. Enhance your app with expert guidance on advanced features like pre-signed URLs—contact us today to elevate your software capabilities. Visit our website to learn more and join businesses trusting DEFX for their development needs.


Other Blogs

no data

See More

Contact Us

Let’s make your Idea into Reality

Let's Talk

logo
[email protected]