问题
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?
回答1:
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
回答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)
来源:https://stackoverflow.com/questions/33477687/list-virtual-folders-in-azure-blob-storage-via-python-api