HoverCraft
HoverCraft

Reputation: 111

Set content type to image in JavaScript v10 SDK Nodejs

When I try to upload images using the Azure JS SDK v10, it shows them as application/octetstream.

This is what I have done initially according to the official azure documentation

 async function uploadLocalFile(
    aborter,
    containerClient,
    filePath,
    file_guid,
  )

    filePath = path.resolve(filePath);  //Image file path
    const fileName = file_guid; //contains file name
    const blobClient = containerClient.getBlobClient(fileName);
    const blockBlobClient = blobClient.getBlockBlobClient();

    
    return await blockBlobClient.uploadFile(filePath, aborter);

  }

This was uploading the images s application/octetstream

Then I tried this, setting the headers and attempting to make it as image/jpeg, but still this makes the content type as application/octetstream.


    filePath = path.resolve(filePath);
    const fileName = file_guid;
    const blobClient = containerClient.getBlobClient(fileName);
    const blockBlobClient = blobClient.getBlockBlobClient();

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

  }

Is there any way to make the images' content type as image/jpeg while uploading to azure blob storage?

Upvotes: 0

Views: 582

Answers (1)

Joey Cai
Joey Cai

Reputation: 20067

I test with your code and set httpHeaderOptions, you could refer to this interface description:BlobHTTPHeaders, below is my test code.

const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
const path = require("path");
// Enter your storage account name and shared key
const account = "xxxx";
const accountKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";

const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  sharedKeyCredential
);

const containerName = "container";

async function main() {
     const containerClient = blobServiceClient.getContainerClient(containerName);

    const filePath = path.resolve("C:\\xxxxx\\me.jpg");
    const fileName = "bbb";
    const blobClient = containerClient.getBlobClient(fileName);
    const blockBlobClient = blobClient.getBlockBlobClient();
    const blobOptions = { blobHTTPHeaders: { blobContentType: 'image/jpeg' } };
    const uploadBlobResponse =await blockBlobClient.uploadFile(filePath,blobOptions);
    
    console.log(`Upload block blob test.txt successfully`);
}

main();

enter image description here

Upvotes: 2

Related Questions