Wednesday, October 7, 2015

Azure - Blob storage – Working with file(s)

Introduction
Blob storage is an azure service to store large size/amount of data in unstructured way in form of binaries in the cloud. This opens up the whole world of files and all their extensions which can be uploaded in the blobs.
This post does not talk about the creation of blobs, connections but explains step by step in uploading the files, downloading the files and moving the files from one blob to another.

Upload file
The first step in the process of uploading a file to Blob is to create Objects of the CloudStorageAccount and the BlobClients in it.

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(UnityConfiguration.GetConfigurationValue("ConfigKey"));

// Create the blob client object from the storage account
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

You may want to add Retry policy to this blobClient which will retry to access the client in an exponential time if there is any problem encountered in the first try
blobClient.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(retryInterval), retryCount);

It is always a best practice to check if the Blob exists with a container, this way you can throw an exception that blob does not exists or create one with the container reference.

// Returns an reference for a given container of the blob
CloudBlobContainer blobContainer = blobClient.GetContainerReference(
UnityConfiguration.GetConfigurationValue("ContainerName"));

// Create container if it doesn’t exists
blobContainer.CreateIfNotExists();


Now that we have completed all the pre-requisites, the next step is to create reference of the CloudBlockBlob and then upload the file

// Create the reference of the block blob from the container
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(this.AddLocationInfo("Blobname"));

using (var inputStream = new MemoryStream())
{
     document.Save(inputStream); // document is a file object(could be xml, image, etc)
     inputStream.Flush();
     inputStream.Position = 0;

     blob.UploadFromStream(inputStream);
}

Note:
    a.  For XmlDocument document -> document.Save(inputStream)
    b.  For other type of files (.docx, .xlsx, etc) need to save into a stream so that it can be uploaded to blob.

Download file
Downloading a file is simple, you can download a file content into a Stream or as a string or even as a byte array.
The CloudBlockBlob object has method to download the file

// Download the file as memory stream
using (var blobStream = blockBlob.OpenRead())
{
      MemoryStream contentStream = new MemoryStream();
      blobStream.CopyTo(contentStream);
}

// Download the file as string
Encoding encoding;
blockBlob.DownloadText(encoding);

// Download the file into byte array
byte[] content;
using (var memoryStream = new MemoryStream())
{
     blockBlob.DownloadToStream(memoryStream);
     content = memoryStream.ToArray();
}


Moving file from one Blob to another
To move a file from one blob to another, simply use the inbuilt method of the CloudBlockBlob object.

CloudBlockBlob source;
CloudBlockBlob target;

using (var sourceStream = source.OpenRead())
{
     target.UploadFromStream(sourceStream);
}


Please leave your feedback and queries in the comments section. Thank you!