简体   繁体   English

错误“未初始化的常量AWS(NameError)”

[英]Error “uninitialized constant AWS (NameError)”

It is saying AWS is uninitialized. 这是说AWS未初始化。 I am usign the aws-sdk-core gem. 我使用了aws-sdk-core gem。

I tried using the aws-sdk gem instead, and the problem was still there. 我尝试使用aws-sdk gem,问题仍然存在。

This is the initializers/aws.rb file: 这是initializers / aws.rb文件:

AWS.config(:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
                      :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'])

s3 = AWS::S3.new
AVATAR_BUCKET = s3.buckets[ENV['AVATAR_BUCKET_NAME']]

When I try running the server or opening the console I get this error: 当我尝试运行服务器或打开控制台时,我收到此错误:

/initializers/aws.rb:1:in `': uninitialized constant AWS (NameError) /initializers/aws.rb:1:in`':未初始化的常量AWS(NameError)

If you are receiving this error and you have the "aws-sdk" gem installed, you likely have upgraded to version 2 of the aws-sdk gem unintentionally . 如果您收到此错误并且安装了“aws-sdk”gem,则可能无意中升级到了aws-sdk gem的版本2 Version 2 uses the Aws namespace, not AWS . 版本2使用Aws命名空间,而不是AWS This allows version 1 and version 2 to be used in the same application. 这允许版本1和版本2在同一应用程序中使用。

See this blog post for more information. 有关更多信息,请参阅此博客文章

You need to install/use the -v1 version of aws-sdk . 您需要安装/使用aws-sdk-v1版本。 Simply doing gem 'aws-sdk' or require 'aws-sdk' may use the 2.x version of aws-sdk instead. 简单地做gem 'aws-sdk'或者require 'aws-sdk'可以使用2.x版本的aws-sdk代替。

To avoid any confusion , for scripts that require 1.x, use: 为避免混淆 ,对于需要1.x的脚本,请使用:

require 'aws-sdk-v1' # not 'aws-sdk'

And for scripts that require 2.x, use: 对于需要2.x的脚本,请使用:

gem 'aws-sdk', '~> 2'

as the GitHub documentation indicates. 正如GitHub文档所示。

You might getting this error, because you didn't define the correct aws sdk version in your Gemfile. 您可能会收到此错误,因为您没有在Gemfile中定义正确的aws sdk版本。 This can happen while re-bundling old apps with version 1 or 2 installed. 在重新捆绑安装了版本1或2的旧应用程序时可能会发生这种情况。

Make sure which version you want to install: 确保要安装的版本:

aws-sdk version 3 aws-sdk版本3

gem 'aws-sdk', '~> 3'

# call sdk    
Aws.<whatever>

aws-sdk version 2 aws-sdk版本2

gem 'aws-sdk', '~> 2'

# call sdk    
Aws.<whatever>

aws-sdk version 1 aws-sdk版本1

# version constraint
gem 'aws-sdk', '< 2'

# or 

# use the v1 gem
gem 'aws-sdk-v1'

# call sdk    
AWS.<whatever>

v1 is scoped under AWS and v2 and v3 scoped under Aws => That allows you to run v1 and v2 side by side. v1的范围是AWS ,v2和v3的范围是Aws =>这允许你并排运行v1和v2。

It sounds as though either the gem isn't present in your load path or it is not being required. 听起来好像宝石不存在于您的加载路径中或者不是必需的。

The entry in your Gemfile should be 你的Gemfile的条目应该是

gem 'aws-sdk'

This will implicitly do a require 'aws-sdk' as the application initializes, as long as you start the app with bundle exec rails server or bundle exec rails console . 这将隐式执行require 'aws-sdk'作为应用程序初始化,只要您使用bundle exec rails serverbundle exec rails console启动应用程序。

Alternatively, if the above code was in a non-rails application, just place require 'aws-sdk' on the first line. 或者,如果上面的代码在非rails应用程序中,只require 'aws-sdk'在第一行上输入require 'aws-sdk'

I encountered this problem in a Chef recipe, so the response below is decidedly Chef-centric. 我在Chef配方中遇到了这个问题,所以下面的响应肯定是以Chef为中心的。

Amazon released version 2 of the aws-sdk in early February 2015. Version 2 is not entirely backwards compatible with version 1. 亚马逊于2015年2月初发布了aws-sdk的第2版。版本2与版本1并不完全向后兼容。

So, you must make a decision - are you content with version 1 functionality, or do you want version 2 functionality? 因此,您必须做出决定 - 您是否满足于版本1功能,或者您是否需要版本2功能?

If you are content with version 1, perhaps for the short term, it is necessary to have Chef explicitly load version 1, because by default, it appears to use the latest version. 如果您满足版本1(可能是短期版本),则必须让Chef明确加载版本1,因为默认情况下,它似乎使用最新版本。 To do this you must specify the version attribute to load in the recipe that loads chef_gem aws-sdk. 要执行此操作,您必须在加载chef_gem aws-sdk的配方中指定要加载的版本属性。 The modification looks like this (probably implemented in a default.rb for the cookbook in question): 修改看起来像这样(可能在相关的cookbook的default.rb中实现):

chef_gem "aws-sdk" do
  action :nothing

  # Source:  https://aws.amazon.com/releasenotes/Ruby?browse=1
  version '1.62.0'  

end.run_action(:install)

Update the version in the cookbook's metadata, then upload the cookbook to your Chef server. 更新cookbook元数据中的版本,然后将cookbook上传到Chef服务器。 Update the cookbook version in the environment, then upload the environment to your Chef server. 更新环境中的cookbook版本,然后将环境上载到Chef服务器。

After convergence, run a gem list on your instance to see the gem versions: 收敛后,在您的实例上运行gem列表以查看gem版本:

On PowerShell PS C:\\Users\\Administrator> gem list | 在PowerShell PS C:\\ Users \\ Administrator> gem list | select-string aws-sdk select-string aws-sdk

On Linux: gem list | 在Linux上:宝石列表| grep -i aws-sdk grep -i aws-sdk

These are typical results: 这些是典型的结果:

aws-sdk (2.0.27, 1.62.0) 
aws-sdk-core (2.0.27) 
aws-sdk-resources (2.0.27) 
aws-sdk-v1 (1.62.0)

Note that the last one specifies aws-sdk-v1. 请注意,最后一个指定aws-sdk-v1。 Now, you must update your recipe to require the older version of aws-sdk. 现在,您必须更新您的配方以要求旧版本的aws-sdk。 Change this: 改变这个:

require 'aws-sdk'

to this: 对此:

require 'aws-sdk-v1'

Update the version in the metadata.rb, upload the cookbook, update the version in the environment file, upload the environment, and you should be good to go after the next convergence. 更新metadata.rb中的版本,上传cookbook,更新环境文件中的版本,上传环境,你应该好好去下一次收敛。

This blog post contains more details and solutions to this problem: http://ruby.awsblog.com/post/TxFKSK2QJE6RPZ/Upcoming-Stable-Release-of-AWS-SDK-for-Ruby-Version-2 此博客文章包含有关此问题的更多详细信息和解决方案: http//ruby.awsblog.com/post/TxFKSK2QJE6RPZ/Upcoming-Stable-Release-of-AWS-SDK-for-Ruby-Version-2

I am not a Ruby expert, but I've solved the same issue by running the below commands. 我不是Ruby专家,但我通过运行以下命令解决了同样的问题。

To remove the installed AWS gems 删除已安装的AWS宝石

gem list --no-version --local | grep aws | xargs gem uninstall -aIx

To install the v1 gem which was compatible with my Ruby script: 要安装与我的Ruby脚本兼容的v1 gem:

gem install aws-sdk -v 1.64.0

I agree this is not the recommended way as AWS recommends to use the latest version, but this should be useful for someone who does not want to modify their existing scripts. 我同意这不是推荐的方式,因为AWS建议使用最新版本,但这对于不想修改现有脚本的人来说应该是有用的。

I was facing the same problem. 我遇到了同样的问题。 One answer worked here without updating the gem . 一个答案在这里没有更新gem

Simply change wherever required [in th require statement in environment] 只需根据需要进行更改[in th require statement in environment]

require 'aws-sdk'

to

require 'aws-sdk-v1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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