Tuesday, April 17, 2012

Windows Azure Blob - Unable to read data from the transport connection: The connection was closed

I had file of 1.5GB uploaded on Azure Blob Storage. My supervisor asked me to write a simple console application to download this huge file of 1.5 GB from Blob storage and send to the client. As my client don’t wanted to have complexities involved in using Windows Azure Cloud Storage Explorer (he never wants any type of complexities other than Notepad... anyways) I started with Console Application with simple code to download the blob.
I added the reference of Microsoft.WindowsAzure.ServiceRuntime.dll and Microsoft.WindowsAzure.StorageClient.dll in my application. Then wrote following code in Main method –
CloudBlobClient blobClient = new CloudBlobClient("https://myStorageService.blob.core.windows.net/");
           
CloudBlob blob = blobClient.GetBlobReference("mycontainer/job2/part-00000");

try
{
            blob.DownloadToFile(AppDomain.CurrentDomain.BaseDirectory + "HeatMapperInput.txt", options);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("=------------------------------------" + ex.StackTrace);
Console.ReadLine();
}

I ran the console application. 28MB out of 1.5 GB of file was downloaded and suddently received following error –
“Unable to read data from the transport connection: The connection was closed.”
It was really strange and I had received this error for the first time. After digging in Blob Storage I found this –
“The default timeout interval is 30 seconds for REST calls to the Blob service. The Blob service reduces larger timeouts to 30 seconds. By default, storage clients specify a 90 second timeout with most Blob service calls. The Blob service will return an error if it cannot prepare a proper response within 30 seconds and the storage client will wait 90 seconds to throw an exception if it receives no response.
The timeout interval can be set for blob request using – BlobRequestOptions.”
So my final code of downloading blob looks as below –
CloudBlobClient blobClient = new CloudBlobClient("https://myStorageService.blob.core.windows.net/");
           
CloudBlob blob = blobClient.GetBlobReference("mycontainer/job2/part-00000");

BlobRequestOptions options = new BlobRequestOptions();           
options.Timeout = new TimeSpan(0, 180, 0);

try
{
            blob.DownloadToFile(AppDomain.CurrentDomain.BaseDirectory + "HeatMapperInput.txt", options);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("=------------------------------------" + ex.StackTrace);
Console.ReadLine();
}
That’s it!! My whole file of 1.5GB got downloaded successfully and no error was received.
Cheers…
Happy Blobbing!!!

6 comments:

  1. It's always interesting to learn how other people employ Azure Blob Storage. You can also check out CloudBerry Explorer freeware that helps to manage it http://www.cloudberrylab.com/free-microsoft-azure-explorer.aspx
    Girlincloud,
    CloudBerry lab team

    ReplyDelete
  2. thanks for the post...it help me in my project error

    ReplyDelete
  3. Thank you, this is exactly what I needed.

    ReplyDelete
  4. Thanks! I broke my brain before I've read your post

    ReplyDelete
  5. Oh man! Thank, thank you for the post!

    ReplyDelete
  6. Good Post. Really Helped & Fixed the above issue.
    Thanks. Keep Posting.

    ReplyDelete