aar*_*ron 3 python azure azure-storage
我研究了Azure文档https://docs.microsoft.com/zh-cn/azure/storage/blobs/storage-quickstart-blobs-python中的示例代码
from azure.storage.blob import BlockBlobService
account_name = "x"
account_key = "x"
top_level_container_name = "top_container"
blob_service = BlockBlobService(account_name, account_key)
print("\nList blobs in the container")
generator = blob_service.list_blobs(top_level_container_name)
for blob in generator:
print("\t Blob name: " + blob.name)
Run Code Online (Sandbox Code Playgroud)
现在,我想知道如何在集装箱行走中获得更多的细颗粒。我的容器top_level_container_name有几个子目录
我希望能够列出这些目录之一中的所有Blob。例如
如何获得仅dir1内容的生成器,而不必遍历所有其他目录?(我也会带一个清单或字典)
我尝试将/ dir1添加到top_level_container_name的名称中,这样虽然可以,top_level_container_name = "top_container/dir1"但是没有用。我找回错误代码 azure.common.AzureHttpError: The requested URI does not represent any resource on the server. ErrorCode: InvalidUri
这些文档似乎甚至都没有关于BlockBlobService.list_blobs()的任何信息https://docs.microsoft.com/zh-cn/python/api/azure.storage.blob.blockblobservice.blockblobservice?view=azure-python
Gau*_*tri 15
请尝试以下方法:
generator = blob_service.list_blobs(top_level_container_name, prefix="dir1/")
Run Code Online (Sandbox Code Playgroud)
这应该列出dir1虚拟目录中的Blob和文件夹。
如果要列出dir1虚拟目录中的所有Blob ,请尝试以下操作:
generator = blob_service.list_blobs(top_level_container_name, prefix="dir1/", delimiter="")
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参见this link。
无法导入 BlockBlobService。似乎 BlobServiceClient 是新的选择。按照官方文档,发现这个:
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
Run Code Online (Sandbox Code Playgroud)
创建 Blob 存储帐户客户端
connect_str = <connectionstring>
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
Run Code Online (Sandbox Code Playgroud)
创建容器客户端
container_name="dummy"
container_client=blob_service_client.get_container_client(container_name)
Run Code Online (Sandbox Code Playgroud)
这将列出 dir1 文件夹/目录中容器中的所有 blob
blob_list = container_client.list_blobs(name_starts_with="dir1/")
for blob in blob_list:
print("\t" + blob.name)
Run Code Online (Sandbox Code Playgroud)
该模块azurebatchload提供了此功能以及更多功能。您可以过滤文件夹或文件名,并选择以各种格式获取结果:
from azurebatchload import Utils
list_blobs = Utils(container='containername').list_blobs()
Run Code Online (Sandbox Code Playgroud)
from azurebatchload import Utils
df_blobs = Utils(
container='containername',
dataframe=True
).list_blobs()
Run Code Online (Sandbox Code Playgroud)
from azurebatchload import Utils
list_blobs = Utils(
container='containername',
name_starts_with="foldername/"
).list_blobs()
Run Code Online (Sandbox Code Playgroud)
from azurebatchload import Utils
dict_blobs = Utils(
container='containername',
name_starts_with="foldername/",
extended_info=True
).list_blobs()
Run Code Online (Sandbox Code Playgroud)
from azurebatchload import Utils
df_blobs = Utils(
container='containername',
name_starts_with="foldername/",
extended_info=True,
dataframe=True
).list_blobs()
Run Code Online (Sandbox Code Playgroud)
免责声明:我是 azurebatchload 模块的作者。