Conditional reading of a BLOB implies, you want to read the BLOB only when the BLOB is
modified. There are scenarios when you need to read a BLOB frequently. In that
case to avoid network usage and bandwidth, you may consider reading the BLOB only
when the 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 a BLOB, you create a reference to a BLOB as below
In the above code snippet
- ConnectionString is connection string to your storage account.
- Containername is name of the public container.
- 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,
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.
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 the ETag value from the server at time of first reading in a local variable
Step 2
Create an instance of BLOBRequestOptions with setting AccessCondition as below,
In the above code we are passing locally saved ETag value to IfNoneMatch
Step 3
At the time of making second call onward to download BLOB pass the Request Option as
below,
If BLOB is not modified then you will get into the Exception
At the time of downloading BLOB in above code, the 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, the full source code of conditional BLOB reading is 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.