Fri, 02 Sep 2011

Analytics, Leasing, and More: Extensions to the .NET Windows Azure Storage Library

I’ve just published the smarx.WazStorageExtensions NuGet package and a corresponding GitHub repository. Some of it is just a repackaging (with minor improvements) of code I’ve published on this blog before, but I’ve also added some support for the new storage analytics API, based loosely on the code from the Windows Azure storage team (logging and metrics).

From the GitHub README:

smarx.WazStorageExtensions is a collection of useful extension methods for Windows Azure storage operations that aren't covered by the .NET client library.

It can be install using the NuGet package via install-package smarx.WazStorageExtensions and contains extension methods and classes for the following:

Here’s some sample code to get you started with the library:

// enable metrics with a 7-day retention policy
static void Main(string[] args)
{
    var blobs = CloudStorageAccount.Parse(args[0]).CreateCloudBlobClient();
    var props = blobs.GetServiceProperties();
    props.Metrics.Enabled = true;
    props.Metrics.RetentionPolicy = new RetentionPolicy { Enabled = true, Days = 7 };
    blobs.SetServiceProperties(props);
}

// try to lease a blob and write "Hello, World!" to it
static void Main(string[] args)
{
    var blob = CloudStorageAccount.Parse(args[0]).CreateCloudBlobClient().GetBlobReference(args[1]);
    var leaseId = blob.TryAcquireLease();
    if (leaseId != null)
    {
        blob.UploadText("Hello, World!", leaseId);
        blob.ReleaseLease(leaseId);
        Console.WriteLine("Blob written!");
    }
    else
    {
        Console.WriteLine("Blob could not be leased.");
    }
}

// check the existence of a blob
static void Main(string[] args)
{
    var blob = CloudStorageAccount.Parse(args[0]).CreateCloudBlobClient().GetBlobReference(args[1]);
    Console.WriteLine("The blob {0}.", blob.Exists() ? "exists" : "doesn't exist");
}

I hope you find these useful. Let me know if you have any feedback! Those links again are: