gpt4 book ai didi

node.js - 如何使用 Node.js 中的 azure/identity 模块进行身份验证?

转载 作者:行者123 更新时间:2023-12-03 00:59:16 25 4
gpt4 key购买 nike

我正在使用 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 不同)。另请注意,您不需要 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

关于node.js - 如何使用 Node.js 中的 azure/identity 模块进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63633558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com