Wednesday, February 11, 2009

Getting started with Amazon WebServices (S3)

Amidst the slew of cloud computing offerings, one company that has stood out in their approach and marketing to attract independent developers is Amazon. A B2C company whose primary business is to sell goods online, has entered a totally different space - cloud space. Although, to my knowledge Sun Microsystems Inc.,was way ahead in this space, I believe, the company failed to put together a strong marketing message and easy-to-use API's to reach out to the development community and the budding Entrepreneurs.

Amongst the many who were attracted towards Amazon, I was one of them. Although I signed up quite some time back, it took me some time before I could execute the first program. Below is what I did to get a basic understanding of the services, the first of which I chose was S3 - the storage service.

Pre-requisites:
- Java 1.5 and above. I used JDK 1.6
- Preferably an Editor. I used Eclipse. Makes life so easy.
- Create an Amazon WebServices account at aws.amazon.com. You need the Access ID and Secret Key.
- A decent amount of understanding of Java.

Steps:
  • Download sample code from http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1366&categoryID=47.
  • Extract the ZIP to a directory.
  • Start Eclipse. Create a New Java Project and in the window, point the src to the above extracted directory where contents of the ZIP file are extracted. Note: Although the ZIP contains java, ruby and python files only java files would be visible in the project.
  • Open the file ExampleUsage.java which contains the main method to execute the code. In the main method, to take baby steps, I commented out all the method calls except usage.s3ServiceOperations().
  • In the same class, edit the method s3ServiceOperations(), replace the constructor with 2 args by constructor with 4 args that intakes AccessID and SecretID. Hard code the ID values and run the class as a simple Java Application. REST is the transport used for executing the code which you can refer to the code in class AWS which is the super class to class S3.
The REST style request that goes out to AWS is as below.
Method : GET
URI : https://s3.amazonaws.com
Headers:
Host=s3.amazonaws.com
Date=Fri, 13 Feb 2009 00:42:53 GMT
Content-Type=
Query Parameters:
Authorization=AWS 149VSd09S1C9Wt8Rm6R2:pkuInE4ZvFfw9wKdtQiZ1u1JbqA=
Host=s3.amazonaws.com
Date=Fri, 13 Feb 2009 00:42:53 GMT


The output, upon executing the above, would be as shown below - first part is the HTTP header followed by the Body. In case you see an exception stating 'NotSignedUp' with HTTP 403 error, you will need to sign up for the S3 service at http://aws.amazon.com/s3/.

Status : 200 OK
Headers:
null=HTTP/1.1 200 OK
x-amz-request-id=752C44D5CBA6171E
Date=Fri, 13 Feb 2009 00:42:49 GMT
Transfer-Encoding=chunked
x-amz-id-2=hp1PQw1VZSOh2swSE/00SJomoDpyfrMuB01g353Le8GbcZTMrDmg6FuvYMWjP53S
Content-Type=application/xml
Server=AmazonS3

Body:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Owner>
<ID>b77136276767d460dc6ea34ff6cc338489d1a4c8d7db343b7f08054170f1fea4</ID>
<DisplayName>vyaapti</DisplayName>
</Owner>
<Buckets/>
</ListAllMyBucketsResult>

Note, in the above <Buckets/> element is empty.

Creating a bucket:
In the simplest terminology, buckets is the term AWS uses to group a set of objects with a unique namespace. For more details, please refer to AWS documentation.
To create a bucket, in the above mentioned method s3ServiceOperations(), uncomment the below line that creates the bucket within the System.out statement. In my case, I also moved the System.out statement that prints the list of buckets below the above statement to reduce the number of invokes and hence save some pennies.:-) (Remember ever invoke is a data transmition to AWS and you will be charged for the same depending on the data).
After the above changes, execute the class and you should see the request going out as:

Method : PUT
URI : https://test.location3.s3.amazonaws.com/
Headers:
Expect=100-continue
Host=test.location3.s3.amazonaws.com
Date=Fri, 13 Feb 2009 01:19:28 GMT
Content-Length=146
Content-Type=application/xml
Request Body Data:
Body:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<LocationConstraint>EU</LocationConstraint>
</CreateBucketConfiguration>

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<LocationConstraint>EU</LocationConstraint>
</CreateBucketConfiguration>

While the output is as below:

Status : 200 OK
Headers:
null=HTTP/1.1 200 OK
x-amz-request-id=218387ACA024E346
Date=Fri, 13 Feb 2009 01:19:23 GMT
Content-Length=0
x-amz-id-2=BuzexE972zyJii/kPzUn0qhfUqPXDNmL2Myic0blVgr7b4Qc3s0vENGLklSuUzlS
Location=http://test.location3.s3.amazonaws.com/
Server=AmazonS3


While the list of buckets, as executed previous would be:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Owner>
<ID>b77136276767d460dc6ea34ff6cc338489d1a4c8d7db343b7f08054170f1fea4</ID>
<DisplayName>vyaapti</DisplayName>
</Owner>
<Buckets>
<Bucket>
<Name>test.location3</Name>
<CreationDate>2009-02-13T01:19:24.000Z</CreationDate>
</Bucket>
</Buckets>
</ListAllMyBucketsResult>

There is so much more to the above, adding ACL (Access Control Lists), creating objects etc.. I wish I get the time to research more on this soon, so that I can continue to this post. But for now I will use the available time to read the developer guide at http://docs.amazonwebservices.com/AmazonS3/2006-03-01/.

No comments: