def list_containers(self, blob_service_client: BlobServiceClient):
containers = blob_service_client.list_containers(include_metadata=True)
for container in containers:
print(container['name'], container['metadata'])
以下示例仅列出以 name_starts_with 参数中指定的前缀开头的容器:
def list_containers_prefix(self, blob_service_client: BlobServiceClient):
containers = blob_service_client.list_containers(name_starts_with='test-')
for container in containers:
print(container['name'])
你还可以指定每页结果数限制。 此示例传入 results_per_page 并分页显示结果:
def list_containers_pages(self, blob_service_client: BlobServiceClient):
all_pages = blob_service_client.list_containers(results_per_page=5).by_page()
for container_page in all_pages:
i += 1
print(f"Page {i}")
for container in container_page:
print(container['name'])
async def main():
sample = ContainerSamples()
# TODO: Replace <storage-account-name> with your actual storage account name
account_url = "https://<storage-account-name>.blob.core.windows.net"
credential = DefaultAzureCredential()
async with BlobServiceClient(account_url, credential=credential) as blob_service_client:
await sample.list_containers(blob_service_client)
if __name__ == '__main__':
asyncio.run(main())