How to list subdirectories in Azure blob storage
Asked Answered
T

3

14

MS has announced directory like blob storage, and I'm trying to use it like directories. Having save some blobs by names:

Common\Service1\Type1\Object1
Common\Service1\Type1\Object2
Common\Service1\Type2\Object1
Common\Service1\Type2\Object2
Common\Service1\Type3\Object1
Common\Service1\Type3\Object2
Common\Service1\Type3\Object3

I'd like to have possibility to enumerate subdirectories, e.g. I have blobclient referenced to Common container name, and I would like to get subcontainers list Type1, Type2, Type3. Is it possible to get list of subdirectories in some directory. Using ListBlobs returns full list of blobs within current container.

Tuyere answered 11/11, 2014 at 14:38 Comment(2)
Are you talking about Blob Storage or Azure File Service?Dhaulagiri
@GauravMantri, Azure BlobstorageTuyere
J
19

If you would like to list all "subdirectories" in "Common\Service1" directory you can use something like this:

    var directory = blobContainer.GetDirectoryReference(@"Common/Service1");
    var folders = directory.ListBlobs().Where(b => b as CloudBlobDirectory != null).ToList();
    foreach (var folder in folders)
    {
        Console.WriteLine(folder.Uri);
    }

Full code sample:

    var random = new Random();
    CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
    var cloudBlobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer blobContainer = cloudBlobClient.GetContainerReference("test-container");
    blobContainer.CreateIfNotExists();
    string[] objects = new[]
                           {
                               @"Common\Service1\Type1\Object1", @"Common\Service1\Type1\Object2", @"Common\Service1\Type2\Object1",
                               @"Common\Service1\Type2\Object2", @"Common\Service1\Type3\Object1", @"Common\Service1\Type3\Object2",
                               @"Common\Service1\Type3\Object3"
                           };
    foreach (var newObject in objects)
    {
        var newBlob = blobContainer.GetBlockBlobReference(newObject);
        var buffer = new byte[1024];
        random.NextBytes(buffer);
        newBlob.UploadFromByteArray(buffer,0,buffer.Length);
    }

    var directory = blobContainer.GetDirectoryReference(@"Common/Service1");
    var folders = directory.ListBlobs().Where(b => b as CloudBlobDirectory != null).ToList();
    foreach (var folder in folders)
    {
        Console.WriteLine(folder.Uri);
    }

This will output Uri for Type1,Type2 and Type3 directory.

Jingo answered 11/11, 2014 at 15:37 Comment(3)
There is mistype in your code, it uses / instead of backslashTuyere
Actually it's not. I does not matter how do you get reference for blob blobContainer.GetBlockBlobReference, both / and \ works, but for some reason GetDirectoryReference needs to be with \ (Uri style), I guess it has something to do with directory being just a prefix and all blob prefixes are stored Uri format.Jingo
for me it was enough to find out there's a method like blobContainer.GetDirectoryReference(@"Common/Service1"); Thanks!Portemonnaie
R
10

Building on b2zw2a's answer:

  • The @ is only needed when using \, not /.
  • Don't chain ToList() after ListBlobs(). ListBlobs() provides lazy loading and will give you better perf.
  • Use OfType<CloudBlobDirectory>() to filter out only the type you want

Giving you:

var directory = blobContainer.GetDirectoryReference("Common/Service1");
var folders = directory.ListBlobs().OfType<CloudBlobDirectory>();
foreach (var folder in folders)
{
    Console.WriteLine(folder.Uri);
}
Rack answered 7/12, 2016 at 16:7 Comment(0)
M
-1
var nameList=logoContainer.ListBlobs().Where(b => b as CloudBlobDirectory != null).Select(x => x.Uri + "").ToList();

By using this you can get all the filenames in a single query.

Macedonia answered 4/5, 2017 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.