如何使用node.js中的azure/identity module进行身份验证?

如何使用node.js中的azure/identity module进行身份验证?,node.js,azure,azure-sdk-js,Node.js,Azure,Azure Sdk Js,我正在使用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 = Azur

我正在使用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完全相同,但显然我在对身份验证过程的理解中遗漏了一些东西。如何使我的新代码与旧代码相同


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

BlobServiceClient应该像下面那样创建(与您正在使用的容器URI不同)。另外,请注意,您不需要匿名凭证

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
const{BlobServiceClient}=require(“@azure/storage blob”);
const account=“”;
const sas=“”;
const blobServiceClient=新的blobServiceClient(
`https://${account}.blob.core.windows.net${sas}`
);
const containerName='MyContainer';
const containerClient=blobServiceClient.getContainerClient(containerName);
//继续做你的事