user1854802
user1854802

Reputation: 388

ROR application using Paperclip and Amazon S3 - can store but not retrieve images

In the generated url for the image the region name is missing. I can store but not retrieve images.

For example an image stored at

https://s3-ap-southeast-2.amazonaws.com/drill-investor-bucket/attachments/images/000/000/042/original/image-3.png  

in the application the view

<%= image_tag attachment.image.url %> <br />

generates this address

https://s3.amazonaws.com/drill-investor-bucket/attachments/images/000/000/042/original/image-3.png?1393281373 

I have tried a number of different ways to try to get the region into the view address. In the code below some of these failed attempts are present but commented out.

In config/initializes/paperclip.rb

#Paperclip::Attachment.default_options[:host] = 's3-ap-southeast-2.amazonaws.com'
Paperclip::Attachment.default_options[:storage] = :s3

# Paperclip::Attachment.default_options[:s3_endpoint] = 's3-ap-southeast-2.amazonaws.com'
# Paperclip::Attachment.default_options[:region] = 'ap-southeast-2'
Paperclip::Attachment.default_options[:s3_protocol] = 'https'
Paperclip::Attachment.default_options[:s3_credentials] =
  { :bucket => ENV['AWS_BUCKET'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] }

# https://s3-ap-southeast-2.amazonaws.com/drill-investor-bucket/attachments
#       /images/000/000/042/original/image-3.png
#Paperclip::Attachment.default_options[:host] = 's3-ap-southeast-2.amazonaws.com' #module AWS
#    module S3
#        DEFAULT_HOST = "s3.amazonaws.com"
#    end
#end
#AWS::S3::DEFAULT_HOST.replace "s3-ap-southeast-2.amazonaws.com"

Images are attached to to the Attachment model. The relevant part of that model is below.

....
has_attached_file :image,
    :s3_credentials => {
          access_key_id: 'my boss would probably',
              secret_access_key: 'object if I left the actual id and key info here',
              #  region:            'ap-southeast-2',
              bucket:            'drill-investor-bucket'
              # s3_endpoint:       's3-eu-west-1.amazonaws.com'
          }
....

From Gemfile

ruby '2.0.0'
gem 'rails', '4.0.0'
...
gem 'paperclip' , '~> 4.1.0'
gem 'aws-sdk'

Any help or suggestions gladly appreciated.

thanks Pierre

Upvotes: 0

Views: 347

Answers (1)

user1854802
user1854802

Reputation: 388

I came across another (thought I had checked all) query on stackoverflow which answered the question. Using their answer I add to config/initializers/paperclip.rb

Paperclip::Attachment.default_options[:s3_host_name] = 's3-ap-southeast-2.amazonaws.com' 

Thanks to those posting on stackoverflow Rails 4, Paperclip, Amazon S3 Config Amazon Path

Pierre

Upvotes: 1

Related Questions