Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

.net - Is there a way to get file structure from azure 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.

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?

File structure is the following:

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

Using 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);
                }
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...