使用Paperclip和AWS S3的自定义URL [英] Custom URL with Paperclip and AWS S3

查看:157
本文介绍了使用Paperclip和AWS S3的自定义URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用带有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆