简体   繁体   中英

Get complete hierarchy of azure blob structure based on prefix

I have following hierarchy in my azure storage container :

Container  
-- Folder 1  
  -- Folder 2  
     -- Folder 2.1  
       -- File 1  
       -- File 2  
       -- File 3 

What I'm searching for is a generic function where I can pass string eg "container/Folder1/Folder2" and it return me the hierarchy ie

-- Folder 2.1  
   -- File 1  
   -- File 2  
   -- File 3  

I have following code in place but in this I'm not able to pass the prefix as "container/Folder1/Folder2". If I add "/" in prefix string then it throws error that invalid uri string.

    static void printCloudDirectories(IEnumerable<IListBlobItem> blobList, Container cont)
    {
        foreach (var blobitem in blobList)
        {
            if (blobitem is CloudBlobDirectory)
            {
                var container = new Container();
                var directory = blobitem as CloudBlobDirectory;
                Console.WriteLine(directory.Prefix);
                container.Name = directory.Prefix;
                BlobContinuationToken token = null;
                var directories = directory.ListBlobsSegmentedAsync(token).Result.Results;
                printCloudDirectories(directories, container);
                cont.Containers.Add(container);
            }
            else
            {
                cont.Children.Add(blobitem.Uri.AbsoluteUri);
            }
        }
    }

    public static void ListClientMethod(CloudBlobClient cloudBlobClient)
    {
        BlobContinuationToken token = null;
        var containerSegments = cloudBlobClient.ListContainersSegmentedAsync(token).Result;
        List<Container> containers = new List<Container>();
        foreach (var container in containerSegments.Results)
        {
            Console.WriteLine("Container: " + container.Name);
            var cont = new Container();
            cont.Name = container.Name;
            // ADD A CALL TO printCloudDirectories:
            BlobContinuationToken token1 = null;
            var blobs = container.ListBlobsSegmentedAsync(token1).Result.Results;
            printCloudDirectories(blobs, cont);
            containers.Add(cont);
        }
    }
    public class Container
{
    public Container()
    {
        Children = new List<string>();
        Containers = new List<Container>();
    }

    public string Name { get; set; }

    public List<string> Children { get; set; }

    public List<Container> Containers { get; set; }
}

I use c# as coding language

Please use ListBlobsSegmentedAsync(String, Boolean, BlobListingDetails, Nullable<Int32>, BlobContinuationToken, BlobRequestOptions, OperationContext) method.

The 1st parameter to this method is the Blob Prefix and you need to specify Folder 1/Folder 2/ there.

2nd parameter to this method is useFlatBlobListing and you need to pass true for that.

It should return you a result like:

Folder 1/Folder 2/Folder 2.1/File 1
Folder 1/Folder 2/Folder 2.1/File 2
Folder 1/Folder 2/Folder 2.1/File 3

and you should be able to construct the desired treeview based on this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM