Azure python SDK 迭代容器和表

Azure python SDK iterate over containers and tables

我遇到了一个问题,我找不到合适的文档来解决它,也找不到任何好的方法可以遵循。

我有几个 azure 存储容器,它们有多个容器和 blob。

我正在尝试遍历每个存储帐户并获得其容器的列表,因此我可以将它们复制到另一个存储帐户作为备份。

昨天在社区的帮助下,我完成了从存储帐户到存储帐户的复制工作,如下所示:

from azure.storage.blob import BlobClient, BlobServiceClient, ContainerClient
from azure.storage.blob import ResourceTypes, AccountSasPermissions
from azure.storage.blob import generate_account_sas    
from datetime import *




#================================ SOURCE ===============================
# Source Client
connection_string = '' # The connection string for the source container
account_key = '' # The account key for the source container
source_container_name = '' # Name of container which has blob to be copied
blob_name = '' # Name of the blob you want to copy




# Create client
client = BlobServiceClient.from_connection_string(connection_string) 



# Create sas token for blob
sas_token = generate_account_sas(
    account_name = client.account_name,
    account_key = account_key, 
    resource_types = ResourceTypes(object=True, container=True),
    permission= AccountSasPermissions(read=True,list=True),
    # start = datetime.now(),
    expiry = datetime.utcnow() + timedelta(hours=4) # Token valid for 4 hours
)

# Create blob client for source blob
source_blob = BlobClient(
    client.url,
    container_name = source_container_name, 
    blob_name = blob_name,
    credential = sas_token
)


# ============================= TARGET =======================================

# Target Client
target_connection_string = ''
target_account_key = ''
source_container_name = source_container_name
target_blob_name = ''
target_destination_blob = ''

# Create target client
target_client = BlobServiceClient.from_connection_string(target_connection_string)
container = ContainerClient.from_connection_string(target_connection_string, target_destination_blob)

# Create new blob and start copy operation.
# new_blob = client.get_blob_client(destination_container_name, blob_name)    
new_blob = target_client.get_blob_client(target_destination_blob, target_blob_name)
new_blob.start_copy_from_url(source_blob.url)

这段代码工作得很好,但如您所见,我必须对容器和 blob 名称进行硬编码。

我正在尝试做的是遍历特定存储帐户中的所有容器,然后开始将内容复制到另一个存储帐户中的相同位置。

到目前为止,我在 GitHub 上找到的唯一文档与此相关:

https://github.com/Azure/azure-storage-python/issues/389

但我无法克服这个问题。

我设法通过使用库 from azure.cosmosdb.table.tableservice import TableService,ListGenerator

实现了这种存储帐户循环但与表相关

所以我想知道是否有人可以帮助我了解周围有哪些图书馆可以帮助实现这一点。非常感谢您能为我提供的任何帮助。

更新:

all_containers = client.list_containers(include_metadata=True)

for container in all_containers:
    print(container['name'], container['metadata'])
    
container_client = BlobServiceClient.get_container_client(container['name'])
    

blobs_list = container_client.list_blobs()
for blob in blobs_list:
    print(blob.name + '\n')

你会想要使用 Azure Storage Blobs client library for Python

要列出容器,您需要在 blob 服务客户端对象上使用 list_containers 方法。

   all_containers = client.list_containers(include_metadata=True)
   for container in all_containers:
       print(container['name'], container['metadata'])

然后使用 container['name'],您将创建一个 ContainerClient. Once you have that, all you need to do is call list_blobs 方法的实例来列出该容器中的 blob。

   blobs_list = container_client.list_blobs()
   for blob in blobs_list:
       print(blob.name + '\n')

更新

请尝试下面的代码。它将在存储帐户中列出所有容器和其中的 blob,并打印容器名称和 blob 名称。

from azure.storage.blob import BlobServiceClient

connection_string = "DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=accountkey"
client = BlobServiceClient.from_connection_string(connection_string)
all_containers = client.list_containers(include_metadata=True)
for container in all_containers:
    print(container['name'], container['metadata'])
    print("==========================")
    container_client = client.get_container_client(container.name)
    print(container_client)
    blobs_list = container_client.list_blobs()
    for blob in blobs_list:
        print(blob.name)
        print("==========================")