copy files within azure blob container using python

96 Views Asked by At

I am using the azure-sdk for python to copy a blob in the same container. But I am getting authentication error.

the source code is:

status = None
blob_service_client = BlobServiceClient.from_connection_string(storage_account_connection_string)
copied_blob = blob_service_client.get_blob_client(containerName, 'test.csv')
result = copied_blob.start_copy_from_url(source_url=source_blob, requires_sync=True)
print(result)
status = result['copy_status']
if status != "success":
    for i in range(1000):
        props = copied_blob.get_blob_properties()
        status = props.copy.status
        print("Copy status: " + status)
        if status == "success":
            # copy finished
            break
        time.sleep(60*0.5)

And I am getting the error:

ClientAuthenticationError: Server failed to authenticate the request. Please refer to the information in the www-authenticate header. Time:2024-01-10T14:36:11.9520689Z ErrorCode:CannotVerifyCopySource Content: CannotVerifyCopySourceServer failed to authenticate the request. Please refer to the information in the www-authenticate header. Time:2024-01-10T14:36:11.9520689Z

Can anyone help me with this?

2

There are 2 best solutions below

0
Venkatesan On

Azure-sdk for python to copy a blob in the same container

Use the below Python code to copy a blob in the same container using Azure SDK for Python.

Code:

from azure.storage.blob import BlobServiceClient

connection_string = "xxxx"
container_name = "block-image"

blob_service_client = BlobServiceClient.from_connection_string(connection_string)

source_blob_client = blob_service_client.get_blob_client(container_name, "bike.png")
destination_blob_client = blob_service_client.get_blob_client(container_name, "Adventure.png")
copy_operation = destination_blob_client.start_copy_from_url(source_blob_client.url)
copy_status = destination_blob_client.get_blob_properties().copy.status

while copy_status != "success":
    copy_status = destination_blob_client.get_blob_properties().copy.status
print("Copy status: " + copy_status)

Output:

Copy status: success

Copy operation successful

Portal:

Blob storage in the Azure portal

Reference:

azure.storage.blob.BlobClient class | Microsoft Learn

0
Uros Krstic On

By looking at the Microsoft official documentation for BlobServiceClient class, you should either create a SAS (Shared Access Signature). This SAS uri can be created in the Azure portal inside of the container in the Security + networking section.

enter image description here

Other solution is to input the credential string parameter for the from_connection_string method. The value can be any of the following credential types as defined by the aformentioned documentation:

The value can be a SAS token string, an instance of a AzureSasCredential or AzureNamedKeyCredential from azure.core.credentials, an account shared access key, or an instance of a TokenCredentials class from azure.identity.