Search code examples
node.jsazurecontent-typeazure-blob-storage

Can't set content type from using uploadFile method in @azure/storage-blob (SDK/NPM)


Can't set azure content type from node using below code. It's always storing content type as octane stream.

const { BlobServiceClient } = require('@azure/storage-blob');

const { AZURE_STORAGE_CONNECTION_STRING } = process.env;

let blobServiceClient;

async function getBlobServiceClient() {
  if (!blobServiceClient) {
    blobServiceClient = await BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);
  }

  return blobServiceClient;
}

async function uploadFile(filePath, containerName) {
  const bsClient = await getBlobServiceClient();
  const containerClient = bsClient.getContainerClient(containerName);
  const blockBlobClient = containerClient.getBlockBlobClient('myImag6.png', { blobHTTPHeaders: { blobContentType: 'image/png' } });

  try {
    const res = await blockBlobClient.uploadFile(filePath);
    console.log(res);
  } catch (error) {
    console.log(error);
  }
}

The following issue seems related to this but I am not sure. https://github.com/Azure/azure-sdk-for-js/issues/6192

Please give me more info on this and how to solve this issue.


Solution

  • Did you try setting the blobHttpHeaders and passed to the uploadFile method?

    const blobOptions = { blobHTTPHeaders: { blobContentType: 'image/png' } };
    const res = await blockBlobClient.uploadFile(filePath, blobOptions);