根据前缀获取 azure blob 结构的完整层次结构
Get complete hierarchy of azure blob structure based on prefix
我的 Azure 存储容器中有以下层次结构:
Container
-- Folder 1
-- Folder 2
-- Folder 2.1
-- File 1
-- File 2
-- File 3
我正在寻找的是一个通用函数,我可以在其中传递字符串,例如“container/Folder1/Folder2”,它 return 我的层次结构,即
-- 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#作为编码语言
此方法的第一个参数是 Blob Prefix
,您需要在此处指定 Folder 1/Folder 2/
。
此方法的第二个参数是 useFlatBlobListing
,您需要为此传递 true
。
它应该 return 你得到这样的结果:
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
你应该能够基于此构建所需的树视图。
我的 Azure 存储容器中有以下层次结构:
Container
-- Folder 1
-- Folder 2
-- Folder 2.1
-- File 1
-- File 2
-- File 3
我正在寻找的是一个通用函数,我可以在其中传递字符串,例如“container/Folder1/Folder2”,它 return 我的层次结构,即
-- 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#作为编码语言
此方法的第一个参数是 Blob Prefix
,您需要在此处指定 Folder 1/Folder 2/
。
此方法的第二个参数是 useFlatBlobListing
,您需要为此传递 true
。
它应该 return 你得到这样的结果:
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
你应该能够基于此构建所需的树视图。