代码之家  ›  专栏  ›  技术社区  ›  HaBo

为Azure blob容器构造树对象

  •  -1
  • HaBo  · 技术社区  · 6 年前

    我想读取blob存储上的所有文件和文件夹,并将其显示在我的应用程序上(ASP.NET核心2.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;
                        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);
            }
    

    public class TreeNode
        {
            public int Id { get; set; }
    
            public int? ParentId { get; set; }
            public string Key { get; set; }
            public string Name { get; set; }
            public string Url { get; set; }
            public string Type { get; set; }
            public bool HasChildren { get; set; }
            public virtual List<TreeNode> Children { get; set; }
            public string ParentKey { get; set; }
        }
    

    任何帮助都将不胜感激。

    1 回复  |  直到 6 年前
        1
  •  1
  •   HaBo    6 年前

    我就是这样做的

    作用方式

     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;
            }
    
    推荐文章