当前位置:   article > 正文

azure 读取文件的目录怎么_如何使用c#获取Azure Blob存储容器中的现有目录列表?...

azure storage directory.getdirectories

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.

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"

How can I get a list of distinct prefixes from Azure Blob Storage?

Updated

I tried to get the feedback from the comments below so I came up with the following code

public async Task Directories(string path = null)

{

int index = path == null ? 0 : path.Split('/', StringSplitOptions.RemoveEmptyEntries).Length;

BlobContinuationToken token = null;

List directories = new List();

do

{

BlobResultSegment blobsListingResult = await ContainerFactory.Get().ListBlobsSegmentedAsync(path ?? string.Empty, true, BlobListingDetails.None, 5000, token, null, null);

token = blobsListingResult.ContinuationToken;

IEnumerable 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();

}

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.

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?

解决方案

Maybe you can improve your solution doing a check for the type of blob item?

var result = new List();

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.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/138221
推荐阅读
相关标签
  

闽ICP备14008679号