Azure Blob 存储 下载所有文件

Azure Blob Storage Download All Files

我想从容器“$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);
}

有什么问题?

问题是 item.ToString() 将 return "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);
}