使用Paperclip和AWS S3的自定义URL [英] Custom URL with Paperclip and AWS S3
问题描述
我们正在使用带有aws-sdk gem的Paperclip在Rails应用程序中存储和显示图像:
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
如果我随后在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">
.
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">
.
如何将https://s3.amazonaws.com/<my_bucket>
用作自定义URL,例如https://example.com
?我将自己的域及其SSL证书全部设置在Cloudfront中.
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.
我在 Paperclip S3存储文档中进行了查找.有一个:url
选项,但是我为该选项编写的任何内容似乎都无效.
I looked up in the Paperclip S3 Storage documentation. There's a :url
option, but nothing I write for that option seems to work.
推荐答案
我刚刚遇到了这个问题,这是我必须使用的设置
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"
来自此链接,我了解到,除了:s3_host_alias
和:url
,还必须指定path
,这样您就不会得到
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效果很好,因为默认的回形针路径还是有点怪.
Kinda works out well because the default paperclip path is kinda wonky anyways.
这篇关于使用Paperclip和AWS S3的自定义URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!