How to list subdirectories in Azure blob storage -
ms has announced directory blob storage, , i'm trying use directories. having save blobs 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 have possibility enumerate subdirectories, e.g. have blobclient referenced common
container name, , subcontainers list type1, type2, type3
. possible list of subdirectories in directory. using listblobs returns full list of blobs within current container.
if list "subdirectories" in "common\service1" directory can use this:
var directory = blobcontainer.getdirectoryreference(@"common/service1"); var folders = directory.listblobs().where(b => b 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 cloudblobdirectory != null).tolist(); foreach (var folder in folders) { console.writeline(folder.uri); }
this output uri type1,type2 , type3 directory.
Comments
Post a Comment