C#azure下载所有文件

时间:2015-06-08 10:19:14

标签: c# azure download storage blobs

我想从容器" $ logs"下载所有文件,但是会抛出StorageException

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);
}

有什么问题?

1 个答案:

答案 0 :(得分:2)

问题是item.ToString()将返回“Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob”,因此不会出现这样的blob,从而导致404错误。

将该行更改为

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

修改 在本地写入文件的代码也是错误的。 试试这个

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);
}