首页 > 解决方案 > 通过 Databricks 上传到 Azure Blob 存储时设置内容类型

问题描述

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

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

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

有没有办法设置这个使用databricks

标签: htmlpysparkdatabricksazure-databricksazure-blob-storage

解决方案


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

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

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

访问 Azure 存储帐户中的位置

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"
        )
    )

推荐阅读