使用 Paperclip 和 AWS S3 自定义 URL

2024-01-27

我们使用 Paperclip 和 aws-sdk gem 在 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

如果我然后使用<%= image_tag current_user.image.url %>在 html.erb 文件中,我得到以下 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 中设置。

我抬头看了看回形针 S3 存储文档 http://www.rubydoc.info/gems/paperclip/Paperclip/Storage/S3。有一个:url选项,但我为该选项编写的任何内容似乎都不起作用。


我刚刚遇到这个问题,这是我必须使用的设置

:s3_host_alias => "s3.example.com",
:url => ":s3_alias_url",
:path => ":class/:attachment/:id.:style.:extension"

From 这个链接 https://www.stormconsultancy.co.uk/blog/development/ror-using-paperclip-with-amazon-s3-and-cloudfront/,我了解到,除了:s3_host_alias and :url,你必须指定path所以你不会得到

Paperclip::InfiniteInterpolationError

效果还不错,因为默认的回形针路径无论如何都有点不稳定。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Paperclip 和 AWS S3 自定义 URL 的相关文章

随机推荐