The following list out all the folders in a storage container and calculate the number of files and the size for each folder.

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(System.Configuration.ConfigurationManager.AppSettings["AzureBlobStorageConnectionString"]);

        CloudBlobClient blobClient;
        string blobContainerName = System.Configuration.ConfigurationManager.AppSettings["AzuareBlobStorageRootFolder"];
        CloudBlobContainer blobContainer;

        // Create a blob client for interacting with the blob service.
        blobClient = storageAccount.CreateCloudBlobClient();
        blobContainer = blobClient.GetContainerReference(blobContainerName);

        foreach (var dir in blobContainer.ListBlobs().OfType<CloudBlobDirectory>())
        {
            long lSize = 0;
            int intCount = 0;

            foreach (var file in ((CloudBlobDirectory)dir).ListBlobs(true).Where(x => x as CloudBlockBlob != null))
            {
                lSize = (file as CloudBlockBlob).Properties.Length + lSize;
                intCount = intCount + 1;
            }
         }

Last modified: February 24, 2019

Author

Comments

Write a Reply or Comment