Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom URL with Paperclip and AWS S3

We're using Paperclip with the aws-sdk gem to store and display images in our Rails app:

class User < ActiveRecord::Base
  has_attached_file :image,
                    storage: :s3,
                    s3_credentials: 'config/s3.yml',
                    s3_protocol: :https,
                    styles: {
                        curriculum: '120x120>',
                        medium: '600x600>',
                        thumb: '200x200>'
                    },
                    default_url: 'missing_photo.png'
end

If I then use <%= image_tag current_user.image.url %> in an html.erb file, I get the following HTML: <img src="https://s3.amazonaws.com/<my_bucket>/users/images/000/000/001/medium/my_image.png?1419989041">.

How do I get that https://s3.amazonaws.com/<my_bucket> to be a custom URL like https://example.com? I have my domain all setup in Cloudfront along with its SSL certificate.

I looked up in the Paperclip S3 Storage documentation. There's a :url option, but nothing I write for that option seems to work.

like image 473
at. Avatar asked Apr 20 '15 19:04

at.


2 Answers

I just ran across this problem and here are the settings I had to use

:s3_host_alias => "s3.example.com",
:url => ":s3_alias_url",
:path => ":class/:attachment/:id.:style.:extension"

From this link, I learned that, in addition to :s3_host_alias and :url, you have to specify path so you don't get

Paperclip::InfiniteInterpolationError

Kinda works out well because the default paperclip path is kinda wonky anyways.

like image 133
vinhboy Avatar answered Nov 01 '22 19:11

vinhboy


Update

I put together an example and was able to get it working with the following:

class User < ActiveRecord::Base

  has_attached_file :profile_picture,
                styles: { :medium => "300x300>", :thumb => "100x100>" },
                path: 'users/:attachment/:style-:hash.:extension',
                hash_secret: "94dfda08e2ed473257345563594dfda08e2ed473257345563594dfda08e2ed473257345563594dfda08e2ed4732573455635",
                default_url: "/images/:style/missing.png",
                storage: :s3,
                s3_protocol: 'http',
                url: ':s3_alias_url',
                s3_host_alias: 'distro1234.cloudfront.net',
                s3_credentials: {
                  access_key_id: 'access_id',
                  secret_access_key: 's3cr3tK3y!',
                  acl: 'private',
                  bucket: 'my-bucket',
                  bucket_url: 'https://my-bucket.s3.amazonaws.com',
                }

  validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/.*\Z/

end

And the following Gemfile:

gem 'paperclip'
gem 'aws-sdk', '~> 1.5.7'

Rails console:

=> u.profile_picture.url
=> "http://distro1234.cloudfront.net/users/profile_pictures/original-95eb509f9c81a341945a5a65e59e81880a739d39.jpg?1429638820"

Try something like this:

has_attached_file :image,
                storage: :s3,
                s3_credentials: 'config/s3.yml',
                s3_protocol: :https,
                styles: {
                    curriculum: '120x120>',
                    medium: '600x600>',
                    thumb: '200x200>'
                },
                url: ':s3_alias_url',
                s3_host_alias: 'example.com',  
                default_url: 'missing_photo.png'
like image 3
Dan Fairaizl Avatar answered Nov 01 '22 19:11

Dan Fairaizl