简体   繁体   English

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

[英]Can CarrierWave upload to Amazon S3 but serve through CloudFront?

I'm working on a small rails site which allows some users to upload images and others to see them. 我正在开发一个小型的rails网站,允许一些用户上传图像,其他用户可以看到它们。 I started using CarrierWave with S3 as the storage medium and everything worked great but then I wanted to experiment with using CouldFront. 我开始使用CarrierWave和S3作为存储介质,一切都运行良好,但后来我想尝试使用CanFront。 I first added a distribution to my S3 bucket and then changed the CarrierWave configuration I was using to this: 我首先在我的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

I should mention that http://static.my-domain.com is a CNAME entry pointing to a CloudFront endpoint ( some-id.cloudfront.net ). 我应该提到http://static.my-domain.com是指向CloudFront端点( some-id.cloudfront.net )的CNAME条目。 The result is that the pictures are shown correctly, URLs look like this: http://static.my-domain.com/some-folder/uploads/gallery_image/attachment/161/large_image.jpg but whenever I try to upload a photo or for that matter get the size of the uploaded attachment I get the following exception: 结果是图片显示正确,网址如下所示: http://static.my-domain.com/some-folder/uploads/gallery_image/attachment/161/large_image.jpghttp://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"

Just to add some more info, I tried the following: 只是为了添加更多信息,我尝试了以下内容:

  • removing the region entry 删除区域条目
  • using the CloudFront URL directly instead of the CNAME 直接使用CloudFront URL而不是CNAME
  • specifying the Amazon endpoint ( https://s3-eu-west1.amazonaws.com ) 指定Amazon端点( https://s3-eu-west1.amazonaws.com

but all of them had no effect. 但所有这些都没有效果。

Is there something I'm missing or is it that CarrierWave does not support this at this time? 是否有我遗漏的东西,或者CarrierWave目前不支持这个?

The answer to the question is YES. 这个问题的答案是肯定的。 The reason why it didn't work with my configuration is that I was missing the fog_directory entry. 它不适用于我的配置的原因是我错过了fog_directory条目。 When I added my asset_host , I removed fog_directory since the CDN urls being generated where malformed. 当我添加我的asset_host ,我删除了fog_directory因为生成的CDN网址格式错误。 I later found out that this was due to having fog_public set to false. 我后来发现这是由于将fog_public设置为false。 After getting the proper CDN urls, I forgot to add fog_directory back since I could see my images and thought everything was fine. 获得正确的CDN网址后,我忘了添加fog_directory因为我可以看到我的图像,并认为一切都很好。 Anyway the correct configuration is: 无论如何,正确的配置是:

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

Try setting :asset_host in your Uploader like so: 尝试在你的上传器中设置: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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM