Direct uploads to S3 with rails with delayed job

2016-07-27

We have done many file uploading with paperclip and it has been very helpful in helping us to resize, compress, and reformat our attached files. 
However, we will face timeout issue when some users are trying to upload a large file or the internet is slow.  
In order to solve timeout issue, we will need to implement the S3 direct upload. This is how we can implement.

Gems
s3_direct_upload
delayed_job
paperclip
aws-sdk

Flow
1. When user submits a form with image, it will upload the image to S3 via s3_direct_upload without going through the rails app.
2. The app will keep the temporarily attachment url from the s3_direct_upload callback.
3. The app will submit the form and process the image via delayed_job. 

Project Setup
1. AWS S3 bucket setup for CORS configuration:
2. Add s3_direct_upload.rb to config/initializers
S3DirectUpload.config do |c| c.access_key_id = ENV['AWS_ACCESS_KEY_ID'] c.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY'] c.bucket = ENV['AWS_DIRECT_BUCKET'] end
3. S3 direct upload form  
4. S3 form coffeescript initialize
$('#s3-form).S3Uploader click_submit_target: $(".add-images")
5. The controller to process the image
@model.delay.process_images(“S3_image_url”)
6. The model with delayed job method


Contact us about our engineering and design services


Let's Talk

Contact Us