Custom URL with Paperclip and AWS S3
Asked Answered
B

2

6

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.

Beane answered 20/4, 2015 at 19:7 Comment(0)
C
4

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.

Countryman answered 1/10, 2015 at 16:39 Comment(1)
What does s3_alias_url do anyway?Philippe
D
3

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'
Deprave answered 20/4, 2015 at 19:14 Comment(8)
You'l probably need s3_protocol set to https tooDeprave
When I added url: ':s3_alias_url', I received an error "no implicit conversion of Symbol into String" on the page displaying the photo.Beane
Actually that "no implicit conversion" error was when I had :s3_alias_url by itself without quotes. With the quotes around :s3_alias_url, I get this error: "Paperclip::Errors::InfiniteInterpolationError"Beane
@Beane I updated the answer with a working example I tested on a new S3 bucketDeprave
what changed? I don't see what I would add to get it to work.. hash_secret is something I don't have... maybe I'll try that.Beane
Secret hash is just a rake secret value. Aside from the styles, everything else was needed in order to get it working and saving properly to S3.Deprave
I can't get this to work... doing the exact same thing as you... doesn't create a proper URLBeane
What does s3_alias_url do anyway?Philippe

© 2022 - 2024 — McMap. All rights reserved.