python - 无法设置 Azure Data Lake 文件的内容类型

标签 python azure azure-data-lake azure-storage-files azure-data-lake-gen2

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

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 的资源。

最佳答案

如果您使用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)

关于python - 无法设置 Azure Data Lake 文件的内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60462808/

相关文章:

c# - 在 Application Insights 中查看请求的响应正文

python - numpy 中的变量赋值不起作用?

c# - 如何使用 Azure Function HTTP 绑定(bind)执行 REST API 可选字符串 URI 参数?

Python 网络服务器 : How to serve requests asynchronously

azure - 如何将每个值从一个资源 block 引用/访问到另一个资源 block terraform

r - 通过 R 访问 Azure Blob 存储

azure - 如何在Azure Data Lake存储上预处理和解压缩.gz文件?

azure - 从 Databricks 访问 AzureDataLake Gen2

python - 我如何一次对两个不同的列求和,其中一个列包含 pandas 中的 Decimal 对象?

python - 如何使字典的键作为合并数据框的索引?