Upload Files Directly To S3 Using Paperclip And Dropzone.jsAmit Bairagi

It’s usually the small time-consuming tasks that frustrate us the most. Such as uploading a file to S3; the requirement is pretty simple but the method chosen to upload the file will decide the efficiency of the task. As uploading files is a feature that most applications require, RailsCarma has compiled a brief tutorial on one of the best methods of getting this task done efficiently: using Paperclip and Dropzone.js.

  • Paperclip is a popular choice for uploading images and files as it offers great features to handle the attachments; ‘paperclip’ gem is the go-to option. Paperclip allows you to upload multiple images and files, generate thumbnails and even automatically resize the images. It boasts of a large and active community making it the top choice of most developers.
  • Dropzone.js is an open source library with file drag & drop (with image preview) features.
  • Amazon S3 is a simple storage device for data storage. We can use it to retrieve images and all type of files.

Why Paperclip? Paperclip is a popular file uploading tool for the following reasons:

  • Supports File Caching:If a form fails to validate, we don’t want the user to pick his file again and re-upload it. Therefore, file caching is necessary from a UX standpoint. And it also conserves the bandwidth.

  • Processes Images Paperclip is able to resize and crop images to several different formats thus allowing the developer to choose the library.

  • Simplifies The Task! Paperclip gem does not pollute your code and is easy to test!

  • Allows File Processing Paperclip allows file processing for EXIF data extraction and thumbnail creation of uploaded PDFs, PSDs, DOCs, XLSXs.

  • Provides CDN & Storage-Service Support This is a big plus as we want to keep the bandwidth to our servers as low as possible and avoid possible data loss due to server failure

  • Offers On-The-Fly Processing Paperclip processes images and files on a per-request basis. This is an innovative feature that enables developers to create custom content that adapts best to different situations.

What Are The Dropzone Asynchronous Events?

  • addedfile: When a file is added to the list.

  • removedfile: Used whenever a file is removed from the list. You can listen to this and delete the file from your server if you want to.

  • thumbnail: When the thumbnail has been generated. It receives the data URL as second parameter.

  • error: An error occurred receives the error message as the second parameter. And if the error was due to xmlhttprequest, the xhr object is received as the third parameter.

  • processing: When a file is processed (since there is a queue, not all files are processed immediately). This event was previously called processingfile. drop: The user dropped something onto the drop zone.

How Can We Configure Paperclip In Our Application?

has_attached _file: asset :storage => :s3 :S3_host_name => ENV[“S3_HOST_NAME”] :S3_region => ENV[“S3_REGION”] :S3_protocol => ENV[“S3_PROTOCOL”] :path => “:account_id/:class/:source_id/:attachment/:file_name”,:s3_headers => {‘ContentDisposition’ => ‘attachment’, ‘content-type’ =>‘application/octet_stream’}, :bucket => ENV[“S3_BUCKET”], :s3_credentials => Proc.new{|a| a.instance.s3_credentials} Do_not_validate_attachment_file_type :asset def s3_credentials {:access_key_id => ENV[“S3_ACCESS_KEY_ID”], :secret_access_key => ENV[“S3_SECRET_ACCESS_KEY”]} end

For more tips on Ruby on Rails visit www.railscarma.com

Пожалуйста авторизуйтесь, чтобы добавить комментарий. Вход