如何直接做我上传到S3与回形针has_attached_file连接到一个模式?回形针、我上、连接到、直接

2023-09-11 09:16:52 作者:-忘记我们说过的诺言

我的应用程序允许用户上传图片和歌曲,然后下载它们。我也用has_attached_file用回形针正在以它的型号连接。

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.

图像速度快,很容易得到调整。然而,当用户上传的歌曲(有时10 MB)它是很慢的,因为它上传到Heroku和然后至S3(而不是将直)。

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).

什么是我建立一个直接上传,同时还具有连接到模型中的文件的最好方法是什么?我已经签出的SOverflow +谷歌一些答案,并看着S3-'swf上传-插件,fancyupload,等等......但我很好奇,如果有一个更优雅/简便的方法。

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!

推荐答案

见的直接到S3 POST形式本教程。

如果直接上传到S3,你不会用曲别针。为了把它连接到你的模型,我觉得你根据你的记录一些独特的价值(如ID或的唯一令牌)。然后,使用success_action_redirect值的形式将让亚马逊回来的URL在你的应用程序(包括参数)。使用这些参数确认上载。

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.