Rails open_uri和amazon s3`open_http':403 Forbidden(OpenURI :: HTTPError)

时间:2017-01-29 21:29:08

标签: ruby-on-rails amazon-web-services amazon-s3 open-uri

我正在尝试解析在s3上远程存储的pdf文件。这是我在本地驱动器上解析文件时的代码

@doc = current_user.docs.order("created_at").last #last file uploaded by user
io = open("#{Rails.root}/public" + @doc.doc.url)
reader = PDF::Reader.new(io)

使用此代码

获取s3上文件的url没有问题
s3 = Aws::S3::Resource.new(
access_key_id: 'XXX',
secret_access_key: 'XXX',
region: 'eu-west-1')
bucket = s3.bucket('my_bucket')
object = bucket.object('mydoc.pdf')
puts object.public_url

但是,当我尝试按以下方式打开此网址时

io = open(object.public_url)

我也尝试过字符串插值

io = open("#{object.public_url}")

我收到错误`open_http':403 Forbidden(OpenURI :: HTTPError)

我怎样才能解决这个问题?打开存储在s3上的文件的URL的正确方法是什么?感谢。

1 个答案:

答案 0 :(得分:1)

io = Net::HTTP.get(URI.parse(object.public_url))