简体   繁体   中英

How do I upload direct to S3 with Paperclip has_attached_file connected to a model?

My app allows users to upload images and songs, and then download them. I'm also using has_attached_file with Paperclip in order to connect it with the models.

Images is fast and gets resized easily. However, when a user uploads a song (sometimes 10 MB) it is very slow because it uploads to Heroku and then to S3 (rather than going direct).

What is the best way for me to set up a direct upload while still having the files attached to the models? I've checked out some answers on SOverflow + Google, and have looked into s3-'swf-upload-plugin', 'fancyupload', etc... but I'm curious if there is a more elegant/easy way.

Current Paperclip setup in music model:

 has_attached_file :music, :storage => :s3,
:bucket => 'mymusicbucket',
:s3_credentials => S3_CREDENTIALS,
:s3_headers => {"Content-Disposition" => "attachment"}

Thanks for the help!

See this tutorial on direct-to-S3 POST forms.

If uploading direct to S3, you would not use paperclip. To get it attached to your model, I think you could just set the key (path and filename in the bucket) based on some unique value for your record (like the id or a unique token ). Then, use the success_action_redirect value in the form so Amazon will come back to an URL in your app (with parameters). Use those parameters to confirm the upload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM