将承载令牌与azure-sdk-for-js一起使用

问题描述 投票:0回答:1

我们正在构建nodejs服务器,该服务器使用AAD对用户进行身份验证。当用户登录到我们的应用程序时,我们从Microsoft登录端点获得JWT accessToken

我们如何使用此令牌通过此javascript API进行调用以获取斑点/容器?我不想使用([Authorization: Bearer accessToken)调用直接向API发送ajax请求。

我已经成功使用像这样的邮递员打了电话?如何使用[blobServiceClient以编程方式进行此操作?

enter image description here

azure azure-active-directory azure-storage azure-storage-blobs azure-container-service
1个回答
0
投票

根据我的研究,如果我们使用V10版本SDK @ azure / storage-blob,我们可以直接使用Azure AD访问令牌来管理Azure Blob服务。因为sdk提供了类TokenCredential。我们可以使用代码const tokenCredential = new azure.TokenCredential("token")初始化凭据,然后使用它获取Blob。

例如

const azure = require("@azure/storage-blob"); 

async function getBlobContent(){

    const tokenCredential = new azure.TokenCredential("")
    const pipeline =  azure.StorageURL.newPipeline(tokenCredential)
    const serviceURL = new azure.ServiceURL(`https://jimtestperfdiag516.blob.core.windows.net`, pipeline);
    const containerURL = azure.ContainerURL.fromServiceURL(serviceURL, "test");
    const blockBlobURL = azure.BlockBlobURL.fromContainerURL(containerURL, "test.csv");
    const aborter=azure.Aborter.timeout(30* 60 * 1000)
    const downloadResponse = await blockBlobURL.download(aborter, 0);
    const downloadedContent = await streamToString(downloadResponse.readableStreamBody);
    console.log(`Downloaded blob content: "${downloadedContent}"`);



}

async function streamToString(readableStream) {
    return new Promise((resolve, reject) => {
      const chunks = [];
      readableStream.on("data", data => {
        chunks.push(data.toString());
      });
      readableStream.on("end", () => {
        resolve(chunks.join(""));
      });
      readableStream.on("error", reject);
    });
}

getBlobContent()
  .then(() => {
    console.log("Successfully executed sample.");
  })
  .catch((err) => {
    console.log(err.message);
  });

有关更多详细信息,请参阅https://www.npmjs.com/package/@azure/storage-blob/v/10.5.0https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-nodejs-legacy

此外,如果要使用Azure AD访问Azure Blob,我们需要将RABS角色(存储Blob数据所有者,存储Blob数据贡献者或存储Blob数据读取器)分配给用户或服务主体:https://docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad

© www.soinside.com 2019 - 2024. All rights reserved.