Rails、Heroku、Paperclip 以及将文件上传到 S3

发布于 2025-01-01 05:01:38 字数 282 浏览 1 评论 0原文

我正在开发一个应用程序,它可以让用户提交我想象的大小约为 5mb 的 zip 文件。然后我将手动获取这些文件并在我这边使用它们。

我正在考虑使用 Paperclip 来完成此任务,就像 Heroku 建议的那样 - 完全绕过 Heroku - 并且只是上传到 Amazon S3。

然而,现在我正在搞乱它,看起来用户模型可以有一个附件,而不是多个。有没有更好的替代方案或者直接的方法来上传到 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酷遇一生 2025-01-08 05:01:38

您可以找到许多 可用 教程设置回形针与多个附件一起使用。

You can find many available tutorials to setup paperclip working with multiple attachments.

反目相谮 2025-01-08 05:01:38

回形针支持无限数量的附件。

查看 S3 POST API,这将需要在您的服务器,但允许您直接上传到 S3。成功上传到 S3 后,您必须发出单独的请求来通知您的 heroku 服务器有新附件和附件文件名。通常,这需要破解 [attachment]_file_name 参数,以避免回形针不必要地再次上传文件。

总的来说,我发现 Paperclip 有一些严重的设计缺陷,这意味着它需要不幸的黑客攻击才能很好地处理未通过插件上传的文件。我不得不彻底破解这个东西。对于我的下一个项目,我将寻找一种替代方案,将附件与模型更清晰地分开,并具有更模块化的存储机制。

也就是说,我提到的解决方案效果很好。棘手的部分是在模型上显式设置 [attachment]_file_name 字段,以便 Paperclip 认为该文件具有已存储的原始文件。

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.

寻找我们的幸福 2025-01-08 05:01:38

回形针的流行替代品是 CarrierWave。几个月前,当我查看文件附件时,强烈推荐它。

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

记忆之渊 2025-01-08 05:01:38

可以使用 S3 Post API,但这会以表单形式泄露您的密钥,从安全角度来看,这非常容易受到攻击。

您应该使用直接 uploaderm 到 s3 js 库,以便您的密钥保持秘密。

谢谢

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文