Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Amazon Releases aws-sdk, An Official AWS SDK for Ruby Developers

By Peter Cooper / July 15, 2011

Amazon has unveiled an official Ruby SDK for AWS! Amazon.com's Amazon Web Services has been a rip-roaring success since its first publicly-available service, S3 (Simple Storage Storage), was released in 2006. It has since expanded to about 20 services in all, the most popular being S3 and the "elastic compute cloud" EC2.

There have previously been unofficial Ruby libraries for interfacing with Amazon's many services, including PoolParty, right_aws, and Marcel Molina's awesome aws-s3, and Amazon even released some bits and pieces of Ruby code before, but the new aws-sdk gem represents a stronger effort to have a single, official cohesive library for Rubyists using AWS.

What Does aws-sdk Support?

A quick laundry list:

  • EC2
  • S3
  • SQS (queue service)
  • SNS (notifications service)
  • SES (Simple E-mail Service)
  • SimpleDB (in the form of an ORM)

This covers the most used items of the AWS suite though some further investigation needs to be done to see if Elastic MapReduce and Elastic Block Store has been baked into the EC2 stuff. There's no Route 53 or Mechanical Turk support as yet, though Mark Percival's RTurk library is still being updated and supports the latter.

Getting Started With aws-sdk

Installing the new official AWS SDK gem is a piece of cake:

gem install aws-sdk

(Note: aws-sdk has the Nokogiri XML/HTML parsing library as a dependency so if you're using MRI, you'll need to be in an environment where that can be compiled. It isn't pure Ruby.)

Now, let's roll out a simple file upload system using the S3 support available in aws-sdk:

require 'aws-sdk'

bucket_name = 'mytestbucket'
source_filename = '/tmp/something.txt'

AWS.config(
  :access_key_id => ACCESS_KEY_ID,
  :secret_access_key => SECRET_ACCESS_KEY
)

# Create the basic S3 object
s3 = AWS::S3.new

# Load up the 'bucket' we want to store things in
bucket = s3.buckets[bucket_name]

# If the bucket doesn't exist, create it
unless bucket.exists?
  puts "Need to make bucket #{bucket_name}.."
  s3.buckets.create(bucket_name)
end

# Grab a reference to an object in the bucket with the name we require
object = bucket.objects[File.basename(source_filename)]

# Write a local file to the aforementioned object on S3
object.write(:file => source_filename)

Note that you'll need to set ACCESS_KEY_ID and SECRET_ACCESS_KEY somehow. You could do this with a YAML file you load in, hard code into your script (not recommended for security reasons), or perhaps use environment variables (a common approach with the Java tools, I believe). You can get your own AWS Access Key ID and the Secret Access Key from your Security Credentials page in your AWS settings.

If you are familiar with the already existing AWS::S3 library, you should note that the new aws-sdk library is not compatible with it and it cannot be loaded at the same time. You should also note that the code used to make everything work is quite different with aws-sdk and while porting a script from one to the other shouldn't be a lengthy job, it's not a straightforward search and replace either.

Further Reading

This was just a flying visit with a basic overview of using aws-sdk for S3. To learn more, hit up the aws-sdk-for-ruby repository on GitHub to see more examples and sample code, as well as the official AWS SDK for Ruby page by Amazon.

One thing I noticed is that if you open up the aws-sdk gem and snoop around the source code, you can learn a lot more than from Amazon's own articles. The code is well documented and I had to refer to it a few times already. So definitely crack open that gem and have a dig around, you'll find useful stuff in there.

[sponsor]RubyMine is a popular and powerful Ruby and Rails IDE with the full stack of essential developer tools, all tightly integrated into a convenient and smart development environment. Download it now for a free 30 day trial.

Comments

  1. Roman2K says:

    AWS.config(...): does it mean there can only be one single AWS connection per process!? (One particular access key ID / secret pair.) I haven't looked at the code but if that's true, it sounds like a major design flaw. Nothing that can't be fixed but still, surprising.

  2. Peter Cooper says:

    Luckily it's not that bad, Roman2K :-) You can add the same config options to the AWS::S3.new call so you could have multiple S3 'instances' running, as it were.

    They could certainly do with documenting this all a bit better..

  3. Roman Le Négrate says:

    Thanks for the info, Peter.

  4. Salman says:

    Great news, I was dreaming of the day I can write my ec2 concoctions in ruby :)

  5. geemus (Wesley Beary) says:

    There are a number of other libraries for Ruby + AWS (as Peter mentioned). Unfortunately you still need to know a good deal about how AWS does things and you are on your own all over again if you decide to try a different cloud.

    If you are considering exploring cloud stuff I highly recommend trying out fog. It supports most of the same services as this sdk, but also provides mocking and support for a bunch of other services across several providers. That way you can try out many services easily and choose the ones that work for you (knowing that you can always swap it out later if things change).

  6. Peter Cooper says:

    Ahh, I was trying to remember fog when doing that list of alternative gems! Thanks for mentioning it.

  7. Derek says:

    Sweet!!!

  8. Pingback: Ruby / [PODCAST] Ruby NoName Podcast S03E03 | Агрегатор ИТ новостей

  9. Srikanth Jeeva says:

    Nice :) Hope this make things easier ..

Other Posts to Enjoy

Twitter Mentions