python - 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 版本,但它们都给了我同样的错误。我怎样才能解决这个问题?
解决方案
您的代码似乎正确,我可以运行它而不会出现任何错误,附加操作对.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 的位置。
如果您仍有问题,请告诉我。并且还提供了如何调用这个方法的详细代码。
推荐阅读
- postman - 某些语言在邮递员中不可用;有没有办法启用它?
- mysql - 不应该返回任何东西的 MySQL 查询,返回一条记录
- java - 什么时候可以增加 jvm (-Xss) 的最大堆栈大小?
- c++ - 不能将结构分配给向量的索引吗?
- twilio - 在房间里从拨号盘收集数字
- bash - 如果“特定”列的行匹配,则比较“特定”列的行并连接其他列
- c# - Selenium 问题关闭弹出窗口
- flutter - 覆盖 operator== 是否有助于避免重建?
- python - 如何在 Python 中结合以下理解?
- python - 如何在 docker(Azure Web App)中为 Python 导入 cv2?