我在carrierwave+fog+s3
使用 Amazon时遇到了这个问题cloud front
。通过以下设置,我可以将文件上传到 s3,但上传后,我从我的 rails 应用程序获得的 S3 对象 URL 没有assets_host
基于 URL,即我正在使 URL 看起来像这种格式https://mycloudfrontname.cloudfront.net/uploads/myfile.mp3
但是它们都以这种格式出现https://mybucketname.s3.amazonaws.com/uploads/myfile.mp3
这里可能有什么问题?
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'XXXX',
:aws_secret_access_key => 'XXXX',
:region => 'us-east-1'
}
config.fog_directory = 'mybucketname'
config.asset_host = 'https://mycloudfrontname.cloudfront.net'
config.fog_public = false
config.fog_attributes = {'Cache-Control' => 'max-age=315576000'}
end
更新:
我从 Carrierwave's 中找到了这个代码位/lib/carrierwave/storage/fog.rb
- 所以如果我们asset_host
在上面的代码片段中设置 as ,这一定可以正常工作吗?还是我还必须做任何其他配置?
def public_url
if host = @uploader.asset_host
if host.respond_to? :call
"#{host.call(self)}/#{path}"
else
"#{host}/#{path}"
end
else
# AWS/Google optimized for speed over correctness
case @uploader.fog_credentials[:provider]
when 'AWS'
# if directory is a valid subdomain, use that style for access
if @uploader.fog_directory.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\d{1,3}){3}$))(?:[a-z0-9\.]|(?![\-])|\-(?![\.])){1,61}[a-z0-9]$/
"https://#{@uploader.fog_directory}.s3.amazonaws.com/#{path}"
else
# directory is not a valid subdomain, so use path style for access
"https://s3.amazonaws.com/#{@uploader.fog_directory}/#{path}"
end
when 'Google'
"https://commondatastorage.googleapis.com/#{@uploader.fog_directory}/#{path}"
else
# avoid a get by just using local reference
directory.files.new(:key => path).public_url
end
end
end