Paperclip, S3 and Rails admin

If ever you'll find yourself having problems with displaying a paperclip attachment stored in a Non-US AWS S3 bucket, please try this solution.

#Gemfile
gem 'paperclip' #3.5.1
gem 'rmagick' #2.13.2
gem 'aws-sdk' #1.18.0
gem 'rails_admin' #0.0.1

So, the trouble was, the image doesn't display on a show and edit page because it kept calling the default endpoint of s3.amazonaws.com.

Solution:

  • Install dot_env gem to isolate all constants and setup related data

  • gem install dot_env

  • Create your .env file as such, using the suggested AWS constants:

    AWS_ACCESS_KEY_ID: accesskeyid

    AWS_SECRET_ACCESS_KEY: mysecretaccesskey

    AWS_DEFAULT_REGION: ap-southeast-2

    BUCKET: mybucketname

  • Your default region is the region where you assigned your bucket to. You can find the list of regions here: Regions and Endpoints. Also take note of the endpoint here.

  • Append a config for paperclip inside your environment file (config/environments/development.rb, etc.) where your s3_host_name is your endpoint found in the Regions and Endpoints AWS document.

    config.paperclip_defaults = { :storage => :s3,
    :s3_credentials => {
    :bucket => ENV['BUCKET'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']},
    :s3_host_name => 's3-ap-southeast-2.amazonaws.com'}

  • Modify your rails_admin.rb. If you have a model that has an image field which is a Paperclip object, you can do this for the display (aside from simply using thumb_method :style)

    field(:image) do
    pretty_value do
    bindings[:view].tag(:img, { :src => bindings[:object].image.url(:style) })
    end
    end

I spent quite some time experimenting with the correct settings.  I hope this saves someone else's time! :)