SharedKeyCredential is not a constructor - Azure Blob Storage + Nodejs

六眼飞鱼酱① 提交于 2020-05-30 08:41:33

问题


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`);

});

回答1:


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();


来源:https://stackoverflow.com/questions/60873972/sharedkeycredential-is-not-a-constructor-azure-blob-storage-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!