1

所以这似乎应该很容易...每个人都说只是使用config.asset_host。当我设置,虽然,我的应用程序内的所有链接仍然指向S3。Cloudfront + Carrierwave

CarrierWave.configure do |config| 

    config.storage = :fog 

    config.fog_credentials = { 
    :provider    => 'AWS', 
    :aws_access_key_id  => AWS_ACCESS_KEY_ID, 
    :aws_secret_access_key => AWS_SECRET_ACCESS_KEY, 
    :region    => 'us-east-1' 
    } 

    config.fog_authenticated_url_expiration = 3.hours 

    config.asset_host  = "http://xyz123.cloudfront.net" 
    config.fog_directory = S3_BUCKET_NAME 
    config.fog_public  = false 
    config.fog_attributes = { 
    'Cache-Control' => "max-age=#{1.year.to_i}" 
    } 
end 

这里是我打电话给我的文件...

image_tag book.attachments.first.filename.file.authenticated_url(:thumb175)

它看起来对我来说,public_url预先考虑适当的主机,但它需要0参数...所以我怎么通过正确的response-content-dispositionresponse-content-type和链接到期时间?

回答

0

我认为你自己找到了这个,但公共网址不会过期。如果你想要的话,你需要使用认证的网址。对于公共网址,我认为您可以简单地获取网址并附加所需的任何查询参数,至少现在是这样。如果这对你有好处,我们当然可以看到修补事情做正确的事情。

1

我有同样的问题,花了太久的时间找出答案!事实证明,当你设置fog_public = false CarrierWave将忽略config.asset_host。您可以通过设置config.fog_public = true来演示此操作:您的网址现在将成为CloudFront网址,而不是S3网址。此问题已得到前次募集:

https://github.com/carrierwaveuploader/carrierwave/issues/1158 https://github.com/carrierwaveuploader/carrierwave/issues/1215

在最近的一个项目,我很高兴通过CarrierWave处理上传到S3,但希望它使用Model.attribute_url当返回一个签名的CloudFront的URL。我想出了以下(肯定是丑陋的)解决方法,我希望其他人可以从中受益或提高:

'cloudfront-signer' gem添加到您的项目并根据说明进行配置。然后加入/lib/carrierwave/uploader/url.rb的以下重写一个新的文件中配置/初始化(注意:AWS :: CF的多次插入:: Signer.sign_url):

module CarrierWave 
     module Uploader 
     module Url 
      extend ActiveSupport::Concern 
      include CarrierWave::Uploader::Configuration 
      include CarrierWave::Utilities::Uri 

      ## 
      # === Parameters 
      # 
      # [Hash] optional, the query params (only AWS) 
      # 
      # === Returns 
      # 
      # [String] the location where this file is accessible via a url 
      # 
      def url(options = {}) 
      if file.respond_to?(:url) and not file.url.blank? 
       file.method(:url).arity == 0 ? AWS::CF::Signer.sign_url(file.url) : AWS::CF::Signer.sign_url(file.url(options)) 
      elsif file.respond_to?(:path) 
       path = encode_path(file.path.gsub(File.expand_path(root), '')) 

       if host = asset_host 
       if host.respond_to? :call 
        AWS::CF::Signer.sign_url("#{host.call(file)}#{path}") 
       else 
        AWS::CF::Signer.sign_url("#{host}#{path}") 
       end 
       else 
       AWS::CF::Signer.sign_url((base_path || "") + path) 
       end 
      end 
      end 

     end # Url 
    end # Uploader 
end # CarrierWave 

然后通过加入覆盖/lib/carrierwave/storage/fog.rb下面以相同的文件的底部:

require "fog" 

module CarrierWave 
    module Storage 
    class Fog < Abstract 
     class File 
      include CarrierWave::Utilities::Uri 
      def url 
      # Delete 'if statement' related to fog_public 
      public_url 
      end 
     end 
    end 
    end 
end 

最后,在配置/初始值设定S/carrierwave.rb

config.asset_host = “http://d12345678.cloudfront.net

config.fog_public =假

就是这样。您现在可以使用Model.attribute_url,它会将已签名的CloudFront URL返回到由CarrierWave上传到您的S3存储桶的私有文件。