简体   繁体   中英

How do you get the size of specific directory within a container in Azure Blob Storage?

I'm dealing with a large amount data contained in specific directories within an Azure Blob storage instance, and I would like to just get the size of all of the contents within a certain directory. I know how to get the size of the entire container, however it is escaping me how to just specify the directory itself to pull the data from.

My current code looks like this:

    private static long GetSizeOfBlob(CloudStorageAccount storageAccount, string nameOfBlob)
    {
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Finding the container
        CloudBlobContainer container = blobClient.GetContainerReference(nameOfBlob);

        // Iterating to get container size
        long containerSize = 0;
        foreach (var listBlobItem in container.ListBlobs(null, true))
        {
            var blobItem = listBlobItem as CloudBlockBlob;
            containerSize += blobItem.Properties.Length;
        }

        return containerSize;
    }

If I specify the blob container to be something like "democontainer/testdirectory", I get a 400-error (I figured since it was a directory, using the backslashes would allow me to just navigate to the directory I wanted to iterate through). Any help would be greatly appreciated.

You need to get the CloudBlobDirectory first then you can work from there.

For Example:

var directories = new List<string>();
var folders = blobs.Where(b => b as CloudBlobDirectory != null);

foreach (var folder in folders)
{
    directories.Add(folder.Uri);
}

I've solved my own question, but leaving here for future azure blob storage explorers.

You actually need to provide the prefix when you call container.ListBlobs(prefix: 'somedir', true) in order to just access the specific directory involved. This directory is whatever comes AFTER the container name you are accessing.

    private string GetBlobContainerSize(CloudBlobContainer contSrc)
    {
        var blobfiles = new List<string>();
        long blobfilesize = 0;    
        var blobblocks = new List<string>();
        long blobblocksize = 0;

        foreach (var g in contSrc.ListBlobs())
        {
            if (g.GetType() == typeof(CloudBlobDirectory))
            {
               foreach (var file in ((CloudBlobDirectory)g).ListBlobs(true).Where(x => x as CloudBlockBlob != null))
               {
                   blobfilesize += (file as CloudBlockBlob).Properties.Length;
                   blobfiles.Add(file.Uri.AbsoluteUri);
              }
                }
                else if (g.GetType() == typeof(CloudBlockBlob))
                {
                    blobblocksize += (g as CloudBlockBlob).Properties.Length;
                    blobblocks.Add(g.Uri.AbsoluteUri);
                }
            }

            string res = string.Empty;
            if (blobblocksize > 0) res += "size: " + FormatSize(blobblocksize) + "; blocks: " + blobblocks.Count();
            if (!string.IsNullOrEmpty(res) && blobfilesize > 0) res += "; ";
            if (blobfilesize > 0) res += "size: " + FormatSize(blobfilesize) + "; files: " + blobfiles.Count();
            return res;
}

private string FormatSize(long len)
{
    string[] sizes = { "B", "KB", "MB", "GB", "TB" };
    int order = 0;
    while (len >= 1024 && order < sizes.Length - 1)
    {
        order++;
        len = len/1024;
    }
    return $"{len:0.00} {sizes[order]}".PadLeft (9, ' ');
}

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