【问题标题】:Azure python SDK iterate over containers and tablesAzure python SDK 迭代容器和表
【发布时间】:2021-09-21 19:56:22
【问题描述】:

我有一个问题,我找不到合适的文档来解决它或找到任何好的路径。

我有几个 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')

【问题讨论】:

    标签: python-3.x azure azure-sdk-python


    【解决方案1】:

    你会想使用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 的实例。一旦你有了它,你需要做的就是调用 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("==========================")
    

    【讨论】:

    • 非常感谢您的回复。我试图跟进你的建议。如果我使用您的代码并运行它,我会收到错误 NameError: name 'blob_service_client' is not defined 并且在我的导入中我有 from azure.storage.blob import BlobClient, BlobServiceClient, ContainerClient 我认为这是一个导入问题,所以我使用了我声明的导入,但这一次,我我收到此错误TypeError: list_containers() missing 1 required positional argument: 'self'
    • 您需要使用client(您的BlobServiceClient 实例的名称)。
    • 我很抱歉,但我在最后一点上遇到了一些麻烦。请你好心解释一下最后一部分。根据您与我分享的有关 Containerclient 的文档,我发现要创建一个新实例,我需要容器名称。在循环范围内,该参数应替换为 container['name'].. but I get and error..TypeError: get_container_client() missing 1 required positional argument: 'container'`
    • 现在我明白我的错误了。非常感谢,这就像一个魅力。
    • 再次感谢你,我希望我能有你作为导师 XD.. 我有很多问题
    猜你喜欢
    • 2013-04-16
    • 2021-10-05
    • 2015-12-10
    • 2012-10-11
    • 1970-01-01
    • 2018-10-06
    • 2016-10-27
    • 1970-01-01
    • 2010-10-04
    相关资源
    最近更新 更多