Custom URL with Paperclip and AWS S3

◇◆丶佛笑我妖孽 提交于 2019-12-13 13:07:17

问题


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.


回答1:


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.




回答2:


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'


来源:https://stackoverflow.com/questions/29756313/custom-url-with-paperclip-and-aws-s3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!