I have a storage container and inside that I have 3 levels of directories as follow:
--Container
--Folder1
--Folder2
--Folder3
--blobs here
I need to check are there any blob present in Folder3 or even better just check if Folder3 exist or not.
I tried to use
blob_exist = BlobClient.from_connection_string(conn_str = os.getenv("conString"), container_name="Container",blob_name="Folder1/Folder2/Folder3").exists()
It always returns False irrespective of folder exists or not. Can anybody tell me how can I achieve this?
I know empty folders doesn't exist in Blob Containers, but my intension is to check if a folder exists, then continue other business logic.
It's impossible to directly check if a folder exists in blob storage. But you can use the list_blobs()
method and the name_starts_with
parameter.
For example:
from azure.storage.blob import BlobServiceClient
blob_service_client=BlobServiceClient.from_connection_string(connstr)
container_client = blob_service_client.get_container_client(container)
myblobs = container_client.list_blobs(name_starts_with="Folder1/Folder2/Folder3")
#define a list to store the blobs if exists
blob_list=[]
for s in myblobs:
blob_list.append(s)
#use break to make sure only one iteration to avoid iterating all the blobs
break
if len(blob_list) > 0:
print("yes")
else:
print("no")