简体   繁体   English

如何使用连接到模型的Paperclip has_attached_file直接上传到S3?

[英]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. 我也在使用带有Paperclip的has_attached_file来将它与模型连接起来。

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). 但是,当用户上传一首歌曲(有时是10 MB)时,它非常慢,因为它上传到Heroku然后上传到S3(而不是直接上传)。

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. 我已经在SOverflow + Google上查看了一些答案,并查看过s3-'swf-upload-plugin','fancyupload'等等...但我很好奇是否有更优雅/简单的方法。

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. 有关直接到S3 POST表单的信息,请参阅本教程

If uploading direct to S3, you would not use paperclip. 如果直接上传到S3,则不会使用回形针。 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 ). 为了将它附加到您的模型,我认为您可以根据记录的某些唯一值(如id或唯一令牌 )设置密钥(存储桶中的路径和文件名)。 Then, use the success_action_redirect value in the form so Amazon will come back to an URL in your app (with parameters). 然后,使用表单中的success_action_redirect值,以便亚马逊返回应用中的URL(带参数)。 Use those parameters to confirm the upload. 使用这些参数确认上载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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