Search code examples
python-3.xazureazure-blob-storageazure-cloud-servicesazure-container-service

how to iterate through all the blob names from subdirectories as well, in Azure Cloud Storage using python?


Objective : to iterate and write all the blobs from root and multiple sub-directories present.

this is current code written:

for _file in container_client.walk_blobs(include='metadata'):
            file_name = 'file.txt'
            f = open(file_name, 'a+') 
            with open(file_name, 'r') as sample:
                read_sample = sample.read()
                if not re.search(_file.name, read_sample):
                    print('New One')
                    f.write(_file.name + os.linesep)
                    f.close()
  • the above code just returns blob names from root directory.

I was able to extract blobs from specific directory, but not all sub-directories present.

How do I get the blob names present in all sub-directories as well? Thanks for the help !


Solution

  • If you want to list blobs in one directory, please refer to the following code

    def walk_blob_hierarchy(prefix=""):
            nonlocal depth
            for item in container_client.walk_blobs(name_starts_with=prefix):
               
                if isinstance(item, BlobPrefix):                    
                    walk_blob_hierarchy(prefix=item.name)
                   
                else:
                  
                    print(item.name)
    walk_blob_hierarchy(prefix="<folder name>")
    

    For more details, please refer to here and here