简体   繁体   中英

Rails, Heroku, Paperclip and the uploading of files to S3

I'm working on an app that will let users submit zip files that are about ~5mb in size I imagine. I will then take these files manually and work with them on my end.

I'm looking into using Paperclip as heroku suggests for this task - bypassing heroku entirely - and just uploading to Amazon S3.

However now that I'm messing with it it looks like a user model can have ONE attachment, rather than multiple. Is there a better alternative or perhaps a direct way to just upload to S3 knowing my bucket name and keys and such?

Or is there a way to configure paperclip such that a user can have many attachments? I'm a bit confused what I'm doing.

您可以找到许多 可用的 教程来设置带多个附件的回形针。

Paperclip supports an unlimited number of attachments.

Look into the S3 POST API , which will require some signature paramaters generated on your server, but will allow you to upload directly to S3. After a successful upload to S3, you have to make a separate request to notify your heroku server that there is a new attachment and of the attachment filename. Generally this requires hacking the [attachment]_file_name parameter in order to avoid paperclip unnecessarily uploading the file a second time.

Overall I find Paperclip to have some serious design flaws which means it requires unfortunate hacking in order to play nicely with files that are not uploaded through the plugin. I've had to pretty much hack the thing completely. For my next project I'll look for an alternative that more cleanly separates attachements from models and has a more modular storage mechanism.

That said, the solution I mentioned works nicely. The tricky part is to explicitly set the [attachment]_file_name field on the model so that Paperclip will think the file has original file has already been stored.

On popular alternative to paperclip is CarrierWave . When I was looking into file attachments a few months ago it was highly recommended.

S3 Post API can be used but this is going to reveal your secret key in forms which is quite vulnerable from security perspective.

you should use direct uploaderm to s3 js library so your secret key remains secret.

Thanks

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