简体   繁体   English

从AWS S3下载Rails控制器

[英]rails controller download from aws s3

I am trying to build a really easy way for my users to download audio content from aws via my website. 我正在尝试为我的用户建立一种非常简单的方法,以便通过我的网站从aws下载音频内容。 Here is the flow: 流程如下:

  1. I give the user a download link. 我给用户一个下载链接。 Ex: www.mysite.com/foobar 例如:www.mysite.com/foobar
  2. User clicks on the link. 用户单击链接。
  3. In my rails controller, I create an expiring aws s3 url and automatically start downloading the audio content from that url. 在我的rails控制器中,我创建了一个即将到期的aws s3 url,并自动开始从该url下载音频内容。
  4. User's browser should ask the user whether or not to save the file or not. 用户的浏览器应询问用户是否保存文件。 In the event the user accepts to save the file, I want a callback to my rails app to log that the user actually downloaded the file. 如果用户接受保存文件,我希望我的rails应用程序回调以记录用户实际下载了文件。

So, from a user's perspective, I want the process to be as simple as going to a url I determine, and accepting to download the file when prompted. 因此,从用户的角度来看,我希望该过程像进入我确定的url一样简单,并在出现提示时接受下载文件。

In the background, I want to keep the aws s3 url hidden from the user and I want to have the flexibility to write callback logic after the user accepts the download. 在后台,我想使aws s3 url对用户隐藏,并且我希望在用户接受下载后能够灵活地编写回调逻辑。

What is the recommended way to achieving this? 推荐的实现此目标的方法是什么?

The best way to solve this is to create an S3 URL with a very short (10 minute?) lifetime and return a redirect to the S3 URL. 解决此问题的最佳方法是创建一个寿命很短(10分钟?)的S3 URL,然后将重定向返回到S3 URL。 This does expose the S3 url to the user, but isn't a vulnerability. 这确实向用户公开了S3网址,但这不是一个漏洞。

If you want to hide the S3 URL, you will need to proxy the download through your servers, which is expensive and consumes a worker process for long periods of time. 如果要隐藏S3 URL,则需要通过服务器代理进行下载,这很昂贵,而且会长时间占用工作进程。 I do not recommend this, but it is the only way to conceal the S3 resource. 我不建议这样做,但这是隐藏S3资源的唯一方法。

Additionally, if triggering a download vs. a view is important, you need to set the Content-Disposition header to trigger an attachment download: 此外,如果触发下载与视图比较重要,则需要设置Content-Disposition标头以触发附件下载:

  Content-Disposition: attachment; filename="fname.ext"

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

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