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

SharedKeyCredential is not a constructor - Azure Blob Storage + Nodejs


I'm trying to delete an image in my aucitonImages container, but when I execute the function from postman, I get SharedKeyCredential is not a constructor I've been following the documentation and I think I have everything setup, but I don't see what's different in my code from the docs. I appreciate any help!

app.delete("/api/removeauctionimages", upload, async (req, res, next) => {
  const { ContainerURL, ServiceURL, StorageURL, SharedKeyCredential } = require("@azure/storage-blob");
  const credentials = new SharedKeyCredential(process.env.AZURE_STORAGE_ACCOUNT, process.env.AZURE_STORAGE_ACCESS_KEY);
  const pipeline = StorageURL.newPipeline(credentials);
  const serviceURL = new ServiceURL(`https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`, pipeline);
  const containerName = "auctionImages";
  const blobName = "myimage.png";



  const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
  const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);
  await blockBlobURL.delete(aborter)
  console.log(`Block blob "${blobName}" is deleted`);

});

Solution

  • Based on the SDK Version 12.1.0 documentation here, looks like Microsoft changed SharedKeyCredential to StorageSharedKeyCredential.

    Can you try with that?

    Also, please see the samples for this version of SDK here: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/storage/storage-blob/samples/javascript.


    Here's the code I wrote using v12.1.0 of Node SDK:

    const { StorageSharedKeyCredential, BlobServiceClient } = require("@azure/storage-blob");
    const sharedKeyCredential = new StorageSharedKeyCredential(process.env.AZURE_STORAGE_ACCOUNT, process.env.AZURE_STORAGE_ACCESS_KEY);
    
    
    const blobServiceClient = new BlobServiceClient(
      `https://${process.env.AZURE_STORAGE_ACCOUNT}.blob.core.windows.net`,
      sharedKeyCredential
    );
    
    const containerName = `temp`;
    const blobName = 'test.png';
    const containerClient = blobServiceClient.getContainerClient(containerName);
    const blockBlobClient = containerClient.getBlockBlobClient(blobName);
    await blockBlobClient.delete();