简体   繁体   English

使用Paperclip和AWS S3的自定义URL

[英]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: 我们使用带有aws-sdk gem的Paperclip在我们的Rails应用程序中存储和显示图像:

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"> . 如果我在html.erb文件中使用<%= image_tag current_user.image.url %> ,我会得到以下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 ? 如何将https://s3.amazonaws.com/<my_bucket>作为自定义URL(如https://example.com I have my domain all setup in Cloudfront along with its SSL certificate. 我在Cloudfront中设置了所有域以及SSL证书。

I looked up in the Paperclip S3 Storage documentation . 我查看了Paperclip S3 Storage文档 There's a :url option, but nothing I write for that option seems to work. 有一个:url选项,但我为该选项编写的内容似乎没有用。

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 这个链接 ,我了解到,除了:s3_host_alias:url ,你必须指定path这样你就不会得到

Paperclip::InfiniteInterpolationError

Kinda works out well because the default paperclip path is kinda wonky anyways. Kinda运作良好,因为默认的回形针路径无论如何都有点不可思议。

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 : 以下Gemfile

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

Rails console: Rails控制台:

=> 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM