Azure Blob存储下载所有文件
本文关键字:文件 下载 Blob 存储 Azure | 更新日期: 2023-09-27 17:54:21
我想从容器"$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()
将返回"Microsoft.WindowsAzure.Storage.Blob "。,因此不存在这样的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);
}