无法设置Azure Data Lake File的内容类型

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

我需要将CSV文件上传到Azure Data Lake Gen2文件系统。我尽力尝试在创建Azure Data Lake File时设置其内容类型。请参见下面的代码:

from azure.storage.filedatalake import DataLakeServiceClient, ContentSettings

def upload_file_to_directory(category, type, startdatetime, enddatetime, content):
    try:

        service_client = get_service_client()

        file_system_client = service_client.get_file_system_client(file_system="tag-data")

        category_directory_client = file_system_client.get_directory_client(category)

        type_directory_client = category_directory_client.get_sub_directory_client(type)

        year_directory_client = type_directory_client.get_sub_directory_client(startdatetime.strftime("%Y"))

        month_directory_client = year_directory_client.get_sub_directory_client(startdatetime.strftime("%m"))

        day_directory_client = month_directory_client.get_sub_directory_client(startdatetime.strftime("%d"))

        metadata = {"uploadedby": "Casper Alant"}
        content_settings = ContentSettings(content_type = "text/csv")
        file_name = startdatetime.strftime("%Y%m%d%H%M%S") + "-" + enddatetime.strftime("%Y%m%d%H%M%S") + ".csv"

        file_client = day_directory_client.get_file_client(file_name)

        file_client.create_file(content_settings=content_settings, metadata=metadata)

        file_client.append_data(data=content, offset=0, length=len(content))

        file_client.flush_data(len(content))

    except Exception as e:
      print(e)

该文件是用内容创建的,“ uploadedby”元数据已正确设置,但是我无法设置内容类型。

我一直在关注官方文档here。我似乎找不到许多使用此SDK的资源。

python azure azure-data-lake azure-storage-files azure-data-lake-gen2
1个回答
0
投票

如果使用azure-storage-file-datalake 12.0.0b7,则可以在content-type方法中设置flush_data

#your other code

content_settings = ContentSettings(content_type = "text/csv")

file_client.flush_data(len(content),content_settings=content_settings)
© www.soinside.com 2019 - 2024. All rights reserved.