通过连接字符串访问Azure Blob存储时身份验证失败 [英] Authentication Failure when Accessing Azure Blob Storage through Connection String

查看:144
本文介绍了通过连接字符串访问Azure Blob存储时身份验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们尝试通过python v12 sdk和Azure Blob Storage v12.5.0和Azure核心1.8.2使用连接字符串创建azure blob客户端时,出现身份验证失败的错误.

我用过azure-storate-blob == 12.5.0天蓝色核心== 1.8.2

我尝试使用Python v12 SDK的连接字符串访问我的Blob存储帐户,并收到上述错误.我正在运行的环境是NixShell中的python venv.

调用blob_upload的代码如下:

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

我打印出了blob_client,它看起来很正常.但是upload_blob的下一行给出了错误.

 ,其中open(os.path.join(root,file),"rb")作为数据:blob_client.upload_blob(数据) 

错误消息如下

 文件< local_address>/.venv/lib/python3.8/site-packages/azure/storage/blob/_upload_helpers.py",位于upload_block_blob中返回client.upload(上传中的文件< local_address>/.venv/lib/python3.8/site-packages/azure/storage/blob/_produced/operations/_block_blob_operations.py"引发models.StorageErrorException(response,self._deserialize)azure.storage.blob._generation.models._models_py3.StorageErrorException:操作返回了无效状态'服务器无法验证请求.确保包括签名在内,正确构成了Authorization标头的值. 

因此,我将http put请求打印到了azure blob存储中,并获得了[403]的响应值

解决方案

对于与您相同的版本,我可以很好地使用以下代码.

从azure.storage.blob中的

 导入BlobServiceClientblob = BlobServiceClient.from_connection_string(conn_str =您在访问密钥中的连接字符串")使用open("./SampleSource.txt","rb")作为数据:blob.upload_blob(数据) 

请检查您的连接字符串,并检查您的PC时间.

关于该错误,存在类似的问题:

然后我使用 AzureCliCredential()而不是 DefaultAzureCredential().我通过Azure CLI使用 az登录进行身份验证.而且有效.

如果使用环境凭证,则需要设置变量.无论如何,我建议您使用特定凭据代替 DefaultAzureCredential .

有关Azure身份的更多详细信息,请参见此处.

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]

解决方案

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.

这篇关于通过连接字符串访问Azure Blob存储时身份验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆