New: Windows Azure Storage Helper for WebMatrix

Hot on the heels of the OData Helper for WebMatrix today we are pushing out a new helper into the community.  The Windows Azure Storage Helper makes it ridiculously easy to use Windows Azure Storage (both blob and table) when building your apps.  If you’re not familiar with “cloud storage” I would recommend you take a look at this video where my pal Ryan explains what it’s all about.  In a nutshell, it provides infinitely simple yet scalable storage which is great if you are a website with lots of user generated content and you need your storage to grow auto-magically with the success of your apps.  Tables aren’t your normal relational databases – but they are great for simple data structures and they are super fast.

You can download the helper from the Codeplex website.

Get started in 60 seconds
  1. If you haven’t already got one, sign up for a Windows Azure account
  2. In the _start.cshtml file setup the helper for use with your account by adding this code:

    1. @using Microsoft.Samples.WebPages.Helpers
    2. @{
    3.     WindowsAzureStorage.AccountName = "youraccountname";
    4.     WindowsAzureStorage.AccountKey = "youraccountkey";
    5. }

  3. Download the latest Windows Azure Storage Helper Binaries
  4. In your WebMatrix project, create a folder named "bin" off the root
  5. Copy the Windows Azure Storage Helper Binaries there
  6. Check out the documentation
  7. Start using Windows Azure Storage Tables and Blobs from WebMatrix with the Helper!

There are loads of use cases out there for blob and table storage.  You just need to look at of the popular startups like Facebook, Twitter, YFrog, Foursquare, Dropbox who all use this type of storage to gain great scale and performance.

How to use the helper

With the helper we’ve provided a bunch of methods that make common operations really easy so that you can that in your WebMatrix apps.  Here are some of my favorite examples:

1. Getting a row from a table – notice how we can just dump the result into a WebGrid and have it render our table for us.

  1. @using Microsoft.Samples.WebPages.Helpers
  2. @{
  3.     var rows = WindowsAzureStorage.GetRows("JamesTable");
  4.     var grid = new WebGrid(rows);
  5. }

2. Updating a row in a table – first we get the individual row, making sure to pass it the partition and table name.  Next we make a change to an item, then update the table.

  1. @using Microsoft.Samples.WebPages.Helpers
  2. @{
  3.     var row = WindowsAzureStorage.GetRow("JamesTable", "partition1", "row1");
  4.     row.Name = "James Senior";
  5.     WindowsAzureStorage.UpdateRow("JamesTable", row);
  6. }

3. Uploading a file into blob storage – I like to use this in combination with the FileUpload helper that we ship with WebMatrix.  You can pass a stream, binary file or text string into the helper method and it’ll be placed in the blob address you specify.

  1. @using Microsoft.Samples.WebPages.Helpers
  2. @{
  3.     var uploadedFile = Request.Files[0];
  4.     WindowsAzureStorage.UploadBinaryToBlob("MyContainer/" + uploadedFile.FileName, uploadedFile.InputStream);
  5. }

4. To grab the blob, use one of the download methods like DownloadBlobAsText or DownloadBlobAsText.

@using Microsoft.Samples.WebPages.Helpers
@{
   // reference to my blob
   var blobRef = "MyContainer/foo.txt";

   // gets the blob as text
   var astring = WindowsAzureStorage.DownloadBlobAsText(blobRef);
  
   // gets the blob as a byte array and puts it in the
   var bytes = WindowsAzureStorage.DownloadBlobAsByteArray(blobRef);
 
   // Send the response so the user can download the file
   Response.ClearContent();
   Response.AppendHeader("Content-Type", "application/octet-stream");
   Response.AppendHeader("Content-Disposition:", String.Format("attachment; filename={0}", HttpUtility.UrlPathEncode(blobRef)));
   Response.BinaryWrite(bytes);
   Response.End();
}

Here’s a video where I walk through how to do all this good stuff in detail:

Get Microsoft Silverlight

 

Future work

There’s some more features I’d like to add to the next version of this helper. I’ve started a discussion here about that – join the conversation if you have any ideas!  Here are the current ideas:

  • Error Handling (Windows Azure Tables)
  • Pagination and continuation tokens (Windows Azure Tables)
  • Lease/Snapshot/Copy Blob operations (Windows Azure Blobs)
  • Sharing Blobs (Windows Azure Blobs)
  • Delimiters (Windows Azure Blobs)
  • Client-side ordering (Windows Azure Tables)

Tags: