![](/img/trans.png)
[英].NET Azure Blob Storage: Get Root-Level Directories w/o Listing All Blobs
[英]Azure issues listing blobs
我试图列出并从 azure blob 存储中获取容器中的 blob,以便能够下载文件,并通过我的 API 发送结果。 我已经按照微软在这里的说明进行操作: https : //docs.microsoft.com/en-us/azure/storage/blobs/storage-blobs-list? tabs =dotnet
我的 blob 的文件结构是 {Guid}/images/{Guid}.jpg,其中 {Guid} 是带有破折号的 Guid。
但是,这两种方法实际上都不起作用。 我已经尝试过使用平面列表(鉴于我有分层嵌套,这将不起作用):
var resultSegment = containerClient.GetBlobsAsync().AsPages();
await foreach(var blobPage in resultSegment)
{
foreach(var blobItem in blobPage.Values)
{
using(var stream = new MemoryStream())
{
var combinedName = Path.Combine("images/", blobItem.Name);
var blobClient = containerClient.GetBlobClient(combinedName);
await blobClient.DownloadToAsync(stream);
result.Add(
new AzureBlobResponse
{
Data = stream.ToArray(),
Filename = blobItem.Name
});
}
}
}
仅针对输入字符串抛出“System.FormatException”。 然后我尝试修改我的代码如下:
private async Task NavigateBlobHierarchy(List<AzureBlobResponse> result, string? prefix)
{
var resultSegment = containerClient.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: "/")
.AsPages();
await foreach(var blobPage in resultSegment)
{
foreach(var blobItem in blobPage.Values)
{
if(blobItem.IsPrefix)
{
await NavigateBlobHierarchy(result, blobItem.Prefix.Replace('/', ' ').Trim());
} else
{
using(var stream = new MemoryStream())
{
var combinedName = Path.Combine("images/", blobItem.Blob.Name);
var blobClient = containerClient.GetBlobClient(combinedName);
await blobClient.DownloadToAsync(stream);
result.Add(
new AzureBlobResponse
{
Data = stream.ToArray(),
Filename = blobItem.Blob.Name
});
}
}
}
}
}
我这样称呼它的地方:
try
{
var listOfBlobs = new List<AzureBlobResponse>();
await NavigateBlobHierarchy(listOfBlobs, "images");
return listOfBlobs;
} catch (Exception e)
{
throw;
}
这只是以无限循环结束,因为它只是挂在了图像前缀上,而且它总是命中的一切都是图像。 我到底做错了什么,这不应该是一个需要解决的复杂问题。 我在旧地方的项目中使用 Azure blob 的旧 API 之前已经这样做了,从来没有这种奇怪的情况。
尝试使用此代码。我在我的系统中尝试过,能够列出 blob 中的所有文件并在本地下载它们。
BlobServiceClient blobServiceClient = new BlobServiceClient("Connection string");
// Get the container client object
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("test");
// 1st method to List all blobs in the container
foreach (BlobItem blobItem in containerClient.GetBlobs())
{
Console.WriteLine("\t" + blobItem.Name);
}
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("ConnectionString");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("test");
//2nd method to List all the files
var blobList = container.ListBlobs(useFlatBlobListing: true);
foreach (var blob in blobList)
{
Console.WriteLine("\t" + blob.Uri);
string name = ((CloudBlockBlob)blob).Name;
CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
string path = (@"local path");
string[] names = name.Split("/");
string newPath = "";
string fileName = "";
for (int i = 0; i < names.Length; i++)
{
if (i != (names.Length - 1))
{
path = path +names[i]+"\\";
newPath = newPath + "\\" + names[i];
}
fileName = names[(names.Length - 1)];
}
string filePath = path + fileName;
if (Directory.Exists(path))
{
blockBlob.DownloadToFile(filePath, FileMode.OpenOrCreate);
}
//blockBlob.DownloadToFile(path, FileMode.OpenOrCreate);
else
{
Directory.CreateDirectory(path);
string filePath1 = path +fileName;
blockBlob.DownloadToFile(filePath1, FileMode.OpenOrCreate);
}
}
输出
容器中的文件(在 azure blob 存储中)
列出的文件
本地文件保存
我有一个文件夹名称包含名为 my learning inside images 的文件,如图所示
与本地保存的文件结构相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.