Avkash Chauhan's Blog

Windows Azure, Windows 8, Cloud Computing, Big Data and Hadoop: All together at one place.. One problem, One solution at One time...

Uploading a blob to Azure Storage with progress bar and variable upload block size

Uploading a blob to Azure Storage with progress bar and variable upload block size

  • Comments 2

Recently there were a few questions on uploading large blobs to Azure storage along with upload progress. I decided to write a sample and share with everyone so we all can take advantage of it. The sample is attached below. The sample is strictly shared for educational purposes.

The sample walkthrough is as below:

1. When you start the sample please enter the storage credentials i.e. storage name, storage key and container name. You can use "Connect" button to connect to storage. If container does not exist it will be created and if exist the list of blobs will be display in the datagrid. 

2. When you decided to upload a blob from local machine, please select the upload block size from the "Upload Block Size drop down list" and then select the file using "Select File" button. Once the file is select the button will change to "Upload". Select "Upload" to start uploading.

3. Once upload is started you will see upload progress in progress bar as well as status bar. This way you can check the upload progress in more granular level. Once upload is completed you will see the uploaded blob listed in the datagrid.

 

The full VS2010 project is attached below. However i decided to show the upload function for those who just want to see the upload code:

 

 private void backgroundWorkerUpload_DoWork(object sender, DoWorkEventArgs e)

        {

            string filePath = uploadFileName.FullName;

            blobName = uploadFileName.Name;

            if (storageConnected == false)

                ConnectStorage();

 

            blob = container.GetBlobReference(blobName).ToBlockBlob;

            FileStream fileStream = null;

            try

            {

                fileStream = File.Open(filePath, FileMode.Open);

                Console.WriteLine("File Size: {0}", fileStream.Length);

                totalFileSize = fileStream.Length;

                // Progressively read the file stream. Read [blockSize] bytes each time.

                int bytesRead = 0;

                totalBytesRead = 0;

                int i = 0;

                byte[] buffer = new byte[uploadBlockSize];

                // Keep a list of block IDs.

                List<string> blockIDList = new List<string>();

                // Read the first block.

                bytesRead = fileStream.Read(buffer, 0, uploadBlockSize);

                statusLableProgressInfo.Text = string.Format("Starting file upload now for {0}Bytes...", totalFileSize);

                while (bytesRead > 0)

                {

                    using (MemoryStream ms = new MemoryStream(buffer, 0, bytesRead))

                    {

                        char[] tempID = new char[6];

                        string iStr = i.ToString();

                        for (int j = tempID.Length - 1; j > tempID.Length - iStr.Length - 1; j--)

                        {

                            tempID[j] = iStr[tempID.Length - j - 1];

                        }

                        byte[] blockIDBeforeEncoding = Encoding.UTF8.GetBytes(tempID);

                        string blockID = Convert.ToBase64String(blockIDBeforeEncoding);

                        blockIDList.Add(blockID);

 

                        blob.PutBlock(blockID, ms, null);

                        Console.WriteLine("Block with ID " + blockID + " uploaded.");

                    }

                    totalBytesRead += bytesRead;

                    i++;

                    Console.WriteLine("Total Uploaded: {0}  Total Remaining {1}", totalBytesRead, (fileStream.Length - totalBytesRead));

                    double dIndex = (double)(totalBytesRead);

                    double dTotal = (double)fileStream.Length;

                    double dProgressPercentage = (dIndex / dTotal);

                    int iProgressPercentage = (int)(dProgressPercentage * 100);

                    backgroundWorkerUpload.ReportProgress(iProgressPercentage);

                    bytesRead = fileStream.Read(buffer, 0, uploadBlockSize);

 

                }

                blob.PutBlockList(blockIDList);

                Console.WriteLine("Block list uploaded.");

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

            finally

            {

                if (fileStream != null)

                {

                    fileStream.Close();

                }

            }

        }

The sample is shared for educational purposes. The information in this sample is provided "AS IS" with no warranties, and confers no rights. Inappropriate comments will be deleted at the authors discretion. All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

 

Attachment: http://avkashstore01.blob.core.windows.net/publicdata/TinyAzureStorageClient.zip
Leave a Comment
  • Please add 2 and 8 and type the answer here:
  • Post
  • Dear Avkash, great sample, but the download link does not work.

  • The download link is not working! :-(

Page 1 of 1 (2 items)