Carrierwave + fog + s3 does not work with Cloud Front URLs

I have a problem with carrierwave+fog+s3 using Amazon cloud front . In the following setup, I can upload files to s3, but after downloading the URLs of the S3 objects that I get from my rails application do not have the assets_host , that is, I set the URLs to look like this https://mycloudfrontname.cloudfront.net/uploads/myfile.mp3 format https://mycloudfrontname.cloudfront.net/uploads/myfile.mp3

But they are all displayed in this format https://mybucketname.s3.amazonaws.com/uploads/myfile.mp3

What could be wrong here?

 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 

UPDATE:

I found this bit of code from Carrierwave /lib/carrierwave/storage/fog.rb - So, if we set asset_host , as in the code snippet above, should this work correctly? or is there any other configuration i have to do?

 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 =~ /^(?:[az]|\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 
+4
source share
3 answers

In the environment file you need to set the host resource. Just add the line below to your config/environments/production.rb file and you should be fine. You may also want to make sure that you are using the latest version of the carrier and fog.

 -- config/environments/production.rb Myapp::Application.configure do # Use Content Delivery Network for assets config.action_controller.asset_host = 'https://mycloudfrontname.cloudfront.net' end 
0
source

Do not use asset_host. The asset_host parameter is for files served by rails agent helpers. CarrierWave files are handled differently. The configuration you are looking for is config.fog_host

 config.fog_host = 'https://mycloudfrontname.cloudfront.net' 
0
source

Change config.fog_public to true and add config.asset_host = 'YOUR_CND_ADDRESS' . asset_host does not work if fog_public false

0
source

Source: https://habr.com/ru/post/1447921/


All Articles