Conditional Reading of Windows Azure BLOB

Conditional reading of BLOB implies, you want to read BLOB only when BLOB is modified. There are scenarios when you need to read BLOB frequently. In that case to avoid network usage and bandwidth, you may consider reading BLOB only when BLOB has been modified since last read or download. This type of BLOB reading is termed as Conditional BLOB reading.

Usually to read or download BLOB, you create reference of BLOB as below

image

In above code snippet

  1. ConnectionString is connection string to your storage account.
  2. Containername is name of the public container.
  3. Doc1.docx is name of the BLOB.

If you want to read it without any condition or in other words you want to read the BLOB regardless of whether it has been modified or not then you can read it in byte array as below,

image

As of now everything is fine for unconditional read but if you want to do Conditional read on the BLOB then you will have to use ,

  • IfNoneMatch
  •  eTag

IfNoneMatch is a static variable of AccessCondition structure. When you download a BLOB you can additionally provide BLOBRequestOptions. Almost all Download functions are overloaded with BlobRequestOptions.

image

If you don’t specify BLOBRequestOptions then you will perform unconditional read.

To perform conditional read, you need to follow below steps,

Step 1

Save ETag value from the server at time of first reading in a local variable

clip_image001

Step 2

Create instance of BLOBRequestOptions with setting AccessCondition as below,

clip_image001[6]

In above code we are passing locally saved ETag value to IfNoneMatch

 

Step 3

At time of making second call onward to download BLOB pass Request Option as below,

clip_image002

If BLOB is not modified then you will get into the Exception

At time of downloading BLOB in above code, server will fist check for the ETatg in IfNoneMatch header. If server don’t find new version, it will send HTTP 304 request to the storage library.

For your reference full source code of conditional BLOB reading is as below,


using System;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{

CloudStorageAccount account = CloudStorageAccount.
Parse("connectionString");

CloudBlobClient BlobClient = account.CreateCloudBlobClient();

CloudBlobContainer ContainerReference = BlobClient.
GetContainerReference
("dhananjay");

CloudBlob BlobReference = ContainerReference.
GetBlobReference
("Doc1.docx");

byte[] DownloadAsByteArray = BlobReference.DownloadByteArray();

var LastReadETag = BlobReference.Properties.ETag;

BlobRequestOptions RequestOption = new BlobRequestOptions
{ AccessCondition = AccessCondition.IfNoneMatch
(LastReadETag)
};

try
{
byte[] DownloadByteArraySecondTimeOnward = BlobReference.
DownloadByteArray(RequestOption);

}
catch (StorageClientException ex)
{
if (ex.StatusCode == System.Net.HttpStatusCode.NotModified)
{

}
}

}
}

}

 

In this way you can perform conditional reading on Windows Azure BLOB. I hope this post is useful. Thanks for reading  Smile

 

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

One response to “Conditional Reading of Windows Azure BLOB”

Leave a comment

Create a website or blog at WordPress.com