How to list all blobs inside of a specific subdirectory in Azure Cloud Storage using Python?

Please try something like:

generator = blob_service.list_blobs(top_level_container_name, prefix="dir1/")

This should list blobs and folders in dir1 virtual directory.

If you want to list all blobs inside dir1 virtual directory, please try something like:

generator = blob_service.list_blobs(top_level_container_name, prefix="dir1/", delimiter="")

For more information, please see this link.


Not able to import BlockBlobService. Seems like BlobServiceClient is the new alternative. Followed the official doc and found this:

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

Create a Blob Storage Account client

connect_str = <connectionstring>
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

Create a container client

container_name="dummy"
container_client=blob_service_client.get_container_client(container_name)

This will list all blobs in the container inside dir1 folder/directory

blob_list = container_client.list_blobs(name_starts_with="dir1/")
for blob in blob_list:
print("\t" + blob.name)