pythonazurecontainersazure-blob-storageblobstorage

Error in listing all blobs under the container in for loop statement- The requested URI does not represent any resource on the server


I am trying to list blobs in a Blob Container in Azure storage account. Here is my code:

 blob_service_client = BlobServiceClient.from_connection_string(landing_Access_token)
 container_client = blob_service_client.get_container_client(container_name)
 blob_list = container_client.list_blobs(prefix='/crm365/internal/reference/')
 
 for i in blob_list:
 print(i.name)

I got the error : The requested URI does not represent any resource on the server when I did the for loop to get every name of blobs. Everthing befor the loop was OK.

I have passed the prefix in list_blobs, but It was still same I've checked the connection string and I was able to log in the Azure storage and upload blobs to in the container. Could someone kindly help with that?


Solution

  • You can use the code below to list the blobs with specified prefixes using Python.

    In my environment, I have some files with the below structure.

    Portal:

    enter image description here Code:

    from azure.storage.blob import BlobServiceClient
    
    connection_string = "xxxxx"
    container_name = "sample"
    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(name_starts_with='backup/backup2/backup3')
    for i in blob_list:
        print(i.name.split('/')[-1])
    

    Output:

    state_county_codes.xlsm
    states_and_counties.xlsx
    temp.png
    temp2.jpg
    

    enter image description here

    Reference:

    azure.storage.blob.ContainerClient class | Microsoft Learn