【问题标题】:Can CarrierWave upload to Amazon S3 but serve through CloudFront?CarrierWave 能否上传到 Amazon S3 但通过 CloudFront 提供服务?
【发布时间】:2014-05-11 11:43:35
【问题描述】:

我正在开发一个小型 Rails 站点,该站点允许一些用户上传图像,而其他用户则可以查看它们。我开始使用带有 S3 的 CarrierWave 作为存储介质,一切都很好,但后来我想尝试使用 CandFront。我首先在我的 S3 存储桶中添加了一个distribution,然后将我使用的 CarrierWave 配置更改为:

CarrierWave.configure do |config|
  config.storage = :fog
  config.fog_credentials = {
    :provider               => 'AWS',                                         # required
    :aws_access_key_id      => ENV['S3_ACCESS_KEY_ID'],                       # required
    :aws_secret_access_key  => ENV['S3_SECRET_ACCESS_KEY'],                   # required
    :region                 => 'eu-west-1',
  }
  config.asset_host = 'http://static.my-domain.com/some-folder'
  config.fog_public     = true                                   # optional, defaults to    true
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
end

我应该提到http://static.my-domain.com 是一个指向 CloudFront 端点 (some-id.cloudfront.net) 的 CNAME 条目。结果是图片显示正确,URL 如下所示:http://static.my-domain.com/some-folder/uploads/gallery_image/attachment/161/large_image.jpg 但每当我尝试上传照片或获取上传附件的大小时,都会出现以下异常:

Excon::Errors::MovedPermanently: Expected(200) <=> Actual(301 Moved Permanently)
  response => #<Excon::Response:0x007f61fc3d1548 @data={:body=>"", 
  :headers=>{"x-amz-request-id"=>"some-id", "x-amz-id-2"=>"some-id", 
  "Content-Type"=>"application/xml", "Transfer-Encoding"=>"chunked", 
  "Date"=>"Mon, 31 Mar 2014 21:16:45 GMT", "Connection"=>"close", "Server"=>"AmazonS3"}, 
  :status=>301, :remote_ip=>"some-ip"}, @body="", @headers={"x-amz-request-id"=>"some-id", 
  "x-amz-id-2"=>"some-id", "Content-Type"=>"application/xml", 
  "Transfer-Encoding"=>"chunked", "Date"=>"Mon, 31 Mar 2014 21:16:45 GMT", 
  "Connection"=>"close", "Server"=>"AmazonS3"}, @status=301, @remote_ip="some-ip"

只是为了添加更多信息,我尝试了以下方法:

  • 删除区域条目
  • 直接使用 CloudFront URL 而不是 CNAME
  • 指定 Amazon 端点 (https://s3-eu-west1.amazonaws.com)

但它们都没有效果。

是我遗漏了什么还是 CarrierWave 目前不支持此功能?

【问题讨论】:

    标签: ruby-on-rails carrierwave amazon-cloudfront


    【解决方案1】:

    尝试在您的上传器中设置:asset_host,如下所示:

    class ScreenshotUploader < CarrierWave::Uploader::Base
      storage :fog 
    
      # Configure uploads to be stored in a public Cloud Files container
      def fog_directory
        'my_public_container'
      end
    
      # Configure uploads to be delivered over Rackspace CDN
      def asset_host
       "c000000.cdn.rackspacecloud.com"
      end
    end
    

    灵感来自https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Store-private-public-uploads-in-different-Cloud-Files-Containers-with-Fog

    【讨论】:

      【解决方案2】:

      问题的答案是肯定的。它不适用于我的配置的原因是我缺少 fog_directory 条目。当我添加我的asset_host 时,我删除了fog_directory,因为生成的 CDN url 格式错误。后来我发现这是由于将fog_public 设置为false。获得正确的 CDN url 后,我忘记添加 fog_directory,因为我可以看到我的图像并认为一切都很好。无论如何正确的配置是:

      CarrierWave.configure do |config|
        config.storage = :fog
        config.fog_credentials = {
          :provider               => 'AWS',                                         # required
          :aws_access_key_id      => ENV['S3_ACCESS_KEY_ID'],                       # required
          :aws_secret_access_key  => ENV['S3_SECRET_ACCESS_KEY'],                   # required
          :region                 => 'eu-west-1'
        }
      
        config.fog_directory  = '-bucket-name-/-some-folder-'
        config.asset_host = 'https://static.my-domain.com/-some-folder-'
        config.fog_public     = true                                   # optional, defaults to true
        config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
      end
      

      【讨论】:

      • 我使用相同的配置,只是没有 cdn 的 cname。上传照片时,在表单操作中会显示 cdn url,因此上传失败。你能帮我吗?
      猜你喜欢
      • 2015-09-08
      • 2017-11-21
      • 1970-01-01
      • 2011-07-23
      • 2016-11-01
      • 2012-05-17
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多