Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am downloading some blobitem from Azure Blob Storage & my code is
C#
public Response DownloadFilesFromAzure(List<string> fileNameList, string folderPath)
{
	response = new Response();
	try
	{       
		string azureConnectionStrig = "DefaultEndpointsProtocol=https;AccountName=" + storageConfig.AccountName + ";AccountKey=" + storageConfig.AccesssKey;
		storageAcc = CloudStorageAccount.Parse(azureConnectionStrig);
		blobClient = storageAcc.CreateCloudBlobClient();
		container = blobClient.GetContainerReference(storageConfig.AccountName); // BlobName : storageConfig.AccountName           
		
		foreach (var blobitem in fileNameList)
		{
			var blobItems = container.ListBlobs(blobitem).ToList();

			blockBlob = container.GetBlockBlobReference(blobItems[0].Uri.ToString());
			var fileName = blockBlob.Name.Substring(blockBlob.Name.LastIndexOf('/') + 1);
			
			blockBlob.BeginDownloadToFile(Path.Combine(folderPath, fileName), FileMode.OpenOrCreate, null, null);
			
			//Task task = Task.Run(()=> blockBlob.BeginDownloadToFile(Path.Combine(folderPath, fileName), FileMode.OpenOrCreate, null, null));
			//task.Wait();                    
		}

		// here my method to zip & move file
		ZipAndMove();
		
		response.ErrorCode = ErrorCode.Success;
		return response;

	}
	catch (Exception e)
	{
		Logging.Log(e.Message, 20, true);
		throw e;
	}           
}


the Problem is method ZipAndMove is called before download complete.
I also use the commented code i.e
C#
//Task task = Task.Run(()=> blockBlob.BeginDownloadToFile(Path.Combine(folderPath, fileName), FileMode.OpenOrCreate, null, null));
//task.Wait();


but the problem remain.

Please help to solve this problem.
Posted

1 solution

You are using a method (BeginDownloadToFile) that designed to be asynchronous...
Use AsyncCallback[^] param to know when download finished or use DownloadToFile[^] to download synchronously...
 
Share this answer
 
Comments
Vi(ky 7-Oct-14 7:49am    
Thank you so much :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900