是否可以配置Paperclip为S3生成HTTPS URL?

问题描述 投票:56回答:2

我正在使用Paperclip在完全由HTTPS提供服务的网站上管理用户上传的图像。为了避免在IE7 / IE8上出现愚蠢的安全警告,我还需要通过SSL提供这些图像。我通常使用以下内容渲染图像:

<%= image_tag @product.image.url(:large) %>

where

class Product < ActiveRecord::Base

  has_attached_file :image,
                  :styles => {
                      :large => {:geometry => "616x450#"}
                  },
                  :storage => :s3,
                  :s3_credentials => {:access_key_id => "xxx", :secret_access_key => "xxx"},
                  :path => ":attachment/:id/:style/:basename.:extension",
                  :bucket => CONFIG['s3_media_bucket'],
                  :default_url => "/assets/image_missing.png"

并且生成的图像URL类似于:

http://s3.amazonaws.com/media.example.com/images/6/large/image123.JPG

是否有一个神奇的回形针选项将其更改为:

https://s3.amazonaws.com/media.example.com/images/6/large/image123.JPG
ruby-on-rails ruby-on-rails-3 amazon-s3 paperclip
2个回答
99
投票

您只需要添加:

:s3_protocol => :https

这已覆盖in the documentation

has_attached_file有一些特定于S3的选项:...

  • s3_protocol:为您的S3资产生成的URL的协议。可以是“ http”或“ https”。当您的:s3_permissions为:public_read(默认值)时,默认值为“ http”;而当您的:s3_permissions为其他值时,默认值为“ https”。

24
投票

要更新您的代码,只需添加:s3_protocol如下:

class Product < ActiveRecord::Base
has_attached_file :image,
              :styles => {
                  :large => {:geometry => "616x450#"}
              },
              :storage => :s3,
              :s3_credentials => {:access_key_id => "xxx", :secret_access_key => "xxx"},
              :s3_protocol => :https,
              :path => ":attachment/:id/:style/:basename.:extension",
              :bucket => CONFIG['s3_media_bucket'],
              :default_url => "/assets/image_missing.png"
© www.soinside.com 2019 - 2024. All rights reserved.