January 31, 2007

CLI to Amazon S3 Service using Ruby

I was looking for a way to post code referred to in these posts that could accessed using URL's. I found that Amazon's S3 service, part of their AWS (Amazon Web Services), offered a way to store any type of files on it and access them using http/https. Amazon provides WSDL API's to the service and the 'aws/s3' ruby gem provides an ruby library to access it and a shell program 's3sh' to access the API's in an interactive fashion. I wanted to access s3 like a filesystem using commands similar to those found in Unix like "ls","mkdir" etc. So I developed a ruby script 's3cli.rb' that uses the 'aws/s3' gem and provides a cli to it. The commands look like "s3cli.rb ls", "s3cli.rb mkdir" etc. This script is still a work in progress but has enough functionality to be useful. I have also started using it to store backups both at work and at home. The current code and readme for the script are available at
http://s3.amazonaws.com/designandcode/s3cli.rb
http://s3.amazonaws.com/designandcode/s3cli-readme.html

I used the "send" method in ruby to eliminate a lot of code that would have been needed to parse the command and call the required method/function. The concerned code looks like

s = S3.new  
command = ARGV.shift
args = ARGV
if (command)
s.send(command,args)
else
s.usage nil
end
Here the "send" method allows us to call a method of the S3 class by name. So all we need to do is parse the command name from the arguments and use "send" to call the correct method. The class defines the method "method_missing" which catches any calls for commands that do not exist yet. This eliminates the need for something like a "case" statement or a data structure mapping commands to function pointers. Further, adding a new command involves just adding another method to the class. We don't need to modify any existing code or data structure.

I already have a TODO list in the readme file. Please give me feedback on any new features you would like see, any bugs in my code or advice/critique on the ruby side of the code. Of course I will be happy to integrate any code snippets you provide into the script.

2 comments:

butlimous said...
This post has been removed by the author.
free ps3 said...

Thanks for the nice post!