OpenURI::HTTPError 403 Forbidden-打开S3上存储的资产的回形针url(fog-gem)



我调用了我的文档控制器下载操作,为客户端提供从s3检索到的可下载对象。然而,OpenURI似乎在解析回形针存储的url时遇到了问题。这个URL在浏览器中是可以访问的,没有任何问题,但当试图在控制器中打开它时,我得到了一个403 Forbidden错误。

documents_controller
  def download
    data = open(Document.find(params[:id]).upload.url)
    send_data data.read, :type => data.content_type, :x_sendfile => true
  end

一个示例url是

"https://s3.amazonaws.com/mybucket/documents/1/Screen_Shot.png?1372238888"

错误-OpenURI::HTTPError 403禁止当URL被打开时,在动作的第一行出现。知道可能是什么吗?

当URL不是字符串格式时,可能会发生403错误。在类似的邮件附件示例中使用字符串插值对我有效:

doc = order.document
attachments["Order.pdf"] = File.read(open("#{doc}"))

你可以尝试另一件对我有用的事情:添加"用户代理"选项:

data = open(Document.find(params[:id]).upload.url, {'User-Agent' => 'ruby'})

最新更新