如何解决此错误NameError:未初始化的常量AWS

时间:2020-08-07 18:55:40

标签: ruby-on-rails heroku web-deployment rails-activestorage aws-sdk-ruby

所以我得到了这个错误:NameError:未初始化的常量,StackOverflow表示这是无意中的升级,但是由于我的版本是最高的,所以不能在我的gem文件中找到它:gem“ aws-sdk-s3” ,要求:false

可能与初始化文件有关吗? 这是在构建日志中出错的地方:

Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       NameError: uninitialized constant AWS
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/active_support.rb:80:in `block in load_missing_constant'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/active_support.rb:9:in `without_bootsnap_cache'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/active_support.rb:80:in `rescue in load_missing_constant'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/active_support.rb:59:in `load_missing_constant'
       /tmp/build_381cfb05_/config/initializers/aws.rb:1:in `<main>'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:666:in `block in load_config_initializer'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.2/lib/active_support/notifications.rb:182:in `instrument'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:665:in `load_config_initializer'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:625:in `block (2 levels) in <class:Engine>'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:624:in `each'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:624:in `block in <class:Engine>'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:32:in `instance_exec'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:32:in `run'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:61:in `block in run_initializers'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:50:in `each'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:50:in `tsort_each_child'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:60:in `run_initializers'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/application.rb:363:in `initialize!'
       /tmp/build_381cfb05_/config/environment.rb:5:in `<main>'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/zeitwerk-2.4.0/lib/zeitwerk/kernel.rb:34:in `require'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/application.rb:339:in `require_environment!'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/application.rb:523:in `block in run_tasks_blocks'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:62:in `block (2 levels) in define'
       
       Caused by:
       NameError: uninitialized constant AWS
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/active_support.rb:61:in `block in load_missing_constant'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/active_support.rb:17:in `allow_bootsnap_retry'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/active_support.rb:60:in `load_missing_constant'
       /tmp/build_381cfb05_/config/initializers/aws.rb:1:in `<main>'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:666:in `block in load_config_initializer'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.2/lib/active_support/notifications.rb:182:in `instrument'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:665:in `load_config_initializer'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:625:in `block (2 levels) in <class:Engine>'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:624:in `each'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/engine.rb:624:in `block in <class:Engine>'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:32:in `instance_exec'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:32:in `run'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:61:in `block in run_initializers'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:50:in `each'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:50:in `tsort_each_child'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/initializable.rb:60:in `run_initializers'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/application.rb:363:in `initialize!'
       /tmp/build_381cfb05_/config/environment.rb:5:in `<main>'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/zeitwerk-2.4.0/lib/zeitwerk/kernel.rb:34:in `require'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/application.rb:339:in `require_environment!'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/application.rb:523:in `block in run_tasks_blocks'
       /tmp/build_381cfb05_/vendor/bundle/ruby/2.6.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:62:in `block (2 levels) in define'
       Tasks: TOP => environment
       (See full trace by running task with --trace)
 !
 !     Precompiling assets failed.
 !
 !     Push rejected, failed to compile Ruby app.
 !     Push failed

这是aws.rb文件:

AWS::S3::Base.establish_connection!(
    :access_key_id   => ENV['S3_KEY:'],
    :secret_access_key => ENV['S3_SECRET']
   )
   

这是在Heroku方面: enter image description here

我还不能嵌入图像,因此只有链接。

好的,我将aws.rb文件更改为此:

require 'aws-sdk'
AWS::S3::Base.establish_connection!(
    :access_key_id   => ENV['S3_KEY:'],
    :secret_access_key => ENV['S3_SECRET']
   )
   

得到了:

 Running: rake assets:precompile
       rake aborted!
       LoadError: cannot load such file -- aws-sdk

我将AWS gem更改为:gem'aws-sdk'

并从构建日志失败中获得了此输出:

Running: rake assets:precompile
       rake aborted!
       NameError: uninitialized constant AWS

这是我的storage.yml文件的一部分:

# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
amazon:
   service: S3
   #access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
  # secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
   region: us-east-1
   bucket: agrifood.bucket

注释掉aws.rb初始化器文件并将storage.yml更改为以下内容后:

# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
amazon:
   service: S3
   #access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
   #access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id  => ENV['S3_KEY:']) %>
   access_key_id: ENV['S3_KEY:'])
   secret_access_key: ENV['S3_SECRET'])
   #secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key => ENV['S3_SECRET']) %>
   #secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
   #region: us-east-1
   region: eu-west-1
   bucket: agrifood.bucket

然后添加Active Storage构建包,我得到了:

###### WARNING:
       We detected that some binary dependencies required to
       use all the preview features of Active Storage are not
       present on this system.
       
       For more information please see:
         https://devcenter.heroku.com/articles/active-storage-on-heroku

最近,我将storage.yml更改为:

amazon:
   service: S3
   #access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
   #access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id  => ENV['S3_KEY:']) %>
   access_key_id: ENV['S3_KEY:'])
   secret_access_key: ENV['S3_SECRET'])
   #secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key => ENV['S3_SECRET']) %>
   #secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
   #region: us-east-1
   #region: eu-west-1
   region: ENV['AWS_BUCKET_REGION'])
   bucket: agrifood.bucket
'''
Which still gave me this:

警告: 检测滑轨配置失败 设置HEROKU_DEBUG_RAILS_RUNNER = 1进行调试

警告:
   We detected that some binary dependencies required to
   use all the preview features of Active Storage are not
   present on this system.

I then changed it to this:

Aws.config.update({ 地区:“ eu-west-1”, 凭证:Aws :: Credentials.new(ENV ['S3_KEY:'],ENV ['S3_SECRET']), })

S3_BUCKET = Aws :: S3 :: Resource.new.bucket(ENV ['S3_BUCKET'])


As per the advice to changed AWS to Aws I did what is just above, however, that didn't fix it.

2 个答案:

答案 0 :(得分:0)

尝试制作bundle install,但无济于事,请与我们共享文件config/initializers/aws.rb,以更好地了解问题。该错误是因为您没有在应用程序中使用AWS

答案 1 :(得分:0)

我通过将 Gem 更改为以下内容来解决此问题:

 gem 'aws-sdk-v1'

并删除:需要'aws-sdk'。

这对我有用。 谢谢