简体   繁体   English

如何使用Ruby on Rails / Coffeescript来测试S3 Presigned GET URL以查看其是否已过期,以及如何对其进行重新生成。

[英]How to test an S3 Presigned GET url to see if it has expired, and regenerate it if it has, using Ruby on Rails/Coffeescript

I am building a document management system in Ruby on Rails that uses Amazon S3 for storage. 我正在Ruby on Rails中构建使用Amazon S3进行存储的文档管理系统。 I am using the carrierwave and carrierwave-aws gems for uploading/downloading the files. 我使用的是carrierwave和carrierwave-aws宝石,用于上传/下载文件。

I have it working to where I can generate a presigned url that expires after a certain amount of time (the sooner the better...maybe 10 seconds-1 minute), but the problem I'm having is if someone loads the page and doesn't click the "Download" button right away, then the link expires and they get directed to an ugly XML error. 我有办法在哪里可以生成一个预签名的URL,该URL在一定时间后会过期(越早越好...也许10秒-1分钟),但是我遇到的问题是,如果有人加载了页面并没有立即单击“下载”按钮,则链接失效,并且将它们定向到一个难看的XML错误。

What I'm trying to figure out is either: 我想找出的是:

  1. How can I generate the presigned url on the fly when the download button is clicked (I'm thinking with Coffeescript) OR 单击下载按钮时如何动态生成预签名的URL(我在考虑Coffeescript)或

  2. Go ahead and generate the presigned url on page load, but when download is clicked, somehow check to see if it returns an error and if so, get a new presigned url and redirect to it at that time. 继续并在页面加载时生成预签名的url,但是单击下载后,以某种方式检查它是否返回错误,如果是,则获取新的预签名url并在那时重定向到该URL。 (Again, thinking Coffeescript, if possible) (如果可能,再次考虑使用Coffeescript)

In the past I have taken the following entirely serverside approach to a similar problem (not using carrier wave, but shouldn't be relevant). 过去,我采用了以下完全服务器端的方法来解决类似的问题(不使用载波,但不应该使用)。

Have the download button link to a controller/action in your app, passing the id of the document. 将下载按钮链接到您应用中的控制器/操作,并传递文档的ID。 Your controller action then generates a presigned URL and then redirects the user to that URL. 然后,您的控制器操作会生成一个预签名的URL,然后将用户重定向到该URL。

In theory you would only need a very short lifetime on such a presigned urls, although if the files are large enough that a user might want to pause and resume transfers then a longer lifetime might be advisable. 从理论上讲,您只需要在这样一个预签名的url上保留很短的生存期,尽管如果文件足够大,以至于用户可能希望暂停和继续传输,则建议使用更长的生存期。

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

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