加载到Heroku时Carrierwave / fog / AWS问题“无法加载此类文件 – 雾”

所以我是一个新手,并通过Michael Hartl的rails教程。 在尝试使用carrierwave,fog-aws为S3 AWS配置图片上传时,我变得完全陷入困境。 当推送到heroku时,我收到以下错误:

LoadError:无法加载此类文件 – 雾

作为旁注,我尝试在开发模式中使用完全相同的代码,rails服务器工作正常,我能够成功上传文件到AWS。 因此,这不是AWS的配置设置问题。 尝试将应用程序推送到Heroku时出现问题。

这是我的设置: 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] 

应用程序/上传/ 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 

配置/初始化/ 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日志有错误:

 ! 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 ' /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 `' /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 ' /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 ' /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 `' /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 `' Tasks: TOP => environment (See full trace by running task with --trace) ! ! Precompiling assets failed. ! ! Push rejected, failed to compile Ruby app. ! Push failed 

有同样的问题。 我这样解决了:

的Gemfile:

 gem 'fog-aws', group: :production 

添加到config / initializers / carrierwave.rb:

 config.fog_provider = 'fog/aws' 

在您不需要升级carrierwave之后。

首先,卸载gem然后安装最新版本1.2.1

尝试将gem’fog’直接添加到Gemfile中

的Gemfile:

  gem 'fog' gem 'fog-aws' 

如果你在Gemfile中定义gem,它会自动要求它(默认情况下)。

次要更新:我能够在开发模式下重现Heroku部署生产错误。

LoadError:无法加载此类文件 – 雾

的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' 

配置/ 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 

要在开发模式中解决问题,以下任一步骤都适用于我:

1)在凭证之前将以下内容添加到carrierwave.rb(并且gem文件中只有gem’flow-aws’):

 config.fog_provider = 'fog/aws' 

2)将gem gem添加到gemfile:

 gem 'fog', '1.41' 

通过此载波图片,上传到Amazon S3现在可以在开发模式下完全正常运行。 不幸的是,这并没有解决部署到Heroku的问题。 可能是Heroku无法根据以上内容建立提供商吗? 生产日志表明安装了雾aws,所以我没有得到它。

最后解决了问题,这很简单。 令人尴尬的是! 我需要做的就是在Heroku重置数据库(在推送之前)

 heroku pg:reset DATABASE 

现在部署到Heroku工作而不会崩溃(包括图片上传到S3 AWS)