【问题标题】:How do I authenticate with azure/identity module in node.js?如何使用 node.js 中的 azure/identity 模块进行身份验证?
【发布时间】:2020-12-17 08:59:14
【问题描述】:

我正在使用 node.js 应用程序(v12.18.2,不在浏览器中)访问 Azure blob 存储。我使用 @azure/storage-blob v10.5.0 的现有代码正在运行,身份验证代码如下所示:

const Azure = require( '@azure/storage-blob' );
let containerUriWithSAS = `${credentials.container_uri}?${credentials.sas_token}`;
let pipeline = Azure.StorageURL.newPipeline( new Azure.AnonymousCredential() );
let ContainerURL = new Azure.ContainerURL( containerUriWithSAS, pipeline );

使用此代码进行身份验证,然后使用例如ContainerURL.listBlobFlatSegment() 列出对象非常有效。我可以创建、获取、删除和列出对象。

当我升级到 @azure/storage-blob v12.1.2 时,发生了一些重大变化。现在我的代码看起来像:

//const{ DefaultAzureCredential } = require( '@azure/identity' ); // tried this instead of AnonymousCredential
const{ BlobServiceClient, AnonymousCredential } = require( '@azure/storage-blob' );
let containerUriWithSAS = `${credentials.container_uri}?${credentials.sas_token}`;
//let defaultAzureCredential = new DefaultAzureCredential();
let anonymousCredential = new AnonymousCredential();
let blobServiceClient = new BlobServiceClient( containerUriWithSAS, anonymousCredential );

const containerName = 'MyContainer';
const containerClient = blobServiceClient.getContainerClient( containerName );
const createContainerResponse = await containerClient.create();

在一台 (Linux) 机器上,我根本无法连接到服务器(create() 调用超时)。在另一个(Windows)上,create() 调用会抛出一个错误,告诉我“请求的 URI 不代表服务器上的任何资源”。

我已验证 URI 与工作代码使用的 URI 完全相同,但显然我在理解身份验证过程时遗漏了一些东西。如何让我的新代码完成旧代码的工作?

另外,我似乎必须先创建一个容器才能创建对象,而我以前不必这样做。这是我困惑的一部分吗?

【问题讨论】:

    标签: node.js azure azure-sdk-js


    【解决方案1】:

    BlobServiceClient 应该像下面这样创建(不像你正在做的容器 URI)。另外,请注意您不需要 AnonymousCredential。

    const { BlobServiceClient } = require("@azure/storage-blob");
     
    const account = "<account name>";
    const sas = "<service Shared Access Signature Token>";
     
    const blobServiceClient = new BlobServiceClient(
      `https://${account}.blob.core.windows.net${sas}`
    );
    
    const containerName = 'MyContainer';
    const containerClient = blobServiceClient.getContainerClient(containerName);
    
    // and go on doing your stuffs
    

    【讨论】:

      猜你喜欢
      • 2017-03-24
      • 1970-01-01
      • 2013-03-31
      • 2016-10-21
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      相关资源
      最近更新 更多