List all files in (sub)directories in Azure

Viewed 9609

I am developing an azure function using Java. I need to iterate all the files in the following folder

aDirectory/aSubdirectoryWithManyFiles/

There are many files in that path,:

aDirectory/aSubdirectoryWithManyFiles/file1
aDirectory/aSubdirectoryWithManyFiles/file2
aDirectory/aSubdirectoryWithManyFiles/file3
aDirectory/aSubdirectoryWithManyFiles/file4
aDirectory/aSubdirectoryWithManyFiles/file5

so I wrote the following code in order to get them all:

// myCloudBlobContainer is a CloudBlobContainer
// I expected to get all files thanks to the next row
Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs();
// The only blob found in the container is the directory itself
for (ListBlobItem blob : blobs) {
    //log the current blob URI
    if (blob instanceof CloudBlob) {  // this never happens
        CloudBlob cloudBlob = (CloudBlob) blob;
        //make nice things with every found file
    }
}

The only blob iterated in the for is the directory, noone of the expected files. so in logs i get only the following URI:

https://blablablabla.blob.core.windows.net/aDirectory/aSubdirectoryWithManyFiles/

What should I do in order to access every file?

And in case I would have more than one subdirectory, as in the following example?

aDirectory/aSubdirectoryWithManyFiles/files(1-5)
aDirectory/anotherSubdirectoryWithManyFiles/files(6-10)

Thanks in advance


Edit

In order to make methods testable, the project uses wrappers and interfaces instead of directly using directly a CloudBlobContainer; basically, the CloudBlobContainer is given by CloudBlobClient.getContainerReference("containername")

After the answer to this question, I changed teh code to the following so I used listBlobs with parameters myCloudBlobContainer.listBlobs("aDirectory", true) and I wrote the following code in order to get them all:

// myCloudBlobClient is a CloudBlobClient
CloudBlobContainer myCloudBlobContainer = myCloudBlobClient.getContainerReference("containername")
// I expected to get all files thanks to the next row
Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs("aDirectory", true); // HERE THE CHANGE
// No blob found this time
for (ListBlobItem blob : blobs) { // NEVER IN THE FOR
    //log the current blob URI
    if (blob instanceof CloudBlob) {
        CloudBlob cloudBlob = (CloudBlob) blob;
        //make nice things with every found file
    }
}

But this time, it doesn't go at all in the for...

2 Answers

I must say that the previous answer made me to waste time; the problem was in the fact that only one for is not enough to find files in folders. The first for finds the folders and subfolders, plus (maybe, i didn't check) files that are in the "root" (let's call it like that).

Having the folders, for each of them we have to cast as CloudBlobDirectory in order to see and iterate all contained files with another for.

Here the solution that works for me:

// myCloudBlobClient is a CloudBlobClient
CloudBlobContainer myCloudBlobContainer = myCloudBlobClient.getContainerReference("containername")
// I expected to get all files thanks to the next row
Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs();
// only directories here, another for needed to scan files
for (ListBlobItem blob : blobs) {
    if (blob instanceof CloudBlobDirectory) {
        CloudBlobDirectory directory = (CloudBlobDirectory)blob;
        //next is in try/catch
        Iterable<ListBlobItem> fileBlobs = directory.listBlobs();
        for (ListBlobItem fileBlob : fileBlobs) {
            if (fileBlob instanceof CloudBlob) {
            CloudBlob cloudBlob = (CloudBlob) fileBlob;
            //make nice things with every found file
            }
        }
    } // else: may be we found a cloudBlob in root?
}

This helped me to find the right way:

https://social.msdn.microsoft.com/Forums/en-US/1cfdc91f-e588-4839-a878-9650339a0a06/list-all-blobs-in-c?forum=windowsazuredata

Try using the following override of listBlobs method:

listBlobs(String prefix, boolean useFlatBlobListing)

So your code would be:

Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs("aDirectory", true);

This will list all blobs inside "aDirectory" virtual folder in your blob container.

Related