node.js - 是否可以使用服务主体来访问 Blob 存储?

标签 node.js azure blob azure-service-principal

我现在尝试使用服务主体来访问 Node 中的 azure blob 存储,而不是使用连接字符串。

我所做的(并且成功了)是使用连接字符串,如下所示:

// connect via connection string
const AZURE_STORAGE_CONNECTION_STRING = process.env.AZURE_STORAGE_CONNECTION_STRING;
const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);

现在我想使用服务主体而不是连接字符串,但我似乎无法使其工作。我可以看到一些使用一些 token 凭据的示例,例如

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

是否可以通过这种方式使用服务主体凭据,或者是否有其他方法可以做到这一点?

最佳答案

试试这个:

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

const account = '<your accounr name>'
//Using  Service Principal
const appID = ""
const appSec = ""
const tenantID = ""

const clientCred = new ClientSecretCredential(tenantID,appID,appSec)

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

//try to list all containers in stroage account to check if success
blobServiceClient.listContainers().byPage().next().then(result =>{
    result.value.containerItems.forEach(element => {
       console.log(element.name);
   });

})

结果:

enter image description here

注意:

在运行此演示之前,请确保您已向服务主体授予所需的权限,详细信息 see this official doc .

关于node.js - 是否可以使用服务主体来访问 Blob 存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66684302/

相关文章:

asp.net - 将asp.net core应用程序部署到azure后出错

javascript - Node.js:无法查找 View

android - 找不到与给定名称匹配的资源 : attr ‘android:keyboardNavigationCluster’

powershell - ADAL 和 Azure 自动化

Azure - web.config 中的 customErrors ="off"未在 Azure 应用程序(云服务)中显示详细错误

javascript - XHR2 文件传输。在 `progress` 事件处理程序期间访问二进制数据

javascript - Angular - 在本地文本文件中保存 blob?

java - BLOB 是哪种类型?

node.js - 使用 TypeORM 将 JSON 数据迁移到数据库

node.js - Node.JS 中的 App.get()