Search code examples
pythonazure-blob-storageazurite

Server failing to authenticate request when using Azurite to copy blob from one container to another


I am getting the error:

AuthorizationFailure, (Exception: HttpResponseError: Server failed to authenticate the request. Make sure the value of the Authorization header is formed correctly including the signature.)

Whenever I try to copy a blob from one container to another using a blob client, I have used the default Azurite connection strings as shown below, which should have authorization automatically granted, so I am confused on how to fix this error.

DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFe4xJtfl0s3bY5lZ0+ZGF4uEXC1Ls+zmLsd3H1qDGJiOggYo==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;

I have made sure the URL looks right. I have tried using 12.10.0 version of Azure blob storage. There has been no change in the error.


Solution

  • AuthorizationFailure, (Exception: HttpResponseError: Server failed to authenticate the request. Make sure the value of the Authorization header is formed correctly including the signature.)

    According to this MS-Document,

    The above error occurred may be passing incorrect key in the connection string.

    Initially, I tried with your same connection string and got the same error in my environment. To resolve the issue, you can use the below code with correct connection string to copy blob from one container to another.

    Code:

    from azure.storage.blob import BlobServiceClient
    
    
    connection_string = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;"
    
    source_container_name = "source-container"
    destination_container_name = "destinationcontainer"
    blob_name = "test-blob.txt"
    
    # Create blob service client
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    
    # Ensure containers exist
    source_container_client = blob_service_client.get_container_client(source_container_name)
    destination_container_client = blob_service_client.get_container_client(destination_container_name)
    
    source_blob_client = source_container_client.get_blob_client(blob_name)
    if not source_blob_client.exists():
        source_blob_client.upload_blob("This is a test blob.", overwrite=True)
    
    destination_blob_client = destination_container_client.get_blob_client(blob_name)
    copy_source = source_blob_client.url
    destination_blob_client.start_copy_from_url(copy_source)
    
    print("Blob copied successfully.")
    

    Output: enter image description here