Han*_*art 7 python azure azure-blob-storage
我正在阅读本教程,但我无法找到一种方法来列出容器下的所有(虚拟)文件夹而不获取所有文件。我的 500 个(虚拟)文件夹中有 26K 个文件。我只想获取文件夹列表,而不必等待几分钟即可获取list_blobs
包含整个文件列表的输出。有没有办法做到这一点?或者至少告诉list_blobs
不要深入到n
容器下方的水平面?
Dan*_*les 15
也许对某人来说还不算太晚。list_blobs
不接受delimiter
争论。相反,使用walk_blobs
( doc ) 获取包含文件的生成器。使用delimiter="/"
您将获得文件/文件夹的下一个子级别:
例如:
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)
Run Code Online (Sandbox Code Playgroud)
将返回:
"my_folder/sub_folder_1/"
"my_folder/sub_folder_2/"
Run Code Online (Sandbox Code Playgroud)
您可以尝试如下操作:
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
Run Code Online (Sandbox Code Playgroud)
SDK 源代码参考:BlobService.list_blobs()
SKD 源代码参考:BlobService.list_blobs().prefixes