我的应用程序中有一个下载链接,用户应该能够下载存储在s3上的文件.这些文件可以在网址上公开访问,看起来像
https://s3.amazonaws.com/:bucket_name/:path/:to/:file.png
Run Code Online (Sandbox Code Playgroud)
下载链接在我的控制器中执行操作:
class AttachmentsController < ApplicationController
def show
@attachment = Attachment.find(params[:id])
send_file(@attachment.file.url, disposition: 'attachment')
end
end
Run Code Online (Sandbox Code Playgroud)
但是当我尝试下载文件时出现以下错误:
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'
Run Code Online (Sandbox Code Playgroud)
该文件肯定存在,可以在错误消息的URL中公开访问.
如何允许用户下载S3文件?
ruby ruby-on-rails amazon-s3 ruby-on-rails-3 ruby-on-rails-3.2
我有一个锚链接
<a href="http://bucket_name.amazonaws.com/uploads/users/4/songs/7/test.mp3">Download</a>
Run Code Online (Sandbox Code Playgroud)
如何在用户点击它时这样做,它实际上会打开一个弹出窗口,要求用户保存文件而不是尝试在浏览器上播放文件?
编辑:
我正在读这篇文章.
def download
data = open(Song.first.attachment)
send_data data.read, :type => data.content_type, :x_sendfile=>true
end
Run Code Online (Sandbox Code Playgroud)
本文建议使用x_sendfile,因为send_file会占用一个http进程,可能会挂起应用程序直到下载完成.
其次,我使用send_data而不是send_file,如果文件是远程的(即在Amazon S3上托管),这似乎有效.正如本文所述.
我提到的那篇文章是在2009年发布的.是否还需要x_sendfile => true?如果不包含应用程序,它会挂起吗?
我真的应该使用send_data还是send_file?
好吧,我的问题是我正在使用send_data我的Rails 3应用程序向用户发送一个来自AWS S3服务的文件
Base.establish_connection!( :access_key_id => 'my_key', :secret_access_key => 'my_super_secret_key')
s3File = S3Object.find dir+filename, "my_unique_bucket"
send_data(open(s3File.url).read,:filename=>filename, :disposition => 'attachment')
Run Code Online (Sandbox Code Playgroud)
但似乎浏览器正在缓冲文件,并且在缓冲之前将文件发送到下载,不需要时间下载,但buffering当时它只需要文件大小....但我需要的是用户查看正常下载过程,他们只会在浏览器选项卡上知道加载器发生了什么:

他们宁愿看到一个下载过程,我想要弄清楚那里发生了什么
我有什么方法可以做到这一点send_data?