Carrierwave/fog/AWS issue when loading to Heroku “

2019-06-24 04:38发布

So im a total newbie and going through Michael Hartl's rails tutorial. Ive become totally stuck when trying to config picture uploads using carrierwave, fog-aws to S3 AWS. When pushing to heroku I get the following error:

LoadError: cannot load such file -- fog

As a side note i've tried using the exact same code in development mode, rails server works just fine and I am able to upload files to AWS successfully. So it's not a configuration setup problem with AWS. My problem comes when trying to push the app to Heroku.

Here is my setup: gemfile

source 'https://rubygems.org'

gem 'rails',        '5.1.4'
gem 'bcrypt', git: 'https://github.com/codahale/bcrypt-ruby.git', :require => 'bcrypt'
gem 'faker',          '1.7.3'
gem 'carrierwave',             '1.0.0'
gem 'fog-aws',                 '2.0.0'
gem 'mini_magick',             '4.7.0'
gem 'nokogiri',                '1.8.1'
gem 'will_paginate',           '3.1.6'
gem 'bootstrap-will_paginate', '1.0.0'
gem 'bootstrap-sass', '3.3.7'
gem 'puma',         '3.9.1'
gem 'sass-rails',   '5.0.6'
gem 'uglifier',     '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks',   '5.0.1'
gem 'jbuilder',     '2.7.0'

group :development, :test do
  gem 'sqlite3', '1.3.13'
  gem 'byebug',  '9.0.6', platform: :mri
end

group :development do
  gem 'web-console',           '3.5.1'
  gem 'listen',                '3.0.8'
  gem 'spring',                '2.0.2'
  gem 'spring-watcher-listen', '2.0.1'
end

group :test do
  gem 'rails-controller-testing', '1.0.2'
  gem 'minitest-reporters',       '1.1.14'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
end

group :production do
  gem 'pg', '0.18.4'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

app/uploaders/picture_uploader.rb

  class PictureUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  process resize_to_limit: [400, 400]
   if Rails.env.production?
     storage :fog
   else
     storage :file
   end

config/initializers/carrierwave.rb

    if Rails.env.production?
      CarrierWave.configure do |config|
            config.fog_provider = 'fog/aws'         
            config.fog_credentials = {
            # Configuration for Amazon S3
            :provider              => 'AWS',
            :aws_access_key_id     => ENV['S3_ACCESS_KEY'],
            :aws_secret_access_key => ENV['S3_SECRET_KEY'],
            :region                => ENV['S3_REGION'],
      }
        config.cache_dir     = "#{Rails.root}/tmp/uploads"   # For Heroku
        config.fog_directory  = ENV['S3_BUCKET'] 
        config.fog_public     = true
        config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" }
       end
    end

Heroku log with error:

!     Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
            Detected buildpacks: Ruby,Node.js
            See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
-----> Ruby app detected
-----> Compiling Ruby/Rails
-----> Using Ruby version: ruby-2.3.4
###### WARNING:
       Removing `Gemfile.lock` because it was generated on Windows.
       Bundler will do a full resolve so native gems are handled properly.
       This may result in unexpected gem versions being used in your app.
       In rare occasions Bundler may not be able to resolve your dependencies at all.
       https://devcenter.heroku.com/articles/bundler-windows-gemfile
-----> Installing dependencies using bundler 1.15.2
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4
       Fetching https://github.com/codahale/bcrypt-ruby.git
       The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
       Fetching gem metadata from https://rubygems.org/.........
       Fetching version metadata from https://rubygems.org/..
       Fetching dependency metadata from https://rubygems.org/.
       Resolving dependencies...
       Fetching rake 12.3.0
       Fetching concurrent-ruby 1.0.5
       Fetching minitest 5.10.3
       Installing minitest 5.10.3
       Installing rake 12.3.0
       Installing concurrent-ruby 1.0.5
       Fetching thread_safe 0.3.6
       Installing thread_safe 0.3.6
       Fetching builder 3.2.3
       Installing builder 3.2.3
       Fetching erubi 1.7.0
       Fetching mini_portile2 2.3.0
       Installing erubi 1.7.0
       Fetching crass 1.0.3
       Installing mini_portile2 2.3.0
       Fetching rack 2.0.3
       Installing crass 1.0.3
       Fetching nio4r 2.2.0
       Installing rack 2.0.3
       Installing nio4r 2.2.0 with native extensions
       Fetching websocket-extensions 0.1.3
       Installing websocket-extensions 0.1.3
       Fetching mini_mime 1.0.0
       Installing mini_mime 1.0.0
       Fetching arel 8.0.0
       Installing arel 8.0.0
       Fetching execjs 2.7.0
       Using bcrypt 3.1.11 from https://github.com/codahale/bcrypt-ruby.git (at master@f2db689)
       Installing execjs 2.7.0
       Fetching rb-fsevent 0.10.2
       Installing rb-fsevent 0.10.2
       Fetching ffi 1.9.18
       Installing ffi 1.9.18 with native extensions
       Fetching will_paginate 3.1.6
       Installing will_paginate 3.1.6
       Using bundler 1.15.2
       Fetching mime-types-data 3.2016.0521
       Installing mime-types-data 3.2016.0521
       Fetching coffee-script-source 1.12.2
       Installing coffee-script-source 1.12.2
       Fetching method_source 0.9.0
       Installing method_source 0.9.0
       Fetching thor 0.20.0
       Installing thor 0.20.0
       Fetching excon 0.60.0
       Installing excon 0.60.0
       Fetching formatador 0.2.5
       Installing formatador 0.2.5
       Fetching multi_json 1.12.2
       Installing multi_json 1.12.2
       Fetching ipaddress 0.8.3
       Installing ipaddress 0.8.3
       Fetching mini_magick 4.7.0
       Installing mini_magick 4.7.0
       Fetching pg 0.18.4
       Installing pg 0.18.4 with native extensions
       Fetching puma 3.9.1
       Installing puma 3.9.1 with native extensions
       Fetching tilt 2.0.8
       Installing tilt 2.0.8
       Fetching turbolinks-source 5.0.3
       Installing turbolinks-source 5.0.3
       Fetching i18n 0.9.1
       Installing i18n 0.9.1
       Fetching tzinfo 1.2.4
       Installing tzinfo 1.2.4
       Fetching nokogiri 1.8.1
       Installing nokogiri 1.8.1 with native extensions
       Fetching websocket-driver 0.6.5
       Installing websocket-driver 0.6.5 with native extensions
       Fetching mail 2.7.0
       Installing mail 2.7.0
       Fetching rack-test 0.8.2
       Installing rack-test 0.8.2
       Fetching sprockets 3.7.1
       Installing sprockets 3.7.1
       Fetching autoprefixer-rails 7.2.3
       Installing autoprefixer-rails 7.2.3
       Fetching uglifier 3.2.0
       Installing uglifier 3.2.0
       Fetching bootstrap-will_paginate 1.0.0
       Installing bootstrap-will_paginate 1.0.0
       Fetching mime-types 3.1
       Installing mime-types 3.1
       Fetching coffee-script 2.4.1
       Installing coffee-script 2.4.1
       Fetching fog-core 1.45.0
       Installing fog-core 1.45.0
       Fetching rb-inotify 0.9.10
       Installing rb-inotify 0.9.10
       Fetching turbolinks 5.0.1
       Installing turbolinks 5.0.1
       Fetching faker 1.7.3
       Installing faker 1.7.3
       Fetching activesupport 5.1.4
       Installing activesupport 5.1.4
       Fetching fog-json 1.0.2
       Installing fog-json 1.0.2
       Fetching sass-listen 4.0.0
       Installing sass-listen 4.0.0
       Fetching globalid 0.4.1
       Installing globalid 0.4.1
       Fetching activemodel 5.1.4
       Installing activemodel 5.1.4
       Fetching jbuilder 2.7.0
       Installing jbuilder 2.7.0
       Fetching sass 3.5.4
       Installing sass 3.5.4
       Fetching activejob 5.1.4
       Installing activejob 5.1.4
       Fetching activerecord 5.1.4
       Installing activerecord 5.1.4
       Fetching carrierwave 1.1.0
       Installing carrierwave 1.1.0
       Fetching bootstrap-sass 3.3.7
       Installing bootstrap-sass 3.3.7
       Fetching rails-dom-testing 2.0.3
       Fetching loofah 2.1.1
       Installing rails-dom-testing 2.0.3
       Installing loofah 2.1.1
       Fetching fog-xml 0.1.3
       Installing fog-xml 0.1.3
       Fetching rails-html-sanitizer 1.0.3
       Installing rails-html-sanitizer 1.0.3
       Fetching fog-aws 2.0.0
       Fetching actionview 5.1.4
       Installing actionview 5.1.4
       Installing fog-aws 2.0.0
       Fetching actionpack 5.1.4
       Installing actionpack 5.1.4
       Fetching actioncable 5.1.4
       Fetching actionmailer 5.1.4
       Installing actionmailer 5.1.4
       Installing actioncable 5.1.4
       Fetching railties 5.1.4
       Fetching sprockets-rails 3.2.1
       Installing sprockets-rails 3.2.1
       Installing railties 5.1.4
       Fetching coffee-rails 4.2.2
       Fetching jquery-rails 4.3.1
       Installing coffee-rails 4.2.2
       Fetching rails 5.1.4
       Installing rails 5.1.4
       Fetching sass-rails 5.0.6
       Installing sass-rails 5.0.6
       Installing jquery-rails 4.3.1
       Bundle complete! 29 Gemfile dependencies, 74 gems now installed.
       Gems in the groups development and test were not installed.
       Bundled gems are installed into ./vendor/bundle.
       Bundle completed (32.67s)
       Cleaning up the bundler cache.
-----> Installing node-v6.11.1-linux-x64
-----> Detecting rake tasks
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       LoadError: cannot load such file -- fog
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/uploader/configuration.rb:122:in `eager_load_fog'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/uploader/configuration.rb:137:in `fog_credentials='
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/config/initializers/carrier_wave.rb:3:in `block in <top (required)>'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/uploader/configuration.rb:159:in `configure'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/carrierwave-1.1.0/lib/carrierwave.rb:14:in `configure'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/config/initializers/carrier_wave.rb:2:in `<top (required)>'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `block in load'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/engine.rb:655:in `block in load_config_initializer'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/notifications.rb:168:in `instrument'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/engine.rb:654:in `load_config_initializer'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/engine.rb:612:in `block (2 levels) in <class:Engine>'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/engine.rb:611:in `each'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/engine.rb:611:in `block in <class:Engine>'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `instance_exec'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `run'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/initializable.rb:59:in `block in run_initializers'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/initializable.rb:48:in `each'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/initializable.rb:48:in `tsort_each_child'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/initializable.rb:58:in `run_initializers'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/application.rb:353:in `initialize!'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/config/environment.rb:5:in `<top (required)>'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/application.rb:329:in `require'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/application.rb:329:in `require_environment!'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/application.rb:445:in `block in run_tasks_blocks'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:62:in `block (2 levels) in define'
       /tmp/build_09f91bebad38e0d893f4a5cd532d3b6b/vendor/bundle/ruby/2.3.0/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
       Tasks: TOP => environment
       (See full trace by running task with --trace)
 !
 !     Precompiling assets failed.
 !
 !     Push rejected, failed to compile Ruby app.
 !     Push failed

6条回答
放荡不羁爱自由
2楼-- · 2019-06-24 04:52

Minor update: I was able to reproduce the Heroku deployment production error in development mode.

LoadError: cannot load such file -- fog

Gemfile:

gem 'carrierwave',             '1.1.0'  #also tried 1.2.1 with same results
gem 'mini_magick',             '4.7.0'
gem 'fog-aws',                 '2.0.0'

config/initiliazers/carrierwave.rb

if Rails.env.production?
  CarrierWave.configure do |config|
    config.fog_credentials = {
      # Configuration for Amazon S3
      :provider              => 'AWS',
      :aws_access_key_id     => ENV['S3_ACCESS_KEY'],
      :aws_secret_access_key => ENV['S3_SECRET_KEY']
    }
    config.fog_directory     =  ENV['S3_BUCKET']
end

To solve the issue in development mode either of the following steps worked for me:

1) Add the following to carrierwave.rb before the credentials (and with only gem 'fog-aws' in the gemfile):

config.fog_provider = 'fog/aws'

2) add fog gem to the gemfile:

gem 'fog',  '1.41'

With this carrierwave picture upload to Amazon S3 is now fully functional in development mode. Unfortunately this does not solve the problem deploying to Heroku. Could it be that Heroku is unable to establish the provider based on the above? The production log indicates the fog-aws is installed so I dont get it.

查看更多
劳资没心,怎么记你
3楼-- · 2019-06-24 04:57

Try to add gem 'fog' directly to your Gemfile

Gemfile:

  gem 'fog'
  gem 'fog-aws'

if you define gem in Gemfile it will automatically require it (by default).

查看更多
可以哭但决不认输i
4楼-- · 2019-06-24 04:58

Had the same issue. I resolved in this way:

Gemfile:

gem 'fog-aws', group: :production

Adding to config/initializers/carrierwave.rb:

config.fog_provider = 'fog/aws'

After you don't need upgrading carrierwave.

查看更多
叛逆
5楼-- · 2019-06-24 05:12

Well,I got same issue but different reason. I coded wrong symbol "provide" in carrier_wave.rb configration

:provide => "AWS"

It caused a error at

require self.fog_provider

and retrieved heroku logs in

/var/lib/gems/2.5.0/gems/carrierwave-1.2.1/lib/carrierwave/uploader/configuration.rb :122:in `eager_load_fog'

self.fog_provider default is "fog",so is that.

Then I add gem "fog" in my gemfile ,it worked

查看更多
等我变得足够好
6楼-- · 2019-06-24 05:13

First, uninstall the gem then install the latest version which is 1.2.1

查看更多
叛逆
7楼-- · 2019-06-24 05:15

Finally resolved the problem and it was so simple. Embarrassingly so! All I needed to do is reset the database at Heroku (before pushing)

heroku pg:reset DATABASE

Now deployment to Heroku works without crashing (including picture uploads to S3 AWS)

查看更多
登录 后发表回答