Extensions to the .NET Windows Azure Storage Library: Add Metrics, Logging, Leasing to Windows Azure Storage

Steve Marx, a Microsoft Technical Strategist for Windows Azure, has put together a set of smarx.WazStorageExtensions NuGet package and a corresponding GitHub repository that shows how you can add support the new storage analytics API."It uses smarx.WazStorageExtensions, a collection of useful extension methods for Windows Azure storage operations that aren't covered by the .NET client […]

Steve Marx, a Microsoft Technical Strategist for Windows Azure, has put together a set of smarx.WazStorageExtensions NuGet package and a corresponding GitHub repository that shows how you can add support the new storage analytics API.

"It uses smarx.WazStorageExtensions, a collection of useful extension methods for Windows Azure storage operations that aren't covered by the .NET client library," informed Marx.

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

The examples in his post show how with a few lines of code you can:

  • enable metrics with a 7-day retention policy
  • try to lease a blob and write "Hello, World!" to it
  • check the existence of a blob

Download:

  • WazStorageExtensions on GitHub
  • smarx.WazStorageExtensions on NuGet

Code sample:

// 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");
}