微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Python SDK 创建 Azure 容器时出现“HTTP 标头格式不正确”错误

如何解决使用 Python SDK 创建 Azure 容器时出现“HTTP 标头格式不正确”错误

我正在尝试使用 Python SDK 和以下代码创建 Azure blob 容器。我在响应中收到“ErrorCode:InvalidHeaderValue”。

我正在使用存储帐户 Azure 门户中“访问密钥”部分中的“ConnectionString”。而且我认为连接不是问题,因为这条线可以正常工作 blob_service_client = BlobServiceClient.from_connection_string(connection_string)

我为此使用了一个干净的 venv,下面是库版本 天蓝色核心==1.10.0 azure-storage-blob==12.7.1

import os
import yaml
from azure.storage.blob import ContainerClient,BlobServiceClient

def load_config():
    dir_root = os.path.dirname(os.path.abspath(__file__))
    with open (dir_root + "/config.yaml","r") as yamlfile:
        return yaml.load(yamlfile,Loader=yaml.FullLoader)

config = load_config()
connection_string = config['azure_storage_connectionstring']

blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_service_client.create_container('testing')
Traceback (most recent call last):
  File "/Users/anojshrestha/Documents/codes/gen2lake/project_azure/lib/python3.7/site-packages/azure/storage/blob/_container_client.py",line 292,in create_container
    **kwargs)
  File "/Users/anojshrestha/Documents/codes/gen2lake/project_azure/lib/python3.7/site-packages/azure/storage/blob/_generated/operations/_container_operations.py",line 134,in create
    raise HttpResponseError(response=response,model=error)
azure.core.exceptions.HttpResponseError: Operation returned an invalid status 'The value for one of the HTTP headers is not in the correct format.'

During handling of the above exception,another exception occurred:
.......
azure.core.exceptions.HttpResponseError: The value for one of the HTTP headers is not in the correct format.
RequestId:5X-601e-XXXX00ab-5368-f0c05f000000
Time:2021-01-22T02:43:22.3983063Z
ErrorCode:InvalidHeaderValue
Error:None
HeaderName:x-ms-version
HeaderValue:2020-04-08```

解决方法

您不需要重新安装。您可以通过在实例化任何客户端时设置 api_version 变量来解决此问题。

例如:

blob = BlobServiceClient(
    account_url="https://MY_BLOB_STORAGE.blob.core.windows.net",credential="MY_PRIMARY_KEY",api_version="2019-12-12",#or api_version='2020-02-10'
)

https://github.com/Azure/azure-sdk-for-python/issues/16193

,

正如@kopaczew 所提到的,将 azure_storage_blob 恢复到 12.6.0 版本解决了我的问题。最新的 azure-storage-blob 库似乎确实存在某种错误。不幸的是,上述问题不仅限于 create_container 调用。回答我自己的问题,以防这对处于类似情况的其他人有所帮助。

我如何解决我的问题:

  1. 移除了我之前的venv环境(重新安装本身导致导入azure库时出现一些问题)

  2. 创建了新的 venv

  3. pip install azure-storage-bob==12.6.0

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。