【问题标题】:Can't set Content Type of Azure Data Lake File无法设置 Azure Data Lake 文件的内容类型
【发布时间】:2020-02-29 07:10:11
【问题描述】:

我需要将 CSV 文件上传到 Azure Data Lake Gen2 文件系统。在创建 Azure 数据湖文件时,我试图设置它的内容类型,我束手无策。请看下面的代码:

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】:

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

#your other code

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

file_client.flush_data(len(content),content_settings=content_settings)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 2015-03-29
    相关资源
    最近更新 更多