簡體   English   中英

如何使用 python 在 azure blob 存儲中設置 upload_blob 的內容類型?

[英]How to set content-type for upload_blob in azure blob storage with python?

我有代碼:

import os
from azure.storage.blob import BlobServiceClient, ContentSettings
AZURE = os.getenv('AZURE_STORAGE_CONNECTION_STRING')

blob_service_client = BlobServiceClient.from_connection_string(AZURE)
cnt_settings = ContentSettings(content_type="text/plain")

with open("my_file", 'rb') as f:
    blob_client = blob_service_client.get_blob_client(container="my_container", 
        blob="my_file")
    # I tried:
    # 1. blob_client.set_http_headers(cnt_settings)
    # 2. blob_client.upload_blob(f, **cnt_settings)
    blob_client.upload_blob(f)

我嘗試的兩種情況(1 和 2)都因不同的錯誤而失敗。 設置 content_type 的正確方法是什么?

請嘗試以下方法:

import os
from azure.storage.blob import BlobServiceClient, ContentSettings
AZURE = os.getenv('AZURE_STORAGE_CONNECTION_STRING')

blob_service_client = BlobServiceClient.from_connection_string(AZURE)
cnt_settings = ContentSettings(content_type="text/plain")

with open("my_file", 'rb') as f:
    blob_client = blob_service_client.get_blob_client(container="my_container", blob="my_file")
    blob_client.upload_blob(f, content_settings=cnt_settings)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM