Wednesday, May 12, 2010

Another walk in the cloud with Amazon Web Services

Talking a look at some of the offering that Amazon Cloud Services has I decided to take a look at Amazon Simple Storage Service or simply Amazon S3. 

Like I wrote in “A walk in the Cloud” the Amazon cloud can be accessed though a web service so you have the option of rolling your own tools or using their API lib.  The has SDK has defiantly improved and the install process is a lot easier now.  Before you had to download the source, build the project, etc, now all you have to do is download AWSSDKForNET and install it.  This adds the Clint to your GAC, and add templates into Visual Studio, if you still want to see what they are doing under the hood you can still download the source. 

Amazon may not provide you with any tools but there are a few free ones that work fairly well.  CloudBerry Explorer is a fairly solid tool for managing your S3 storage, it looks kind of like an ftp client and it lets you manage multiple accounts, create buckets, upload/download content, generate web URLs(http and https), search, etc. Another client is the Firefox plug-in S3Fox, basically the same as CloudBerry but it runs in Firefox.

Pulling data from S3 is relatively simple, this is a code sample for pulling your list of buckets, then building a list of links to the images in each bucket
   1: AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(
   2:     ConfigurationManager.AppSettings["AWSAccessKey"],
   3:     ConfigurationManager.AppSettings["AWSSecretKey"]);
   4:  
   5: ListBucketsResponse response = s3Client.ListBuckets();
   6:    
   7: foreach (S3Bucket bucket in response.Buckets)
   8: {
   9:     HtmlGenericControl header = new HtmlGenericControl("div")
  10:     {
  11:         InnerText = bucket.BucketName
  12:     };
  13:  
  14:     DivlistOfImages.Controls.Add(header);
  15:  
  16:     ListObjectsRequest request = new ListObjectsRequest
  17:      {
  18:          BucketName = bucket.BucketName
  19:      };
  20:     ListObjectsResponse imageList = s3Client.ListObjects(request);
  21:  
  22:     foreach (S3Object s3Object in imageList.S3Objects)
  23:     {
  24:         HtmlAnchor imageLink = new HtmlAnchor
  25:        {
  26:            InnerText = s3Object.Key
  27:        };
  28:         string bucketName = bucket.BucketName;
  29:         string objectKey = s3Object.Key;
  30:         GetPreSignedUrlRequest preSignedUrlRequest = new GetPreSignedUrlRequest
  31:          {
  32:              BucketName = bucketName,
  33:              Key = objectKey,
  34:              Protocol = Protocol.HTTP,
  35:              Expires = DateTime.Now.AddDays(7)
  36:          };
  37:  
  38:  
  39:         imageLink.HRef = s3Client.GetPreSignedURL(preSignedUrlRequest);
  40:         DivlistOfImages.Controls.Add(imageLink);
  41:     }
  42: }
it’s that simple.

With this you can have your content be managed by another department with a tool like CloudBerry and remove the work of updating images and other forms of content. 

As always here is a link to the sample code project

1 comment:

Anonymous said...

If you need Windows client for Amazon S3, you can use S3 Browser.

It is lightweight, clean and easy to use.