Search code examples
javascriptnode.jsazureazure-blob-storageazure-storage

Upload a image to Microsoft Azure Blob Storage with node js


I am trying to upload a image to my blob storage but it upload text of the file path instead of image


const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");


const account = "<account name hided";
const accountKey = "<key hided>";

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


var containerName = 'democontainer1';

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

    var blobName = "newblob" + new Date().getTime();
    var filePath = "./newblob.jpg";

    const blockBlobClient = containerClient.getBlockBlobClient(blobName);

    const uploadBlobResponse = await blockBlobClient.upload(filePath, filePath.length);
    console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
}

createContainer();

I want like this :

enter image description here

But the output is :

img


Solution

  • Please change the following line of code:

    const uploadBlobResponse = await blockBlobClient.upload(filePath, filePath.length);
    

    to

    const uploadBlobResponse = await blockBlobClient.uploadFile(filePath);
    

    and you should see proper blob being uploaded.