Is there a way to get file structure from azure blob?

别来无恙 提交于 2020-05-09 07:28:26

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/59714051/is-there-a-way-to-get-file-structure-from-azure-blob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!