Error “uninitialized constant AWS (NameError)”

。_饼干妹妹 提交于 2019-12-20 15:58:09

问题


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

I tried using the aws-sdk gem instead, and the problem was still there.

This is the initializers/aws.rb file:

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)


回答1:


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.

Make sure which version you want to install:

aws-sdk version 3

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

# call sdk    
Aws.<whatever>

aws-sdk version 2

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

# call sdk    
Aws.<whatever>

aws-sdk version 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.




回答2:


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

See this blog post for more information.




回答3:


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

To avoid any confusion, for scripts that require 1.x, use:

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

And for scripts that require 2.x, use:

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

as the GitHub documentation indicates.




回答4:


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

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.

Alternatively, if the above code was in a non-rails application, just place require 'aws-sdk' on the first line.




回答5:


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

Amazon released version 2 of the aws-sdk in early February 2015. Version 2 is not entirely backwards compatible with version 1.

So, you must make a decision - are you content with version 1 functionality, or do you want version 2 functionality?

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):

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.

After convergence, run a gem list on your instance to see the gem versions:

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

On 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:

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.

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




回答6:


I am not a Ruby expert, but I've solved the same issue by running the below commands.

To remove the installed 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:

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.




回答7:


I was facing the same problem. One answer worked here without updating the gem.

Simply change wherever required [in th require statement in environment]

require 'aws-sdk'

to

require 'aws-sdk-v1'


来源:https://stackoverflow.com/questions/22826432/error-uninitialized-constant-aws-nameerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!