【发布时间】:2015-11-11 16:55:12
【问题描述】:
我有一个已经在 S3 上工作的应用程序(在 staging 和 prod 中)。
现在我们希望它与 Cloudfront 一起工作。
我发现由于某种原因我在两个地方有回形针定义:
/confog/initializers/paperclip.rb:
if Rails.env.production? || Rails.env.staging? || true
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
end
/config/environments/staging.rb 和 /config/environments/production.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => s3_options[:bucket],
:access_key_id => s3_options[:access_key_id],
:secret_access_key => s3_options[:secret_access_key]
}
}
(我从我拥有的 s3.yml 文件中加载s3_options)
第一个问题 - 是否有必要(或者另一方面 - 是错误的)让这两个地方有配置?
有了这个配置,我得到了这个:
> Profile.last.image.url
=> "https://mybucket.s3.amazonaws.com/profiles/images/000/000/001/original/someimage.jpg?1439912576"
我的目标: 获取 cloundfront url 而不是 s3。
我尝试了几件事:
-
添加到 paperclip.rb 这一行:
Paperclip::Attachment.default_options[:s3_host_alias] = "xxxxx.cloudfront.net"(其中
xxxxx代表云端哈希)。
结果:没有任何改变。 -
将这一行添加到 paperclip.rb:
Paperclip::Attachment.default_options[:s3_host_name] = "xxxxx.cloudfront.net"(其中
xxxxx代表云端哈希)。
结果:回形针将存储桶名称之前连接起来:> Profile.last.image.url => "https://mybucket.xxxxx.cloudfront.net/profiles/images/000/000/001/original/someimage.jpg?1439912576" -
在 paperclip.rb 中禁用配置并将这些行添加到环境配置文件中(我在 development.rb 上尝试过):
config.paperclip_defaults = { : :s3_credentials => { : : :url => "xxxxx.cloudfront.net", :s3_host_name => "xxxxx.cloudfront.net", :path => '/:class/:attachment/:id_partition/:style/:filename', } }结果:回形针在之后连接桶名:
> Profile.last.image.url => "https://xxxxx.cloudfront.net/mybucket/profiles/images/000/000/001/original/someimage.jpg?1439912576" -
同 (3),但将这些行添加到更高一级:
config.paperclip_defaults = { :storage => :s3, :url => "xxxxx.cloudfront.net", :s3_host_name => "xxxxx.cloudfront.net", :path => '/:class/:attachment/:id_partition/:style/:filename', :s3_credentials => { : : } }结果:同(3)。
简而言之,无论我在:s3_host_name 中输入什么,回形针在某个地方连接存储桶名称。
有什么想法吗?
【问题讨论】:
标签: ruby-on-rails-4 amazon-s3 paperclip amazon-cloudfront