CarrierWave可以上传到Amazon S3但是通过CloudFront服务吗?

时间:2014-03-31 22:23:10

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

我正在开发一个小型网站,允许一些用户上传图像,其他用户可以看到它们。我开始使用CarrierWave和S3作为存储介质,一切都运行良好,但后来我想尝试使用CanFront。我首先在我的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条目。结果是图片显示正确,网址如下所示: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网址而不是CNAME
  • 指定Amazon端点(https://s3-eu-west1.amazonaws.com

但所有这些都没有效果。

我有什么遗漏或是CarrierWave目前不支持吗?

2 个答案:

答案 0 :(得分:7)

问题的答案是肯定的。它无法使用我的配置的原因是我错过了fog_directory条目。当我添加asset_host时,我删除了fog_directory,因为生成的CDN网址格式错误。后来我发现这是因为fog_public设置为false。获得正确的CDN网址后,我忘了添加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

答案 1 :(得分:3)

尝试在您的上传器中设置: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