Authentication Failure when Accessing Azure Blob Storage through Connection String

6.9k views Asked by At

We got error of Authentication fail, when we try to create an azure blob client from connection string, using python v12 sdk with Azure Blob Storage v12.5.0, and Azure core 1.8.2.

I used azure-storate-blob == 12.5.0 azure-core == 1.8.2

I tried to access my blob storage account using connection string with Python v12 SDK and received the error above. The environment I'm running in is python venv in NixShell.

The code for calling the blob_upload is as following:

blob_service_client = BlobServiceClient(account_url=<>,credential=<>)    
blob_client = blob_service_client.get_blob_client(container=container_name,
        blob=file)

I printed out blob_client, and it looks normal. But the next line of upload_blob gives error.

with open(os.path.join(root,file), "rb") as data:
                    blob_client.upload_blob(data)

The error message is as follows

    File "<local_address>/.venv/lib/python3.8/site-packages/azure/storage/blob/_upload_helpers.py", in upload_block_blob
    return client.upload(
  File "<local_address>/.venv/lib/python3.8/site-packages/azure/storage/blob/_generated/operations/_block_blob_operations.py", in upload
    raise models.StorageErrorException(response, self._deserialize)
azure.storage.blob._generated.models._models_py3.StorageErrorException: Operation returned an invalid status 'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.'

So I printed out the http put request to azure blob storage, and get the response value of [403]

1

There are 1 answers

6
unknown On BEST ANSWER

I can work the following code well with the version the same as yours.

from azure.storage.blob import BlobServiceClient
blob=BlobServiceClient.from_connection_string(conn_str="your connect string in Access Keys")
with open("./SampleSource.txt", "rb") as data:
    blob.upload_blob(data)

Please check your connect-string, and check your PC's time.

There is a similar issue about the error: AzureStorage Blob Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature


UPDATE:

I tried with this code, and get the same error:

from azure.storage.blob import BlobServiceClient
from azure.identity import DefaultAzureCredential

token_credential = DefaultAzureCredential()

blob_service_client = BlobServiceClient(account_url="https://pamelastorage123.blob.core.windows.net/",credential=token_credential)    
blob_client = blob_service_client.get_blob_client(container="pamelac", blob="New Text Document.txt")

with open("D:/demo/python/New Text Document.txt", "rb") as data:
    blob_client.upload_blob(data)

enter image description here

Then I use AzureCliCredential() instead of DefaultAzureCredential(). I authenticate via the Azure CLI with az login. And it works.

enter image description here

If you use environment credential, you need to set the variables. Anyway, I recommend you to use the specific credentials instead DefaultAzureCredential.

For more details about Azure Identity, see here.