首页 > 解决方案 > StorageSharedKeyCredential - 无法设置未定义的“accountName”属性

问题描述

我一直在按照教程为文档中的 npm 包“@azure/storage-blob”创建 azure blob 服务客户端。我正在使用另一种方法,即使用存储帐户的帐户名称和密钥来生成“StroageSharedKeyCredential”对象,但在包试图将帐户名称分配给未定义的对象时出现错误。

我从 azure 门户中的“设置”->“访问密钥”选项卡中获取了我的帐户名和密钥,但看不到我哪里出错了。如果有人能指出我应该检查或更改的正确方向,将不胜感激。

我的代码片段试图创建 StorageSharedKeyCredentialObject

const azureAccount = 'some_account_name';
const azureAccountKey = 'some_account_key';
let azureSharedKeyCredential = StorageSharedKeyCredential(azureAccount, azureAccountKey);

下面的片段取自 node_modules/@azure/storage-blob/dist/index.js

    function StorageSharedKeyCredential(accountName, accountKey) {
        var _this = _super.call(this) || this;
        _this.accountName = accountName;//<---- line reporting the undefined object
        _this.accountKey = Buffer.from(accountKey, "base64");
        return _this;
    }

标签: node.jsazurenpm

解决方案


我可以重现您的问题,正如@juunas 所说,您应该使用new StorageSharedKeyCredential(account, accountKey);

尝试下面的代码来让容器中的所有 blob 进行测试,它对我来说非常有效:

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

const account = "<your storage name>";
const accountKey = "<storage key>";
const containerName = "<container name>";

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

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

let i = 1;
let iter = await containerClient.listBlobsFlat();
for await (const blob of iter) {
  console.log(`Blob ${i++}: ${blob.name}`);
}}

main();

结果 :

在此处输入图像描述


推荐阅读