【问题标题】:Authentication Failure when Accessing Azure Blob Storage through Connection String通过连接字符串访问 Azure Blob 存储时身份验证失败
【发布时间】:2021-01-28 08:35:18
【问题描述】:

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

我用过 天蓝色存储 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 storage,得到响应值为[403]

【问题讨论】:

    标签: azure-storage azure-blob-storage nix azure-authentication


    【解决方案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)
    

    请检查您的连接字符串,并检查您电脑的时间。

    关于错误有一个类似的问题:AzureStorage Blob Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature


    更新:

    我尝试了这段代码,得到了同样的错误:

    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 Identity 的更多详细信息,请参阅here

    【讨论】:

    • 当我尝试将文件上传到 blob 存储时出现问题:通过调用 blob_client.upload_blob(data) File "/.venv/lib/python3.8/site-packages/azure/storage /blob/_shared/response_handlers.py",第 147 行,在 process_storage_error 中引发错误
    • 嗨,@AliciaYang!您可以将代码添加到问题中吗?
    • 我在回复中添加了关于上传 blob 的示例。请分享您的错误的详细信息。
    • Hi Pamela,感谢您的回复,由于回复频道字数限制,我将代码添加到帖子中
    • 同样在我的nix虚拟环境中,时间显示与当前时间几乎相同,但微秒不同。但是它的单位是 DST 而不是 EDT,你认为这可能是问题吗?
    猜你喜欢
    • 2021-05-23
    • 2017-10-30
    • 1970-01-01
    • 2022-10-17
    • 2021-09-24
    • 2020-08-06
    • 2019-06-22
    • 1970-01-01
    • 2017-11-15
    相关资源
    最近更新 更多