How to download my documents from Azure Blob?

I have been in the position where I’ve been storing data on blob. However, there was a request to export all data in-house so that the same can be given to the third party.

The document ID, which is stored in the SQL database, is given as blob name.

I have managed to achieve this through the following piece of code:

storageConnectionString = “DefaultEndpointsProtocol=https;AccountName=<azure blob name >;AccountKey=<your private Key to blob>”

Settings a = new Settings();

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(a.StorageConnectionString);

using (SqlConnection conn = new SqlConnection(a.ConnectionString))
{
string sqlCommandText = @”select documentID, documentName from documentTable”;

SqlCommand command = new SqlCommand(sqlCommandText, conn);
conn.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(“tra”);

while (reader.Read())
{

int documentID;
if (int.TryParse((reader[“DocID”] ?? “”).ToString(), out documentID))
{

string documentName = reader[“DocName”].ToString();

int subfolderID = documentID / 1000 * 1000;
var blobreference = string.Format(“docsearch/{0}/{1}”, subfolderID, documentID);
// Retrieve reference to a blob named “photo1.jpg”.
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobreference);

if (File.Exists(string.Format(@”C:TRAResume{0}”, documentName)))
{

}
else
{
// Save blob contents to a file.
using (var fileStream = System.IO.File.OpenWrite(string.Format(@”C:TRAResume{0}”, documentName)))
{

if (Program.Exists(blockBlob))
{
blockBlob.DownloadToStream(fileStream);
}
}
}
}

}
}
}
}

public static bool Exists(CloudBlockBlob blob)
{
try
{
blob.FetchAttributes();
return true;
}
catch
{
return false;

}
}