slayedbylucifer
slayedbylucifer

Reputation: 23492

AWS S3 + Ruby SDK : How to list buckets

I am running fairly simple program to list S3 buckets. There are two approaches. However, both the approaches give me same error.

Approach 1: Create a s3 client and then access the client methods(list_buckets).

Code:

#!/usr/bin/ruby
require 'aws-sdk'
require 'awesome_print'
AWS.config( :access_key_id      => 'My Access Key', 
            :secret_access_key  => 'My Secret Key',
            :region             => 'us-west-2')

s3 = AWS.s3
puts s3.client.list_buckets()

Output:

/var/lib/gems/1.9.1/gems/aws-sdk-1.14.1/lib/aws/s3/client.rb:459:in `block (2 levels) in <class:Client>': uninitialized constant AWS::Core::XML::ListBuckets (NameError)

Approach 2: Create a Bucket Object and then Enumerate it.

Code:

#!/usr/bin/ruby
require 'aws-sdk'
require 'awesome_print'
AWS.config( :access_key_id      => 'My Access Key', 
            :secret_access_key  => 'My Secret Key',
            :region             => 'us-west-2')

s3obj = AWS::S3.new
s3obj.buckets.each do |bucket|
  puts bucket
end

OutPut:

/var/lib/gems/1.9.1/gems/aws-sdk-1.14.1/lib/aws/s3/client.rb:459:in `block (2 levels) in <class:Client>': uninitialized constant AWS::Core::XML::ListBuckets (NameError)

What am I doing wrong here? AFAIK, My code follows what is mentioned in each approach. This is the most basic code to list buckets and I am failing at it.

Upvotes: 1

Views: 2345

Answers (1)

slayedbylucifer
slayedbylucifer

Reputation: 23492

After reading the error more carefully, I found aws-sdk-1.14.1. So then I checked on AWS and realized that they have a newer version of Ruby SDK released. It was 1.29.1.

  1. So I cleaned AWS SDK 1.14.1 from my ubuntu.
  2. Removed ruby as well. (it was 1.9.1)
  3. Installed ruby 1.9.2 from source
    • Newer Ruby was needed for the newer AWS-SDK to work
    • to be more precise, newer version of AWS-SDK has a newer version of nokogiri which in turn need ruby 1.9.2 or later.
    • so I ended up installing ruby 1.9.2 from source as the Ubuntu I have is 10.04 which is pretty old and its apt-get repo provides only ruby 1.9.1
  4. installed AWS-SDK 1.29.1

And Now both above my code are working just fine.

So, it was probably older version of SDK that was causing the error.

Upvotes: 1

Related Questions