Utka
Utka

Reputation: 77

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

I have code:

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)

Both cases (1 and 2) which I tried failed with different errors. What is correct way of setting content_type?

Upvotes: 0

Views: 5450

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136366

Please try something like below:

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)

Upvotes: 4

Related Questions