【问题标题】:SharedKeyCredential is not a constructor - Azure Blob Storage + NodejsSharedKeyCredential 不是构造函数 - Azure Blob Storage + Nodejs
【发布时间】:2020-03-27 08:55:49
【问题描述】:

我正在尝试删除我的 aucitonImages 容器中的图像,但是当我从邮递员执行该功能时,我得到 SharedKeyCredential is not a constructor 我一直在关注 documentation 并且我认为我已经设置了所有内容,但我没有'看不出我的代码与文档有什么不同。感谢您的帮助!

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

});

【问题讨论】:

  • 您使用的是什么版本的 Azure 存储节点 SDK?
  • "@azure/storage-blob": "^12.1.0"

标签: node.js azure azure-storage azure-blob-storage


【解决方案1】:

根据 SDK 版本 12.1.0 文档 here,微软似乎将 SharedKeyCredential 更改为 StorageSharedKeyCredential

你可以试试吗?

另外,请在此处查看此版本 SDK 的示例:https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/storage/storage-blob/samples/javascript


这是我使用 Node SDK v12.1.0 编写的代码:

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

【讨论】: