hyp*_*jas 20 ruby-on-rails cdn amazon-s3 carrierwave
我在我的网站上使用带载波的雾.但图像加载速度非常慢.
然后我想加快用CDN加载图像.
我已经按照本教程创建了图像的CDN:
http://maketecheasier.com/configure-amazon-s3-as-a-content-delivery-network/2011/06/25
我现在已经为图像部署了我的发行版,但我不知道cdn是如何工作的.我有初始化器/ fog.rb下一个配置:
CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => 'key',
    :aws_secret_access_key  => 'key',
    :region                 => 'eu-west-1'
  }
  config.fog_host = "http://da33ii2cvf53u.cloudfront.net" #config.asset_host instead of config.fog_host for new fog gem versions
  config.fog_directory  = 'pin-pro'
  config.fog_public     = false
  #config.fog_attributes = {'Cache-Control' => 'max-age=315576000'} 
end 
我不知道这是否正确,但在我的本地机器上它对我来说效果不好.我看到图像位置,与以前的路线相同:
https://s3-eu-west-1.amazonaws.com/pin-pro/uploads/pins/medium_610cafbe-5d43-4223-ab0e-daa4990863c4.jpg?AWSAccessKeyId=AKIAIDX34WHYKB3ZKFVA&Signature=RwQriNpiRXaTxyfYVvYjsvclUa8%3D&Expires=1333203059
如何使用s3和cloudfront在运载波中添加CDN到雾文件?
Rob*_*Rob 10
当你设置CarrierWave将无法正常工作config.fog_public =假 和点config.asset_host到CloudFront的分布.这已被多次记录:
https://github.com/carrierwaveuploader/carrierwave/issues/1158 https://github.com/carrierwaveuploader/carrierwave/issues/1215
在最近的一个项目中,我很高兴使用CarrierWave处理上传到S3,但希望它在使用Model.attribute_url时返回签名的CloudFront URL .我提出了以下(当然是丑陋的)解决方法,我希望其他人可以从中受益或改进:
将"cloudfront-signer" gem 添加到项目中,并按照说明进行配置.然后在config/initializers的新文件中添加/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
最后,在config/initializers/carrierwave.rb中:
config.asset_host =" http://d12345678.cloudfront.net "
config.fog_public = false
而已.您现在可以使用Model.attribute_url,它会将已签名的CloudFront URL返回到CarrierWave上传到S3存储桶的私有文件.
您好像没有将以下行添加到配置中.您需要使用Amazon的云端地址替换下面的示例地址.
来自github自述文件:https://github.com/jnicklas/carrierwave
"您可以选择在配置中包含您的CDN主机名.强烈建议这样做,因为没有它,每个请求都需要查找此信息"
config.asset_host = "http://c000000.cdn.rackspacecloud.com"
| 归档时间: | 
 | 
| 查看次数: | 10551 次 | 
| 最近记录: |