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.
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.
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
6. The model with delayed job method