I have a rails4 app and I'm trying to add CDN to S3 uploads. For some reason it doesn't work.
When I set the cloudfront in the config for some reason it has effect only on some of the images (NOT ALL) that belong to the same uploader and on the top of that those images are broken. Images displayed from S3 work properly except they also should be served from cloudfront.
What did I miss?
Image that is displayed (broken) via cloudfront:
<%= image_tag current_user.avatar.url(:small_thumb), class: 'navbar-avatar', alt: "#{current_user.first_name}'s_avatar", title: "#{current_user.first_name}'s avatar" %>
https://xyz.cloudfront.net/images/xyz.cloudfront.net/uploads/profile/avatar/3/small_thumb_3f4ff02.jpg
Image from the same uploader that for some reason is displayed from S3 not cloudfront (display is NOT broken)
<%= image_tag post.user.avatar.url(:base_thumb), class: 'post-avatar' %>
https://eaxmple.s3.amazonaws.com/uploads/profile/avatar/10/base_thumb_john_smith.png
config:
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
}
config.fog_directory = ENV['S3_BUCKET_NAME']
config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" }
config.asset_host = ENV['CLOUDFRONT_URL']
end
avatar_uploader.rb
if Rails.env.production?
storage :fog
else
storage :file
end
gemfile
gem 'carrierwave', '~> 0.10.0'
gem 'fog', '~> 1.34.0'
gem 'mini_magick', '~> 4.3.3'
UPDATE
uploader default storage:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
How can I change this:
https://xyz.cloudfront.net/images/xyz.cloudfront.net/uploads/profile/avatar/3/small_thumb_3f4ff02.jpg
to this:
https://xyz.cloudfront.net/uploads/profile/avatar/3/small_thumb_3f4ff02.jpg
看起来您可能需要将协议添加到您的 cloudfront url:
CLOUDFRONT_URL=https://xyz.cloudfront.net
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.