2015-11-02 70 views
0

我在读this tutorial,但我无法找到一种方法来列出容器下的所有(虚拟)文件夹而无需获取所有文件。我有500个(虚拟)文件夹中的26K文件。我只想获取文件夹列表,而不必等待几分钟,以获取包含整个文件列表的list_blobs的输出。有没有办法做到这一点?或者至少告诉list_blobs不要比容器下面的n更深?通过python API在azure blob存储中列出虚拟文件夹

回答

0

@拉夫Mantri指出了正确的方式来获得BlobPrefix元素的列表,我们可以利用它来创建一个函数来要求你的要求:

例如我有4个级别的目录:

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)