Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...