Search code examples
rubyamazon-s3open-uri

RUby - amazon S3 for remote url attachments - OpenURI::HTTPError: 403 Forbidden


I know this question has been asked several times but none of the solutions provided work for me. I am using Ruby version 2.2.1 with open-uri and trying to add a remote url for my email attachments.

What I have is:

@filename = "#{@doc.id}-#{@company.subdomain}-#{@doc.doc_number}-#{@doc.created_at.strftime("%Y-%m-%d")}.pdf"
@file_path = "https://mybucket.s3.amazonaws.com/docs/doc_#{@doc.id}/#{@doc.filename}"

attachments[@filename] = open(URI.parse("#{@file_path}")).read

I have also used

 1. File.read(open("#{@file_path}")
 2. open(Net::HTTP.get(URI.parse("#{@file_path}"))
 3. Added User-Agent

I removed URI.parse, tried with instance variable and without string interpolation. Nothing works. If I visit a file path on the browser it does work but not when I call it in my mailer.

Any help would be much appreciated.

EDIT: If I just hardcode a working url (no string interpolation) as a file attachment, it works fine.


Solution

  • Well,that was an easy fix but a bit unrelated. I had a typo elsewhere in my code which made filename a little different from the urls I used to test.

    I realized it when I used the following instead for file_path and it worked instantly with:

    file_path = @doc.filename.url
    
    attachments[@file_name] = open(URI.parse("#{file_path}")).read