使用由send_file下载来自Amazon S3的文件?文件、send_file、Amazon

由网友(我深知我该努力了)分享简介:我在我的应用程序下载链接,从哪些用户可以下载它们存储在S3上的文件。这些文件将在网址看起来像I have a download link in my app from which users should be able to download files which are stored on s3. These...

我在我的应用程序下载链接,从哪些用户可以下载它们存储在S3上的文件。这些文件将在网址看起来像

I have a download link in my app from which users should be able to download files which are stored on s3. These files will be publicly accessible on urls which look something like

https://s3.amazonaws.com/:bucket_name/:path/:to/:file.png

下载链接点击的动作在我的控制器:

The download link hits an action in my controller:

class AttachmentsController < ApplicationController
  def show
    @attachment = Attachment.find(params[:id])
    send_file(@attachment.file.url, disposition: 'attachment')
  end
end

不过,我得到以下错误,当我尝试下载文件:

But I get the following error when I try to download a file:

ActionController::MissingFile in AttachmentsController#show

Cannot read file https://s3.amazonaws.com/:bucket_name/:path/:to/:file.png
Rails.root: /Users/user/dev/rails/print

Application Trace | Framework Trace | Full Trace
app/controllers/attachments_controller.rb:9:in `show'

该文件确实存在,并且是在URL中的错误信息可公开访问的。

The file definitely exists and is publicly accessible at the url in the error message.

我如何让用户下载S3文件?

How do I allow users to download S3 files?

推荐答案

为了从您的Web服务器发送一个文件,

In order to send a file from your web server,

您需要从S3(见 @ nzajt的回答)或者

可以 redirect_to时@ attachment.file.url

阅读全文

相关推荐

最新文章