How to automatically set Brotli encoding in Azure Blob storage?

107 Views Asked by At

Using the Azure Portal blob storage file upload via browser, how do I make it so the system automatically detects a file uses Brotli compression and sets 'br' as encoding type?

For example, if I upload a .js file using the Azure Portal file upload in-browser, the file's content type is automatically set to "application/x-javascript".

If I upload a file with .br extension, is there a way the Azure storage could detect it is a "brotli" compressed file and thus set the content-encoding for that file to "br" automatically? Right now I have to do this manually on every file.

1

There are 1 best solutions below

0
Venkatesan On

If you upload a file with a .br extension, is there a way for Azure storage to detect it as a "brotli" compressed file and automatically set the content-encoding for that file to "br"?

Azure will not automatically add your content-encoding. If you need to upload the blob with content-encoding set to br, you can use the following Python code, which will upload the .br extension file and set the content-encoding.

Code:

import brotli
from azure.storage.blob import BlobServiceClient, ContentSettings

blob_service_client = BlobServiceClient.from_connection_string("xxxx")
container_client = blob_service_client.get_container_client("block-image")
blob_client = container_client.get_blob_client("sample.br")

with open(r"path of file") as f:
    data = f.read()A
    compressed_data = brotli.compress(data)

# Upload the compressed file to Azure Blob Storage
blob_client.upload_blob(compressed_data, blob_type="BlockBlob", content_settings=ContentSettings(content_encoding="br"))

The above code was executed and uploaded file to the Azure blob storage by setting the content encoding.

Output: enter image description here

Reference: