使用 send_file 从 Amazon S3 下载文件? [英] Using send_file to download a file from Amazon S3?

查看:28
本文介绍了使用 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.expiring_url(10)

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

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