Tuesday, September 29, 2015

Saving file from Azure Storage Blob onto a hard drive using C#

Recently started working with Azure storage blob. So far I am really enjoying it so I wanted to share some code snippet I have wrote so far for various purposes. The one below I have used to save the file from the blob onto my hard drive.
//reading blob file data into a file on your hard drive
string filename ="mytargetfile.txt"
string blobConn = CloudConfigurationManager.GetSetting("BlobConn");
CloudStorageAccount storageAcct = CloudStorageAccount.Parse(blobConn);
CloudBlobClient blobClient = storageAcct.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference("container1");
if (blobContainer.Exists(null, null))
{
     CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(filename);
     if (blockBlob.Exists(null, null))
    {
          string tempFile = Path.Combine(@"C:\BlobFiles",filename);
          blockBlob.DownloadToFile(tempFile, FileMode.Create);
    }
}

No comments:

Post a Comment