【问题标题】:Set content type when uploading to Azure Blob Storage via Databricks通过 Databricks 上传到 Azure Blob 存储时设置内容类型
【发布时间】:2021-04-18 20:12:54
【问题描述】:

我正在使用databricks 平台上传一个静态站点,专门使用以下命令将html 内容推送到某个位置。

dbutils.fs.put("/mnt/$web/index.html", html, overwrite=True)

这是可行的,但 HTML 文件正在下载而不是显示。这是因为内容类型错误:Content-Type:application/octet-stream

有没有办法使用databricks 来设置它?

【问题讨论】:

    标签: html pyspark databricks azure-databricks azure-blob-storage


    【解决方案1】:

    最后,这段代码对我有用。首先,我从 databricks 范围获取连接字符串为

    dbutils.secrets.get(scope = "generic-scope", key = "website-key") 
    

    如果您没有,请在存储帐户的容器访问密钥中查找它

    from azure.storage.blob import BlobServiceClient, ContentSettings
    connect_str="connectionString"
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)
    
    # Instantiate a ContainerClient
    container_client = blob_service_client.get_container_client("$web")
    
    # List files in blob folder
    blobs_list = container_client.list_blobs()
    for blob in blobs_list:
        print(blob.content_settings.content_type) # application/octet-stream
        blob.set_http_headers(
        content_settings=ContentSettings(
            content_type="text/html; charset=utf-8"
            )
        )
    

    【讨论】:

      【解决方案2】:

      dbutils.fs.put 与 DBFS 上的文件一起使用,并且不“了解”底层实现细节,因为您可以挂载不同的东西 - S3、ADLSv1/v2 等。内容类型的更改特定于 blob 存储API,因此您需要在 Python(example)或 Scala 中实现代码,使用该 API 为上传的文件设置内容类型,或通过 API 上传文件和设置内容类型,无需dbutils.fs.put

      【讨论】:

        猜你喜欢
        • 2020-05-02
        • 2021-09-17
        • 2016-11-11
        • 1970-01-01
        • 2021-04-14
        • 1970-01-01
        • 2021-02-27
        • 2013-03-27
        • 2014-08-23
        相关资源
        最近更新 更多