如何使用c#获取Azure Blob存储容器中的现有目录列表?
我在Core .NET 2.2框架的顶部有一个使用C#
编写的控制台应用程序.
I have a console app written using C#
on the top of Core .NET 2.2 framework.
I am trying to the C# library to get a list of all directories inside my container. It is my understanding that Azure Blob Storage does not really have directories. Instead, it creates virtual names that the blobs look like a folder inside a container within browsers like Azure Blob Explorer
我使用以下代码存储文件
I store my files using the following code
CloudBlockBlob blockBlob = container.GetBlockBlobReference("foldername/filename.jpg");
await blockBlob.UploadFromStreamAsync(stream);
所以我想在我的容器内选择一个前缀又名文件夹名称的列表.
So I want to select a distinct list of the prefix aka folder names on inside my container.
因此,如果我有以下斑点"foldername1/file1.jpg","foldername1/file2.jpg","foldername1/file3.jpg"和"foldername2/file1.jpg".我想返回"foldername1","foldername2"
So if I have the following blobs "foldername1/file1.jpg", "foldername1/file2.jpg", "foldername1/file3.jpg", and "foldername2/file1.jpg". I want to return "foldername1", "foldername2"
如何从Azure Blob存储中获取不同前缀的列表?
How can I get a list of distinct prefixes from Azure Blob Storage?
已更新
我试图从下面的评论中获得反馈,所以我想出了以下代码
I tried to get the feedback from the comments below so I came up with the following code
public async Task<string[]> Directories(string path = null)
{
int index = path == null ? 0 : path.Split('/', StringSplitOptions.RemoveEmptyEntries).Length;
BlobContinuationToken token = null;
List<string> directories = new List<string>();
do
{
BlobResultSegment blobsListingResult = await ContainerFactory.Get().ListBlobsSegmentedAsync(path ?? string.Empty, true, BlobListingDetails.None, 5000, token, null, null);
token = blobsListingResult.ContinuationToken;
IEnumerable<IListBlobItem> blobsList = blobsListingResult.Results;
foreach (var item in blobsList)
{
var blobName = (item as CloudBlob).Name;
var blobParts = blobName.Split('/', StringSplitOptions.RemoveEmptyEntries);
if (blobParts.Length <= index)
{
// At this point, we know that this not a directory inside the provided path directory
continue;
}
directories.Add(blobParts[index]);
}
}
while (token != null);
return directories.Distinct().ToArray();
}
由于容器中有很多Blob,因此花费的时间太长,因为它几乎必须获取每个块才能获取目录列表.另外,这可能会非常昂贵,因为每次调用此方法时我都必须读取每个blob.
Since I have lots of blobs in the container, this takes way too long because it would have to almost get every single block in order to get a list of the directories. Additionally, this may be very costly since I have to read every blob every time this method is called.
如果一切都在本地运行,我基本上需要与运行Directory.GetDirectories(path)
相同的结果! 是否可以改善此功能?
I essentially need the same result that I would get as running Directory.GetDirectories(path)
if everything was running locally! Is there a way to improve this function?
也许您可以通过检查blob项的类型来改进您的解决方案?
Maybe you can improve your solution doing a check for the type of blob item?
var result = new List<string>();
var directory = _blobContainer.GetDirectoryReference(relativeFilePath);
if (directory.Equals(null))
return result;
var blobs = directory.ListBlobsSegmentedAsync(null).Result;
foreach (var item in blobs.Results)
{
if (item.GetType() == typeof(CloudBlobDirectory))
{
result.Add(item.Uri.Segments.Last().Trim('/'));
}
}
return result;
我没有太多文件夹,所以最好再次检查一下性能是否符合您的要求.
I didn't have too many folders so would be good to double check if the performance meets your requirements.