使用由send_file下载来自Amazon S3的文件? [英] Using send_file to download a file from Amazon S3?

查看:253
本文介绍了使用由send_file下载来自Amazon S3的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序下载链接,从哪些用户可以下载它们存储在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,

可以 redirect_to时@ attachment.file.url

这篇关于使用由send_file下载来自Amazon S3的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆