Authentication Failure when Accessing Azure Blob Storage through Connection String

微笑、不失礼 提交于 2021-02-10 20:21:07

问题


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:


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)

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

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.



来源:https://stackoverflow.com/questions/64344806/authentication-failure-when-accessing-azure-blob-storage-through-connection-stri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!