简体   繁体   中英

uninitialized constant CarrierWave::Storage::Fog with Google Cloud Storage

I have seen lots of other people having similar problems to me but none of the listed solutions apply so I am hoping this awesome community can help me out.

I am trying to use the sitemap_generator gem but I host with Heroku so I am trying to follow their documentation here to use Carrierwave to upload the sitemaps to Google Cloud Storage. I am already using Google Cloud to upload my images with all works fine so I was hoping it would be straightforward however the files are not being uploaded. The documentation says you need to add:

config.storage = :fog

To your carrierwave config file however whenever I add it, I get the following error:

gems/carrierwave-49fdad1ec6ca/lib/carrierwave/uploader/configuration.rb:75:in `eval': uninitialized constant CarrierWave::Storage::Fog (NameError)

My Carrierwave config looks like this:

CarrierWave.configure do |config|
     config.cache_dir = "#{Rails.root}/tmp/"
     config.storage = :fog
     config.fog_credentials = {
         :provider => 'Google'
     }
     config.fog_directory = 'bucket-name'
     config.asset_host = 'https://domain.storage.googleapis.com'
end

(fog_directory and asset_host are replaced with dummy values)

And in my gem file I have:

gem 'fog'
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'

I have seen a lot of people using AWS with the same error but their solution is to change to use fog gem instead of fog-aws (which I am already doing) and require fog/aws. I have tested this like so:

gem 'fog', require: 'fog/google'

But still have the same issue.

Can anyone suggest what I can do to try and resolve this? Any help would be greatly appreciated!!

Many thanks

I got the same error using fog-aws for Amazon S3. It seems this error occurs if storage is fog, doesn't matter it's amazon s3 or google cloud.

I used carrierwave gem to upload user profile picture so I solved this by moving storage configuration after credentials configuration as below:

CarrierWave.configure do |config|
  if Rails.env.staging? || Rails.env.production?
    config.fog_provider = 'fog/aws'
    config.fog_credentials = {
      provider: 'AWS',
      aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
      aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
      region: ENV['AWS_REGION']
    }
    config.storage = :fog
    config.fog_directory = ENV['S3_BUCKET']
    config.fog_public    = true
    config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" }
  else
    config.storage = :file
    config.enable_processing = Rails.env.development?
  end
end

Another solution was to add require 'carrierwave/storage/fog' at the top, in above file. For me the file was carrier_wave.rb under initializers directory.

Adding links where I found above solutions.

Medium Carrierwave Fog

I know this post is 7 months old but i just wasted 36h trying to implement that same gem... no luck. It seems that documentation is slightly outdated.

If you ask me, fog-google is unnecessary at this point!

Try this gem for CarrierWaveUploader integration with google.

carrierwave-google-storage github

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.

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