AyhanGzr
AyhanGzr

Reputation: 33

Azure Blob Storage Download All Files

I want to download all Files from the Container "$logs", but a StorageException is thrown.

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=" + accName + ";AccountKey=" + accKey);

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

CloudBlobContainer container = blobClient.GetContainerReference("$logs");

IEnumerable<IListBlobItem> ListBlobs = container.ListBlobs(string.Empty, true);

foreach (var item in ListBlobs)
{
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(item.ToString());
    string path= (@"C:\Users\Ay\Downloads\AzureLogs");
    blockBlob.DownloadToFile(path, FileMode.Open);
}

What is the problem?

Upvotes: 2

Views: 10325

Answers (1)

Alex S
Alex S

Reputation: 1594

The problem is that item.ToString() will return "Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob" and therefore no such blob exists resulting in a 404 error.

Change that line to

CloudBlockBlob blockBlob = container.GetBlockBlobReference(((CloudBlockBlob)item).Name);

Edit: The code to write the file locally is incorrect as well. Try this

foreach (var item in ListBlobs)
{
    string name = ((CloudBlockBlob) item).Name;
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
    string path = (@"C:\Users\Ay\Downloads\AzureLogs\" + name);
    blockBlob.DownloadToFile(path, FileMode.OpenOrCreate);
}

Upvotes: 3

Related Questions