我有我想象的是一个相当常见的设置。
我的rails 3应用程序托管在Heroku上,我使用Paperclip管理文件上传,视频和图像,所有文件都保存在Amazon S3上。文件附加到的模型是Entry,附件本身称为“媒体”。所以,我的回形针设置如下:
class Entry < ActiveRecord::Base
has_attached_file :media, {:storage=>:s3,
:bucket=>"mybucketname",
:s3_credentials=> <credentials hash>}
这一切都很好。但是,我现在想要添加文件的下载链接,以便用户可以下载视频进行编辑。我这样做了如下:
在页面上下载链接:
<p><%= link_to "Download", download_entry_path(entry) %></p>
这只是调用EntriesController中的下载操作,如下所示:
def download
@entry = Entry.find(params[:id])
if @entry.media.file?
send_file @entry.media.to_file, :type => @entry.media_content_type,
:disposition => 'attachment',
:filename => @entry.media_file_name,
:x_sendfile => true
else
flash[:notice] = "Sorry, there was a problem downloading this file"
redirect_to report_path(@entry.report) and return
end
end
由于某些下载量非常大,我希望将下载内容发送到服务器以避免绑定dyno。这就是我使用x_sendfile选项的原因。但是,我认为它没有正确设置:在heroku日志中我可以看到:
2011-06-30T11:57:33+00:00 app[web.1]: X-Accel-Mapping header missing
2011-06-30T11:57:33+00:00 app[web.1]:
2011-06-30T11:57:33+00:00 app[web.1]: Started GET "/entries/7/download" for 77.89.149.137 at 2011-06-30 04:57:33 -0700
2011-06-30T11:57:33+00:00 app[web.1]: ### params = {"action"=>"download", "controller"=>"entries", "id"=>"7"}
2011-06-30T11:57:33+00:00 heroku[router]: GET <my-app>/entries/7/download dyno=web.1 queue=0 wait=0ms service=438ms status=200 bytes=94741
“缺少X-Accel-Mapping标头”消息表明某些事情不对,但我不知道是什么。基本上我不知道heroku的nginx服务器是否自动进行文件下载,如果没有,那么如何告诉它,我在heroku的文档中找不到任何关于它的东西(我可能正在寻找错误的东西)。 / p>
有人能让我直截了当吗?感谢任何建议 - 最多
答案 0 :(得分:3)
我不确定你为什么要通过服务器发送文件。如果它们存储在S3上,为什么不直接链接到它们?
<%= link_to "Download", entry.media.url %>
这样下载完全绕过你的Heroku服务器。