有没有办法从azure blob获取文件结构? [英] Is there a way to get file structure from azure blob?

查看:198
本文介绍了有没有办法从azure blob获取文件结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Azure的新手,正在我的.Net应用程序中玩弄Blob.

I'm new to Azure and playing around with blobs in my .Net application.

我希望能够获得包含文件夹,子文件夹和文件的结构.

I want to be able to get structure with folders, subfolders and files inside.

到目前为止,我已经找到了一种与父母一起从所有文件夹和子文件夹中获取文件的方法. 除了解析这些文件的父文件夹的Prefix之外,还有其他方法可以获取文件夹结构吗?

For now I've figured a way to get the files from all folders and subfolders altogether with parents. Is there any way to get folder structure some other way than parse Prefix of those files' parents?

文件结构如下:

root container
 -folder1
   -subfolder1
       -file
       -file
   -subfolder2
       -file
   -file
 -file

我已经尝试过了,但是它只给了我根目录下的文件夹,没有子文件夹:

I've tried this, but it only gives me folder in the root directory, no subfolders:

            //returns account, client and container
            var blobData = GetBlobDetails(blobConnectionString, rootContainerName); 

            var rootContainer = blobData.Container;
            var blobList =  rootContainer.ListBlobsSegmentedAsync(string.Empty, false, BlobListingDetails.None, int.MaxValue, null, null, null);

            return (from blob in blobList.Result
                    .Results
                    .OfType<CloudBlobDirectory>()
                select blob).ToList();

推荐答案

首先,如注释中所述:Blob存储不了解文件夹的概念.都是平面结构,并且您在下面看到的是前缀,是blob(= file)路径的全部.

First of all, as noted in the comments: Blob storage does not know the concept of folders. Is all a flat structure and what you see below as prefixes, is all part of the path of a blob (=file).

也就是说,您可以遍历前缀来复制行为:

That said, you can replicate the behavior by traversing the prefixes:

使用Azure.Storage.Blobs 12.2.0

using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System;
using System.Threading.Tasks;
using System.Linq;

namespace BlobLister
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // Get a connection string to our Azure Storage account.
            string connectionString = "*****";
            string containerName = "mycontainer";

            Console.WriteLine($"Recursivly listing blobs and virtual directories for container '{containerName}'");

            BlobContainerClient container = new BlobContainerClient(connectionString, containerName);
            await ListBlobsForPrefixRecursive(container, "", 0);
        }

        public static async Task ListBlobsForPrefixRecursive(BlobContainerClient container, string prefix, int level)
        {         
            string spaces = new string(' ', level);
            Console.WriteLine($"{spaces}- {prefix}");
            await foreach (Page<BlobHierarchyItem> page in container.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: "/").AsPages())
            {
                foreach (var blob in page.Values.Where(item => item.IsBlob).Select(item => item.Blob))
                {
                    Console.WriteLine($"{spaces} {blob.Name}");
                }
                var prefixes = page.Values.Where(item => item.IsPrefix).Select(item => item.Prefix);
                foreach (var p in prefixes)
                {
                    await ListBlobsForPrefixRecursive(container, p, level + 1);
                }
            }
        }
    }
}

这篇关于有没有办法从azure blob获取文件结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆