[英]list virtual folders in azure blob storage via python API
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.
我在 500 个(虚拟)文件夹中有 26K 个文件。 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.我只想获取文件夹列表,而不必等待几分钟来获取包含整个文件列表的
list_blobs
输出。 Is there a way to do that?有没有办法做到这一点? or at least tell
list_blobs
to not go deeper than n
levels below a container?或者至少告诉
list_blobs
不要深入到容器下方的n
层?
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() SDK 源代码参考:BlobService.list_blobs()
SKD Source Code Reference: BlobService.list_blobs().prefixes SKD 源代码参考:BlobService.list_blobs().prefixes
Perhaps it's not too late for 2someone.也许现在对 2someone 来说还不算太晚。
list_blobs
doesn't accept delimiter
argument. list_blobs
不接受delimiter
参数。 Instead, use walk_blobs
( doc ) to get a generator with the files.相反,使用
walk_blobs
( doc ) 来获取带有文件的生成器。 Using delimiter="/"
you will get the next sublevel of files/folders:使用
delimiter="/"
您将获得下一个子级别的文件/文件夹:
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: @ Gaurav Mantri 指出了获取 BlobPrefix 元素列表的正确方法,我们可以利用它来创建一个函数来满足您的要求:
For example I have 4 levels in directory:例如,我在目录中有 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.