Ruby-on-rails 错误“未初始化的常量 AWS (NameError)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22826432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:30:49  来源:igfitidea点击:

Error "uninitialized constant AWS (NameError)"

ruby-on-railsamazon-web-servicesruby-on-rails-4amazon-s3aws-sdk-ruby

提问by bezzoon

It is saying AWS is uninitialized. I am usign the aws-sdk-core gem.

据说 AWS 未初始化。我正在使用 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:

这是初始化程序/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)

采纳答案by Simon Franzen

You might getting this error, because you didn't define the correct aws sdk version in your Gemfile.This can happen while re-bundling old apps with version 1 or 2 installed.

您可能会收到此错误,因为您没有在 Gemfile 中定义正确的 aws sdk 版本。在重新捆绑安装了版本 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 AWSand v2 and v3 scoped under Aws=> That allows you to run v1 and v2 side by side.

v1 的范围在AWSv2 和 v3 的范围下Aws=> 这允许您并排运行 v1 和 v2。

回答by Trevor Rowe

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. Version 2 uses the Awsnamespace, not AWS. This allows version 1 and version 2 to be used in the same application.

如果您收到此错误并且安装了“aws-sdk”gem,则您可能无意中升级到了 aws-sdk gem 的第 2 版。版本 2 使用Aws命名空间,而不是AWS. 这允许在同一应用程序中使用版本 1 和版本 2。

See this blog postfor more information.

有关更多信息,请参阅此博客文章

回答by DrStrangepork

You need to install/use the -v1version of aws-sdk. Simply doing gem 'aws-sdk'or require 'aws-sdk'may use the 2.x version of aws-sdkinstead.

你需要安装/使用-v1的版本aws-sdk。简单地做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 documentationindicates.

正如GitHub 文档所示。

回答by Chris Aitchison

It sounds as though either the gem isn't present in your load path or it is not being required.

听起来好像您的加载路径中不存在 gem 或不需要它。

The entry in your Gemfileshould 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 serveror bundle exec rails console.

这将require 'aws-sdk'在应用程序初始化时隐式执行 a ,只要您使用bundle exec rails server或启动应用程序bundle 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'第一行。

回答by doer

I encountered this problem in a Chef recipe, so the response below is decidedly Chef-centric.

我在 Chef recipe 中遇到过这个问题,所以下面的回复显然是以 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. To do this you must specify the version attribute to load in the recipe that loads chef_gem aws-sdk. The modification looks like this (probably implemented in a default.rb for the cookbook in question):

如果您对版本 1 感到满意,也许是短期的,有必要让 Chef 显式加载版本 1,因为默认情况下,它似乎使用最新版本。为此,您必须指定要在加载chef_gem aws-sdk 的配方中加载的版本属性。修改看起来像这样(可能在有问题的食谱的 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. Update the cookbook version in the environment, then upload the environment to your Chef server.

更新说明书元数据中的版本,然后将说明书上传到您的 Chef 服务器。更新环境中的说明书版本,然后将环境上传到您的 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 | select-string aws-sdk

在 PowerShell PS C:\Users\Administrator> gem list |上 选择字符串 aws-sdk

On Linux: gem list | grep -i aws-sdk

在 Linux 上: gem list | 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. Now, you must update your recipe to require the older version of aws-sdk. Change this:

请注意,最后一个指定 aws-sdk-v1。现在,您必须更新您的配方以需要旧版本的 aws-sdk。改变这个:

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

回答by Zelocox

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'

回答by vikas027

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 gems

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 建议使用最新版本,但这对于不想修改现有脚本的人来说应该很有用。