I was reading this tutorial but I cannot find a way to list all the (virtual) folder under a container without getting all the files. I have 26K files in 500 (virtual) folders. I just want to get the list of folder without having to wait few minutes to get the output of list_blobs
containing the entire file list. Is there a way to do that? or at least tell list_blobs
to not go deeper than n
levels below a container?
You can try something like the following:
from azure.storage import BlobService
blob_service = BlobService(account_name='account-name', account_key='account-key')
bloblistingresult = blob_service.list_blobs(container_name='container-name', delimiter='/')
for i in bloblistingresult.prefixes:
print(i.name) #this will print the names of the virtual folders
SDK Source Code Reference: BlobService.list_blobs()
SKD Source Code Reference: BlobService.list_blobs().prefixes
Perhaps it's not too late for 2someone. list_blobs
doesn't accept delimiter
argument. Instead, use walk_blobs
( doc ) to get a generator with the files. Using delimiter="/"
you will get the next sublevel of files/folders:
For example:
blob_service_client = BlobServiceClient.from_connection_string(file_connect_str)
container_client = blob_service_client.get_container_client(container_name)
for file in container_client.walk_blobs('my_folder/', delimiter='/'):
print(file.name)
will return:
"my_folder/sub_folder_1/"
"my_folder/sub_folder_2/"
@ Gaurav Mantri pointed out the correct way to get a list of BlobPrefix elements, and we can leverage this to create a function to require your requirement:
For example I have 4 levels in directory:
import azure
from azure.storage.blob import BlobService
blob_service = BlobService(account_name='<account_name>', account_key='<account_key>')
def getfolders(depth=1):
result = []
searched = []
delimiter = '/'
print depth
blob_list = blob_service.list_blobs('container-name',delimiter='/')
result.extend(str(l.name) for l in blob_list.prefixes)
#for l in blob_list.prefixes:
# result.extend(str(l.name))
depth -= 1
while (depth>0):
print 'result: \n'
print ','.join(str(p) for p in result)
print 'searched: \n'
print ','.join(p for p in searched)
for p in [item for item in result if item not in searched]:
print p +' in '+ str(depth)
searched.append(p)
blob_list = blob_service.list_blobs('vsdeploy',prefix=p,delimiter='/')
result.extend(str(l.name) for l in blob_list.prefixes)
depth -= 1
return result
blob_list = getfolders(4)
print ','.join(str(p) for p in blob_list)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.