首页 > 解决方案 > Python azure.storage.filedatalake - InvalidHeaerValue

问题描述

我正在尝试简单地将 CSV 文件附加到 Azure Datalake 上的容器中,并且我有以下类来执行此操作:

from azure.storage.filedatalake import DataLakeServiceClient
from datetime import datetime


class AzureHandler:

    ###
    # CONSTRUCTOR AzureHandler
    ###
    def __init__(self, storage_account, storage_key):
        connect_string = "DefaultEndpointsProtocol=https;AccountName=" + \
            storage_account + ";AccountKey=" + storage_key + \
            ";EndpointSuffix=core.windows.net"
        self.datalake_service_client = DataLakeServiceClient.from_connection_string(
            conn_str=connect_string)

    def write_tag_csv_file(self, container, folder_name, file_name, data):
        filename_to_write = datetime.today().strftime('%Y%m%d')+'_'+file_name
        file_system_client = self.datalake_service_client.get_file_system_client(
        container)
        directory_client = file_system_client.get_directory_client(folder_name)

        try:
            file_client = directory_client.get_file_client(filename_to_write)
            file_client.get_file_properties().size
            filesize_previous = file_client.get_file_properties().size
            file_client.append_data(
                data, offset=filesize_previous, length=len(data))
            file_client.flush_data(filesize_previous+len(data))
        except:
            file_client = directory_client.create_file(file_name)
            filesize_previous = 0
            file_client.append_data(
                data, offset=filesize_previous, length=len(data))
            file_client.flush_data(filesize_previous+len(data))

但是,每当我可以AzureHandler.write_tag_csv_file时,我都会收到以下一些错误:

Traceback (most recent call last):

File "C:\python39\lib\site-packages\azure\storage\filedatalake\_data_lake_file_client.py", line 450, in append_data
    return self._client.path.append_data(**options)
  File "C:\python39\lib\site-packages\azure\storage\filedatalake\_generated\operations\_path_operations.py", line 1617, in append_data
    raise HttpResponseError(response=response, model=error)
azure.core.exceptions.HttpResponseError: (InvalidHeaderValue) The value for one of the HTTP headers is not in the correct format.

我试着阅读了一下,发现这可能是一个 API 版本问题,所以我通过在声明我的 datalake_service_client 的位置指定它们来尝试所有可用的 API 版本,但它们都给了我同样的错误。我怎样才能解决这个问题?

标签: pythonazureazure-data-lake

解决方案


您的代码似乎正确,我可以运行它而不会出现任何错误,附加操作对.csv文件运行良好。

请尝试在the latest version of ADLS Gen2 package 12.2.3此处安装:azure-storage-file-datalake 12.2.3。您可以使用此命令安装它:pip install azure-storage-file-datalake==12.2.3,并且无需指定api_version您声明我的 datalake_service_client 的位置。

如果您仍有问题,请告诉我。并且还提供了如何调用这个方法的详细代码。


推荐阅读