One of the great features that the Blobs Service in Azure provides, is the ability to create snapshots for a given blob.
In the following I will elaborate on how to create a snapshot and how to get all the snapshots for a given blob.
Needless to say that snapshot make the application more robust and has the ability to add “transaction” behavior when manipulating the blob data (point of failure return to base blob).
Another useful case could be creating a snapshot to a cloud drive (mounted as NTFS to a blob) and restoring/imaging when needed.
Creating a snapshot
First lets access the storage service
(Note that the code samples are using the Microsoft.WindowsAzure.StorageClient namespace)
string connectionString = String.Format("DefaultEndpointsProtocol=https;AccountName={0};
AccountKey={1}",storageInfo.StorageName, storageInfo.PrivateKey);
CloudStorageAccountaccount = CloudStorageAccount.Parse(connectionString);
blobClient = account.CreateCloudBlobClient();
Get a reference to blob
CloudBlob baseblob=blobClient.GetBlobReference("testcontainer/baseblob.txt");
Create a snapshot of the blob
CloudBlob snapshot = baseblob.CreateSnapshot();
Let verfy that we managed to create a snapshot using Cloud Storage Manger:
When creating the snapshot the blob returned holds a Snapshot property of type DateTime that can be used for future access.
DateTime timestamp = (DateTime)snapshot.Attributes.Snapshot;
I can use the timestamp to get a reference to the snapshot in the following way:
CloudBlob snapshot2 = new CloudBlob("testcontainer/baseblob.txt", timestamp, blobClient);
List all snapshots
Getting all the snapshot for given blob is a bit more tricky since a snapshot blob is referred as an ordinary blob in the blobs service.
The only hint is the parameter SnapshotTime in the blob that can indicate if it’s a regular blob or snapshot one.
So I will need to traverse all the blobs in the container and collect only the blobs that are typed as snapshots and has the URI to my base blob.
BlobRequestOptions options = new BlobRequestOptions()
{
BlobListingDetails = BlobListingDetails.Snapshots,
UseFlatBlobListing = true
};
IEnumerable<IListBlobItem> items = blobClient.GetContainerReference("testcontainer").ListBlobs(options);
items.Where(item=> ((CloudBlob)item).SnapshotTime.HasValue&& item.Uri.Equals(baseblob.Uri));
Point of interest
On of the cool feature Cloud Storage Manager offers is the ability to create and restore snapshot for an entire directory in the Azure blob service.
Loading...

