Rails 4 - 使用带有Paperclip的cloudfront

guy*_*oni 12 amazon-s3 paperclip amazon-cloudfront ruby-on-rails-4

我有一个应用程序,它已经(在staging和prod中)与S3一起工作.
现在我们希望它与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
Run Code Online (Sandbox Code Playgroud)

/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]
  }
}
Run Code Online (Sandbox Code Playgroud)

(我s3_options从我有的s3.yml文件加载)

第一个问题 - 是否有必要(或者另一方面 - 这是错误的)将这两个地方配置?

有了这个配置,我得到了这个:

> Profile.last.image.url
=> "https://mybucket.s3.amazonaws.com/profiles/images/000/000/001/original/someimage.jpg?1439912576"
Run Code Online (Sandbox Code Playgroud)

我的目标: 获取cloundfront url而不是s3.

我尝试了几件事:

  1. 添加到paperclip.rb这一行:

    Paperclip::Attachment.default_options[:s3_host_alias]  = "xxxxx.cloudfront.net"
    
    Run Code Online (Sandbox Code Playgroud)

    (xxxxx代表cloudfront哈希).
    结果:没有任何改变.

  2. 添加到paperclip.rb这一行:

    Paperclip::Attachment.default_options[:s3_host_name]  = "xxxxx.cloudfront.net"
    
    Run Code Online (Sandbox Code Playgroud)

    (xxxxx代表cloudfront哈希).
    结果:回形针之前连接桶名称:

    > Profile.last.image.url
    => "https://mybucket.xxxxx.cloudfront.net/profiles/images/000/000/001/original/someimage.jpg?1439912576"
    
    Run Code Online (Sandbox Code Playgroud)
  3. 禁用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',
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    结果:回形针在其后连接桶名称:

    > Profile.last.image.url
    => "https://xxxxx.cloudfront.net/mybucket/profiles/images/000/000/001/original/someimage.jpg?1439912576"
    
    Run Code Online (Sandbox Code Playgroud)
  4. 如(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 => {
         :
         :
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    结果:与(3)相同.

简而言之,无论我放入什么:s3_host_name,paperclip都会在某些地方连接桶名.

有些想法?

guy*_*oni 25

这比我想象的容易.
看起来像回形针使用:url字符串或作为符号的参考,指示如何构造网址.

在我的/config/environments/staging.rb/config/environments/production.rb文件中,我现在有:

config.paperclip_defaults = {
  :storage => :s3,
  :url => ':s3_alias_url',
  :s3_host_alias => "xxxxx.cloudfront.net",
  :path => '/:class/:attachment/:id_partition/:style/:filename',
  :s3_credentials => {
       :
       :
  }
}
Run Code Online (Sandbox Code Playgroud)