Wednesday, 22 August 2012

Upload File to the Azure blob in chunks using Parallel Programming



Below written code describes how to upload a file to the blob in chunks. In below code i tried to write comment for each line of code.
/// <summary>
/// Upload file to the blob parallely.
/// </summary>
/// <param name="blobName">blob url</param>
/// <param name="uploadFileInfo">File Info</param>
/// <param name="chunkSizeInKB">Chunk Size in KB</param>
/// <returns></returns>
public static bool ParallelFileUploadToBlob(string blobName, FileInfo uploadFileInfo, int chunkSizeInKB)
{
    List<String> blockList = new List<String>();
    CloudBlockBlob UploadableBlob;

    try
    {
        //get the blob reference
        UploadableBlob = blobClient.GetBlockBlobReference(blobName);

        //read the contents of the file into byte array
        var dataToUpload = File.ReadAllBytes(uploadFileInfo.FullName);

        // if file byte is empty return false
        if (dataToUpload.Length == 0)
        {
            return false;
        }

        //creating block size in MB. Ex: 1024*1024=1MB
        Int32 blockLength = 1024 * Convert.ToInt32(chunkSizeInKB);

        //calculate number of chunks based on chunk size.
        var numberOfBlocks = ((int)dataToUpload.Length / blockLength) + 1;
        string[] blockIds = new string[numberOfBlocks];

        //Using Parallel programming to upload chunk datas of file
        Parallel.For(0, numberOfBlocks, x =>
        {
            var blockId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());

            //calculating size of first chunk
            var currentLength = Math.Min(blockLength, dataToUpload.Length - (x * blockLength));

            //creating memory stream of the chunk of file and upload it to the blob.
            using (var memStream = new MemoryStream(dataToUpload, x * blockLength, currentLength))
            {
                retryPolicy.ExecuteAction(() =>
                {
                    //committing a block as a part of blob
                    UploadableBlob.PutBlock(blockId, memStream, null);
                });
            }
            blockIds[x] = blockId;
        });

        retryPolicy.ExecuteAction(() =>
        {
            //committing all blocks of a blob
            UploadableBlob.PutBlockList(blockIds);
        });

        return true;
    }
    catch (Exception ex)
    {
        //Handle Error.
        return false;
    }
}

No comments:

Post a Comment