当我们尝试将python v12 sdk与Azure Blob存储v12.5.0和Azure核心1.8.2结合使用时,当尝试从连接字符串创建azure 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=file)
我打印出了blob_client,它看起来很正常。但是下一行upload_blob给出了错误。
with open(os.path.join(root,file), "rb") as data:
blob_client.upload_blob(data)
错误消息如下
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.'
因此,我将http put请求打印到了azure blob存储中,并获得了[403]的响应值
答案 0 :(得分:1)
我可以使用与您相同的版本很好地运行以下代码。
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)
请检查您的连接字符串,并检查您的PC时间。
更新:
我尝试使用此代码,并得到相同的错误:
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)
然后我使用AzureCliCredential()
而不是DefaultAzureCredential()
。我通过Azure CLI使用az login
进行身份验证。而且有效。
如果使用环境凭证,则需要设置变量。无论如何,我建议您使用特定凭据代替DefaultAzureCredential
。
有关Azure身份的更多详细信息,请参见here。