1

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 !

  • In Azure blob storage, it does not contain `Directory`. If you want to get one blob,, we can directly use the name like `root/sub-folder/sample.txt`. – Jim Xu Jun 23 '21 at 09:01
  • yes, you're right. but is it possible to iterate through all the sub-folders present and get the blobs present there? – sanjay srinivasa Jun 23 '21 at 09:15

1 Answers1

3

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

Jim Xu
  • 21,610
  • 2
  • 19
  • 39