gpt4 book ai didi

azure - 根据前缀获取azure blob结构的完整层次结构

转载 作者:行者123 更新时间:2023-12-03 02:22:04 26 4
gpt4 key购买 nike

我的 azure 存储容器中有以下层次结构:

Container  
-- Folder 1
-- Folder 2
-- Folder 2.1
-- File 1
-- File 2
-- File 3

我正在寻找的是一个通用函数,我可以在其中传递字符串,例如“container/Folder1/Folder2”,它返回给我层次结构,即

-- Folder 2.1  
-- File 1
-- File 2
-- File 3

我有以下代码,但在此我无法将前缀作为“container/Folder1/Folder2”传递。如果我在前缀字符串中添加“/”,则会抛出无效 uri 字符串的错误。

    static void printCloudDirectories(IEnumerable<IListBlobItem> blobList, Container cont)
{
foreach (var blobitem in blobList)
{
if (blobitem is CloudBlobDirectory)
{
var container = new Container();
var directory = blobitem as CloudBlobDirectory;
Console.WriteLine(directory.Prefix);
container.Name = directory.Prefix;
BlobContinuationToken token = null;
var directories = directory.ListBlobsSegmentedAsync(token).Result.Results;
printCloudDirectories(directories, container);
cont.Containers.Add(container);
}
else
{
cont.Children.Add(blobitem.Uri.AbsoluteUri);
}
}
}

public static void ListClientMethod(CloudBlobClient cloudBlobClient)
{
BlobContinuationToken token = null;
var containerSegments = cloudBlobClient.ListContainersSegmentedAsync(token).Result;
List<Container> containers = new List<Container>();
foreach (var container in containerSegments.Results)
{
Console.WriteLine("Container: " + container.Name);
var cont = new Container();
cont.Name = container.Name;
// ADD A CALL TO printCloudDirectories:
BlobContinuationToken token1 = null;
var blobs = container.ListBlobsSegmentedAsync(token1).Result.Results;
printCloudDirectories(blobs, cont);
containers.Add(cont);
}
}
public class Container
{
public Container()
{
Children = new List<string>();
Containers = new List<Container>();
}

public string Name { get; set; }

public List<string> Children { get; set; }

public List<Container> Containers { get; set; }
}

我使用 C# 作为编码语言

最佳答案

请使用 ListBlobsSegmentedAsync(String, Boolean, BlobListingDetails, Nullable<Int32>, BlobContinuationToken, BlobRequestOptions, OperationContext) 方法。

此方法的第一个参数是 Blob Prefix并且您需要指定 Folder 1/Folder 2/那里。

此方法的第二个参数是 useFlatBlobListing你需要通过true为此。

它应该返回如下结果:

Folder 1/Folder 2/Folder 2.1/File 1
Folder 1/Folder 2/Folder 2.1/File 2
Folder 1/Folder 2/Folder 2.1/File 3

您应该能够基于此构建所需的 TreeView 。

关于azure - 根据前缀获取azure blob结构的完整层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68452836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com