node.js - 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;
}
解决方案
我可以重现您的问题,正如@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();
结果 :
推荐阅读
- c++ - 使用intel内在函数加载内存中等距的双精度数?
- reactjs - Gatsby 将目录与页面滚动同步并设置活动链接的样式
- ios - 将 Quicktime (.mov) 从 iPhone 转换为 mp4
- c# - Xamarin.Forms。安卓。无法隐藏键盘
- docker - docker nginx-proxy:如何从一个容器“卷曲”到另一个容器?
- python-3.x - 使用 Prophet 的 Python 时间序列
- java - QueryDsl 投影 ElementCollection
- php - 当您在具有命名空间的类的文件名中添加 .class.php 时是否存在问题?
- git - git mv 一个 bash 脚本,用于重命名特定分支的文件夹,更改所有分支
- wso2 - wso2am-analytics-2.6.0的网页中文显示不正常