【问题标题】:Downloading: Paperclip, S3, Heroku, and x_sendfile下载:Paperclip、S3、Heroku 和 x_sendfile
【发布时间】:2011-06-30 20:57:15
【问题描述】:

我有我想象的相当常见的设置。

我的 rails 3 应用程序托管在 Heroku 上,我使用 Paperclip 管理视频和图像的文件上传,所有文件都保存在 Amazon S3 上。文件附加到的模型是条目,附件本身称为“媒体”。所以,我有这样的回形针设置:

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

由于某些下载量会非常大,我想将下载内容集中到服务器上以避免占用测功机。这就是我使用 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 的文档中找不到任何关于它的内容(我可能正在寻找错误的东西)。

任何人都可以纠正我吗?感谢任何建议 - 最大

【问题讨论】:

    标签: ruby ruby-on-rails-3 heroku paperclip x-sendfile


    【解决方案1】:

    我不确定您为什么要通过服务器发送文件。如果它们存储在 S3 上,为什么不直接链接到它们?

    <%= link_to "Download", entry.media.url %>
    

    这样下载就完全绕过了您的 Heroku 服务器。

    【讨论】:

    • 嗨弗兰基。我让它通过控制器的原因是,在某些时候我需要将 s3 存储桶设为私有,以便它只能由应用程序访问。然后,人们将无法右键单击(或按照您的建议单击链接)并直接从其 s3 url 打开文件。基本上我需要控制谁可以下载文件。
    • @Max,您可以请求 S3 为您的私有存储桶中的文件提供一个可超时的下载 URL。见docs.amazonwebservices.com/AmazonS3/latest/dev/…。 Ruby 库(例如 Fog 和 Carrierwave)内置了对经过身份验证的 URL 的支持。
    猜你喜欢
    • 2012-12-30
    • 1970-01-01
    • 2011-01-10
    • 2012-05-02
    • 1970-01-01
    • 2012-02-22
    • 2013-12-23
    • 2016-05-23
    • 1970-01-01
    相关资源
    最近更新 更多