简体   繁体   中英

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

I am trying to parse a pdf file stored remotely on s3. This is the code I have when I parse the file on my local drive

@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)

I have no issue to obtain the url of the file on s3 using this code

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

However, when I try to open this url as follows

io = open(object.public_url)

I also tried with string interpolation

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

I get the error `open_http': 403 Forbidden (OpenURI::HTTPError)

How can I get around that? What is the right way to open a URL of a file stored on s3? Thanks.

尝试

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM