尝试使用 ContainerClient 类时遇到顽固的 ClientAuthenticationError

问题描述 投票:0回答:1

我正在尝试通过以前的团队设计的管道在Azure中运行一些集成测试,但我运气不佳。具体来说,管道不断达到

ClientAuthenticationError

这是 Python 代码的最小工作示例,它在崩溃之前运行:

SAS_TOKEN = os.environ["SAS_TOKEN"]

credential = AzureSasCredential(SAS_TOKEN)
account_name = "ACCOUNT_NAME"
account_url = f"https://{account_name}.blob.core.windows.net"
container_name = "CONTAINER_NAME"

blob_service_client = BlobServiceClient(account_url, credential=credential)
container = blob_service_client.get_container_client(container_name)
parquet_names = container.list_blob_names(name_starts_with="PATTERN")
list_of_parquet = list(parquet_names)

这是上面代码产生的回溯:

File "/home/vsts/work/1/s/./tests/run_tests.py", line 24, in access_datalake_locally
  list_of_parquet = list(parquet_names)
File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/azure/core/paging.py", line 123, in __next__
  return next(self._page_iterator)
File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/azure/core/paging.py", line 75, in __next__
  self._response = self._get_next(self.continuation_token)
File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/azure/storage/blob/_list_blobs_helper.py", line 175, in _get_next_cb
  process_storage_error(error)
File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/azure/storage/blob/_shared/response_handlers.py", line 186, in process_storage_error
  exec("raise error from None")   # pylint: disable=exec-used # nosec
File "<string>", line 1, in <module>
azure.core.exceptions.ClientAuthenticationError: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

我可以想到这里可能出现问题的一些事情:

  1. 那个
    parquet_names
    对象确实不喜欢被类型转换为
    list
  2. 我正在以构建那个
    credential
    对象的方式做一些愚蠢的事情。 (我已经尝试过
    blob_service_client = BlobServiceClient(account_url, credential=SAS_TOKEN
    。遇到完全相同的错误。)
  3. SAS_TOKEN
    环境变量错误。

对于如何解决这种情况的任何建议,我将非常感激。

python azure
1个回答
0
投票

azure.core.exceptions.ClientAuthenticationError:服务器无法对请求进行身份验证。确保授权标头的值正确形成,包括签名。

由于 SAS 令牌生成或传递到 Azure Blob 存储服务的方式而发生上述错误。

您可以从门户生成

sas
令牌。

传送门:

存储帐户->共享访问签名->检查必填字段->生成SAS令牌。

enter image description here

我使用的是 Windows,因此我使用以下命令来存储来自门户的

sas
令牌。

我同意Gaurav Mantri的评论,

sas
令牌的格式应该是这样的。

 $env:SAS_TOKEN="?sv=2022-11-02&ss=bfqt&srt=sco&sp=rwdlacupiytfx&se=2024-10-30T12:50:48Z&st=2024-10-30T04:50:48Z&spr=https&sig=redacted"

代码和输出:

import os
from azure.core.credentials import AzureSasCredential
from azure.storage.blob import BlobServiceClient

SAS_TOKEN = os.environ.get("SAS_TOKEN")
print(f"SAS_TOKEN: {SAS_TOKEN}")

account_name = "venkat326123" 
account_url = f"https://{account_name}.blob.core.windows.net"
credential = AzureSasCredential(SAS_TOKEN)

try:
    blob_service_client = BlobServiceClient(account_url, credential=credential)
    container_name = "venkat" 
    container = blob_service_client.get_container_client(container_name)
    parquet_names = container.list_blob_names(name_starts_with="Pattern")  
    list_of_parquet = list(parquet_names) 
    print(list_of_parquet)

except Exception as e:
    print(f"An error occurred: {e}")

输出:

SAS_TOKEN: ?sv=2022-11-02&ss=bfqt&srt=sco&sp=rwdlacupiytfx&se=2024-10-30T12:50:48Z&st=2024-10-30T04:50:48Z&spr=https&sig=redacted
['Pattern/data.parquet', 'Pattern/flights-1m.parquet', 'Pattern/mtcars.parquet']

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.