0

我正在查看 Microsoft 的此文档。

https://docs-microsoft-com.translate.goog/es-es/azure/storage/blobs/storage-quickstart-blobs-python?_x_tr_sl=es&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=nui,sc,elem

据说要创建一个容器,我们应该使用

# Create a unique name for the container
container_name = str(uuid.uuid4())

# Create the container
container_client = blob_service_client.create_container(container_name)

我认为这很容易,所以我尝试在我的代码中使用它。如下

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("==========================")
    # # ============================= TARGET =======================================

    # Target Client
    target_connection_string = ''
    target_account_key = ''
    source_container_name = source_container_name
    target_blob_name = blob.name
    target_destination_blob = str(container['name'])
    print(target_destination_blob)
    container_client = BlobServiceClient.create_container(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)
    print(source_blob.url)

目的是遍历存储A中的所有容器,并复制所有容器和blob并将它们发送到存储B,重新创建相同的容器名称和blob(基本上是一个备份系统)

此时,由于存储 B 中不存在容器,我需要确保我正在检查这些存储does not exists,如果是,则创建它们,否则它可以处理副本。但是现在我只是想用上面的代码创建一个容器,但我得到了这个错误。

TypeError: create_container() missing 1 required positional argument: 'name'

因为名称应该是一个字符串。我试图转换为 str 但我得到了同样的错误。

根据文档,参数应该是一个字符串,但这似乎不起作用。

更新:

这是我的代码更新版本:

from typing import Container
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
destination_container_name = '' # Name of container where blob will be copied



# 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
)

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("==========================")
    # # ============================= TARGET =======================================

    # Target Client
    target_connection_string = ''
    target_account_key = ''
    source_container_name = source_container_name
    target_blob_name = blob.name
    target_destination_blob = str(container['name'])
    print(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)
    container_client = target_client.create_container(target_destination_blob)
    # Create new blob and start copy operation.  
    new_blob = target_client.get_blob_client(target_destination_blob, target_blob_name)
    new_blob.start_copy_from_url(source_blob.url)

我对这种行为的期望是循环遍历所有容器和 blob,SOURCE并在其中创建容器和 blob 的精确副本TARGET

一个问题是,如果我第一次运行代码,它只创建第一个存储,但没有 blob,如果我第二次运行它,我会收到错误

ErrorCode:ContainerAlreadyExists
4

1 回答 1

0

您需要先创建target_client,然后再尝试创建容器并在调用create_container. 请尝试以下代码(部分代码):

# Target Client
target_connection_string = ''
target_account_key = ''
source_container_name = source_container_name
target_blob_name = blob.name
target_destination_blob = str(container['name'])
print(target_destination_blob)

# Create target client
target_client = BlobServiceClient.from_connection_string(target_connection_string)
container_client = target_client.create_container(target_destination_blob)
于 2021-09-21T14:05:24.947 回答