我在我的应用程序中有一个下载链接,用户应该能够从中下载在s3上存储的文件。这些文件将在类似以下的URL上公开访问:
https://s3.amazonaws.com/:bucket_name/:path/:to/:file.png
下载链接触发了我控制器中的一个操作:
class AttachmentsController < ApplicationController
def show
@attachment = Attachment.find(params[:id])
send_file(@attachment.file.url, disposition: 'attachment')
end
end
但是,当我尝试下载文件时,我收到以下错误:
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地址可以确认该文件确实存在且公开可访问。
我该如何允许用户下载S3文件?
stream
和buffer_size
选项 https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/data_streaming.rb http://api.rubyonrails.org/classes/ActionController/DataStreaming.html - equivalent8article = Article.find params[:id]
file_data = open(article.file.url)
send_data file_data.read, filename: article.filename, type: article.file.content_type, disposition: 'attachment'
- ArnoHolo