我想读取Blob存储上的所有文件和文件夹,并将其显示在层次结构模型的应用程序(ASP.NET CORE 2.1)中。
这是我的操作方法
gen_js_api
TreeNode类:
public async Task<ActionResult> List()
{
CloudBlobContainer container = GetCloudBlobContainer();
//List<string> blobs = new List<string>();
BlobResultSegment resultSegment = container.ListBlobsSegmentedAsync(null).Result;
var tree = new List<TreeNode>();
int i = 1;
foreach (IListBlobItem item in resultSegment.Results)
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob) item;
//blobs.Add(blob.Name);
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob blob = (CloudPageBlob) item;
//blobs.Add(blob.Name);
}
else if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory dir = (CloudBlobDirectory) item;
var response = await dir.ListBlobsSegmentedAsync(true, BlobListingDetails.None, int.MaxValue, null,
null, null);
tree.Add(new TreeNode
{
Id = i,
Key = dir.Prefix,
Name = dir.Prefix.TrimEnd('/'),
Url = dir.Uri.ToString(),
//HasChildren = response.Results.Any(),
//Children = response.Results.Select(x => new TreeNode
//{
// Key = x.StorageUri.PrimaryUri.ToString(),
// Name = x.StorageUri.SecondaryUri.ToString(),
// Url = x.Uri.ToString(),
// HasChildren = false
//}).ToList()
});
foreach (var blobItem in response.Results)
{
tree.Add(GetNode(blobItem, i));
}
//blobs.Add(dir.Uri.ToString());
i++;
}
}
return View(tree);
}
如何递归读取blob项的详细信息并构建树
任何帮助将不胜感激。
答案 0 :(得分:1)
这就是我最终这样做的方式
动作方法
public async Task<ActionResult> List()
{
CloudBlobContainer container = GetCloudBlobContainer();
List<string> blobs = new List<string>();
BlobResultSegment resultSegment = container.ListBlobsSegmentedAsync(null).Result;
var tree = new List<TreeNode>();
int i = 1;
foreach (IListBlobItem item in resultSegment.Results)
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob) item;
blobs.Add(blob.Name);
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob blob = (CloudPageBlob) item;
blobs.Add(blob.Name);
}
else if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory dir = (CloudBlobDirectory) item;
tree.Add(new TreeNode
{
Id = i,
Type = TreeNodeType.Folder,
Key = dir.Prefix,
Name = dir.Prefix.TrimEnd('/'),
Url = dir.Uri.ToString()
});
i++;
}
}
foreach (var treeNode in tree)
{
if (treeNode.Type == TreeNodeType.Folder)
treeNode.Children = await GetBlobDirectoriesAsync(container, treeNode);
}
return View(tree);
}
递归树节点
private async Task<List<TreeNode>> GetBlobDirectoriesAsync(CloudBlobContainer container, TreeNode parentNode)
{
var directory = container.GetDirectoryReference(parentNode.Key);
var folders = await directory.ListBlobsSegmentedAsync(null);
var tree = new List<TreeNode>();
if (folders.Results.Any())
parentNode.HasChildren = true;
foreach (var folder in folders.Results.ToList())
{
if (folder is CloudBlobDirectory directoryItem)
{
tree.Add(new TreeNode
{
Id = 0,
Type = TreeNodeType.Folder,
Key = directoryItem.Prefix,
Name = directoryItem.Prefix.Replace(directoryItem.Parent.Prefix, "").TrimEnd('/'),
Url = directoryItem.Uri.ToString()
});
}
if (folder is CloudPageBlob pageItem)
{
tree.Add(new TreeNode
{
Id = 0,
Key = pageItem.Name,
Name = pageItem.Name,
Url = pageItem.Uri.ToString()
});
}
if (folder is CloudBlockBlob blockItem)
{
tree.Add(new TreeNode
{
Id = 0,
Type = TreeNodeType.File,
Key = blockItem.Name.Replace(blockItem.Parent.Prefix,""),
Name = blockItem.Name.Replace(blockItem.Parent.Prefix, ""),
Url = blockItem.Uri.ToString()
});
}
}
foreach (var treeNode in tree)
{
if(treeNode.Type == TreeNodeType.Folder)
treeNode.Children = await GetBlobDirectoriesAsync(container, treeNode);
}
return tree;
}