为用户保持简单
我认为处理此问题的最佳方法是使用过期的S3网址 . 其他方法有以下问题:
文件首先下载到服务器,然后下载到用户 .
使用 send_data 不会产生预期的"browser download" .
绑定Ruby进程 .
需要额外的 download 控制器操作 .
我的实现看起来像这样:
在您的attachment.rb中
def download_url
S3 = AWS::S3.new.buckets[ 'bucket_name' ] # This can be done elsewhere as well,
# e.g config/environments/development.rb
url_options = {
expires_in: 60.minutes,
use_ssl: true,
response_content_disposition: "attachment; filename=\"#{attachment_file_name}\""
}
S3.objects[ self.path ].url_for( :read, url_options ).to_s
end
在您的观点中
而已 .
如果你仍然想出于某种原因保留你的 download 动作,那么就这样使用:
In your attachments_controller.rb
def download
redirect_to @attachment.download_url
end