Cri*_*ila 9 ruby-on-rails carrierwave amazon-cloudfront
我正在开发一个小型的rails网站,允许一些用户上传图像,其他用户可以看到它们.我开始使用CarrierWave和S3作为存储介质,一切都运行良好,但后来我想尝试使用CanFront.我首先distribution在我的S3存储桶中添加了一个,然后将我正在使用的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
Run Code Online (Sandbox Code Playgroud)
我应该提一下,它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"
Run Code Online (Sandbox Code Playgroud)
只是为了添加更多信息,我尝试了以下内容:
https://s3-eu-west1.amazonaws.com)但所有这些都没有效果.
是否有我遗漏的东西,或者CarrierWave目前不支持这个?
这个问题的答案是肯定的.它不适用于我的配置的原因是我错过了这个fog_directory条目.当我添加我的时候asset_host,我删除了fog_directory因为生成的CDN网址格式错误.我后来发现这是因为fog_public设置为假.获得正确的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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2637 次 |
| 最近记录: |