Search code examples
pythonlistcontainersazure-blob-storageblob

How to list all the blob files that don't belong to any subfolders in Azure Storage explorer using python


I want to list down all the blob files that don't belong to any subfolders inside a blob container in Azure Storage Explorer. I'm using python.

I referred to this and this and tested it.But I'm getting an error.

This is my code.

from azure.storage.blob import BlobServiceClient

def main(inputblob: func.InputStream, outputblob: func.Out[bytes]):
connection_string = 'DefaultEndpointsProtocol=http;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;'
container_name="cloudops-resources"


#create the BlobServiceClient object which will be used to create a container client
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
#list blobs 
container_client = blob_service_client.get_container_client(container_name)
blob_list = container_client.list_blobs(container_name, prefix="", delimiter="/")
blobs = []
for blob in blob_list:
    print(blob.name)

This is the error I'm getting:

System.Private.CoreLib: Exception while executing function: Functions.BlobTrigger. System.Private.CoreLib: Result: Failure Exception: TypeError: request() got an unexpected keyword argument 'delimiter'

This was the only method I can found.Can someone please help me?


Solution

  • I tried in my environment and got the below results:

    Initially, I had some blobs and folders in my storage account.

    Portal:

    enter image description here

    I want to list down all the blob files that don't belong to any subfolders inside a blob container in Azure Storage Explorer

    You can use the below code to get the list of blobs that does not belongs to any folders.

    Code:

    from azure.storage.blob import BlobServiceClient
    
    connection_string = ""
    container_name = "test2"
    
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    container_client = blob_service_client.get_container_client(container_name)
    
    blob_list = container_client.list_blobs()
    
    for blob in blob_list:
        if "/" not in blob.name:
            print(blob.name)
    

    Output:

    The above code was executed and successfully listed blobs.

    27-03-2023.html
    28-03-2023.html
    29-03-2023 (1).html
    29-03-2023 (2).html
    29-03-2023.html
    30-03-2023.html
    31-03-2023 (1).html
    31-03-2023 (2).html
    

    enter image description here

    Reference: List blobs with Python - Azure Storage | Microsoft Learn