Error “uninitialized constant AWS (NameError)”

前端 未结 7 1763
梦毁少年i
梦毁少年i 2021-02-06 21:39

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 initializ

相关标签:
7条回答
  • 2021-02-06 22:14

    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.

    0 讨论(0)
  • 2021-02-06 22:18

    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.

    0 讨论(0)
  • 2021-02-06 22:23

    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.

    0 讨论(0)
  • 2021-02-06 22:25

    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.

    0 讨论(0)
  • 2021-02-06 22:37

    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.

    0 讨论(0)
  • 2021-02-06 22:38

    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

    0 讨论(0)
提交回复
热议问题