【问题标题】:Carrierwave/fog/AWS issue when loading to Heroku "cannot load such file -- fog"加载到 Heroku 时的 Carrierwave/fog/AWS 问题“无法加载此类文件 - 雾”
【发布时间】:2018-06-08 04:37:15
【问题描述】:

所以我是个新手,正在阅读 Michael Hartl 的 rails 教程。尝试使用carrierwave,fog-aws到S3 AWS配置图片上传时,我完全陷入困境。推送到 heroku 时出现以下错误:

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

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

这是我的设置: 宝石文件

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 日志错误:

!     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

【问题讨论】:

标签: ruby-on-rails heroku amazon-s3 carrierwave fog


【解决方案1】:

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

宝石文件:

gem 'fog-aws', group: :production

添加到 config/initializers/carrierwave.rb:

config.fog_provider = 'fog/aws'

在您不需要升级carrierwave之后。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我尝试使用carrierwave 1.1.0 仍然是同样的错误。
    • 还尝试使用carrierwave 1.2.1,结果相同:(
    【解决方案3】:

    尝试将 gem 'fog' 直接添加到您的 Gemfile 中

    宝石文件:

      gem 'fog'
      gem 'fog-aws'
    

    如果您在 Gemfile 中定义 gem,它将自动需要它(默认情况下)。

    【讨论】:

      【解决方案4】:

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

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

      宝石文件:

      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/initiliars/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(并使用 gemfile 中只有 gem 'fog-aws'):

      config.fog_provider = 'fog/aws'
      

      2) 将雾 gem 添加到 gemfile:

      gem 'fog',  '1.41'
      

      通过此载波图片上传到 Amazon S3,现在可以在开发模式下完全正常运行。不幸的是,这并不能解决部署到 Heroku 的问题。难道是 Heroku 无法根据上述情况建立提供者?生产日志表明fog-aws已安装,所以我不明白。

      【讨论】:

      • 您只能安装 fog-aws 而不是正确安装雾,其中包括多个提供程序的库。
      【解决方案5】:

      终于解决了问题,就这么简单。太丢人了! 我需要做的就是在 Heroku 重置数据库(在推送之前)

      heroku pg:reset DATABASE
      

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

      【讨论】:

        【解决方案6】:

        好吧,我遇到了同样的问题,但原因不同。 我在carrier_wave.rb 配置中编码了错误的符号“提供”

        :provide => "AWS"
        

        它导致了一个错误

        require self.fog_provider
        

        并检索了heroku登录

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

        self.fog_provider 默认是"fog",就是这样。

        然后我在我的gemfile 中添加gem "fog",它起作用了

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-30
          • 1970-01-01
          • 1970-01-01
          • 2012-04-12
          • 2021-05-16
          • 2015-08-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多