Azure python sdk 容器已存在

Azure python sdk Container already exists

我有以下脚本可以将 blob 从一个容器复制到另一个容器。

#================================ SOURCE ===============================
# Source Client
connection_string = '' # The connection string for the source container
account_key = '' # The account key for the source container

# source_container_name = 'newblob' # Name of container which has blob to be copied
table_service_out = TableService(account_name='', account_key='')
table_service_in = TableService(account_name='', account_key='')

# Create client
client = BlobServiceClient.from_connection_string(connection_string) 




client = BlobServiceClient.from_connection_string(connection_string)
all_containers = client.list_containers(include_metadata=True)
for container in all_containers:
    # 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
    )

    
    print("==========================")
    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:
        # Create blob client for source blob
        source_blob = BlobClient(
        client.url,
        container_name = container['name'],
        blob_name = blob.name,
        credential = sas_token
    )
        target_connection_string = ''
        target_account_key = ''
        source_container_name = container['name']
        target_blob_name = blob.name
        target_destination_blob = container['name'] + today
        print(target_blob_name)
        # print(blob.name)
        target_client = BlobServiceClient.from_connection_string(target_connection_string)
        try:
            container_client = target_client.create_container(target_destination_blob)
            new_blob = target_client.get_blob_client(target_destination_blob, target_blob_name)
            new_blob.start_copy_from_url(source_blob.url)
            print("COPY TO")
            print(f"TRY: saving blob {target_blob_name} into {target_destination_blob} ")
        except:
            # 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)
            print("COPY TO")
            print(f"EXCEPT: saving blob {target_blob_name} into {target_destination_blob} ")
        

这个完全相同的代码昨天工作得很好,但今天当我再次 运行 时,我得到了以下错误。

Traceback (most recent call last):
  File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_container_client.py", line 292, in create_container
    return self._client.container.create( # type: ignore
  File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_generated/operations/_container_operations.py", line 132, in create
    map_error(status_code=response.status_code, response=response, error_map=error_map)
  File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/core/exceptions.py", line 105, in map_error
    raise error
azure.core.exceptions.ResourceExistsError: Operation returned an invalid status 'The specified container already exists.'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/user/Desktop/AzCopy/blob.py", line 68, in <module>
    container_client = target_client.create_container(target_destination_blob)
  File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/core/tracing/decorator.py", line 83, in wrapper_use_tracer
    return func(*args, **kwargs)
  File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_blob_service_client.py", line 518, in create_container
    container.create_container(
  File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/core/tracing/decorator.py", line 83, in wrapper_use_tracer
    return func(*args, **kwargs)
  File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_container_client.py", line 300, in create_container
    process_storage_error(error)
  File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_shared/response_handlers.py", line 150, in process_storage_error
    error.raise_with_traceback()
  File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/core/exceptions.py", line 247, in raise_with_traceback
    raise super(AzureError, self).with_traceback(self.exc_traceback)
  File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_container_client.py", line 292, in create_container
    return self._client.container.create( # type: ignore
  File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_generated/operations/_container_operations.py", line 132, in create
    map_error(status_code=response.status_code, response=response, error_map=error_map)
  File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/core/exceptions.py", line 105, in map_error
    raise error
azure.core.exceptions.ResourceExistsError: The specified container already exists.
RequestId:
Time:2021-09-29T10:42:10.8775237Z
ErrorCode:ContainerAlreadyExists
Error:None

通过 try and except 我直到今天才遇到任何问题。 检查容器是否存在而不尝试再次创建它是否有更好的选择?

非常感谢您提供的任何帮助。

更新:

我用 if 条件更新了我的代码,如下所示:

target_client = BlobServiceClient.from_connection_string(target_connection_string)

        container_client = target_client.get_container_client(target_destination_blob)
        if (!container_client.exists())
            container_client.create()
        new_blob = target_client.get_blob_client(target_destination_blob, target_blob_name)
        new_blob.start_copy_from_url(source_blob.url)
        print("COPY TO")
        print(f"TRY: saving blob {target_blob_name} into {target_destination_blob} ")

但是 if (!container_client.exists()): 被突出显示为错误 Expression Expected

您可以在 ContainerClient 上使用 exists 方法来查看容器是否存在。如果容器不存在,exists 方法将 return false 然后您可以创建容器。如果它存在,您可以简单地继续您的代码。

例如您的代码可以是:

...
...
...
container_client = target_client.get_container_client(target_destination_blob)
if not container_client.exists():
    print("container does not exist. create it")
    container_client.create_container()
...
...
...