close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to list all blobs inside of a specific subdirectory in Azure Cloud Storage using Python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

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

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

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

  2. list all blobs inside of a specific subdirectory in Azure Cloud Storage using Python

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

Method 1

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="")

Method 2

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)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read