downloaded blob file encoding not reflecting correctly

Abhishake Jaiswal 160 Reputation points
2023-09-27T12:29:17.8166667+00:00

we are writing a solution to upload email attachments to blob Storage using Azure Function app.

Below is code for uploading the csv file to Storage.

        const attachment_filebuff = Buffer.from(attachmentContentString,'base64');
        const decoded_content = new Buffer(attachment_filebuff);
        
        //Blob connection details
        const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME;
        if (!accountName) throw Error('Azure Storage accountName not found');

        const blobServiceClient = new BlobServiceClient(`https://${accountName}.blob.core.windows.net`,new DefaultAzureCredential());
        // create container client
        const containerClient = await blobServiceClient.getContainerClient(containerName);
        // Push data to decoded storage
        
        const blkBlobClt = containerClient.getBlockBlobClient(mainBlobpath);
        const blobOptions = { blobHTTPHeaders: { blobContentType: 'text/plain', blobContentEncoding: 'UTF-8' } };
        await blkBlobClt.uploadData(decoded_content,blobOptions);

but when we download the uploaded file, and open it in notepad++

its encoding is coming as UCS-2 LE BOM instead of UTF-8

could anyone advise what am i missing here ?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,010 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,864 questions
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,336 Reputation points
    2023-09-29T12:14:49.0666667+00:00

    Abhishake Jaiswal Thanks for your engagement and sharing your solution in the community. Since the Microsoft Q&A community has a policy that the question author cannot accept their own answer, they can only accept answers by others, I'll repost your solution in case you'd like to Accept the answer.

    Solution:

    Converted Buffer to utf8 (refer https://nodejs.org/api/buffer.html) and also, added an additional logic to remove extra 00 in hex before uploading to azure storage resolved the issue.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.