连接到 Azure 容器时出错

Posted

技术标签:

【中文标题】连接到 Azure 容器时出错【英文标题】:Errors while connecting to Azure Container 【发布时间】:2021-11-12 07:58:26 【问题描述】:

我正在尝试连接到 azure 容器并保存一个文本文件

  const azure = require('azure-storage');
    const BlobServiceClient = azure.createBlobService();
    
    const AZURE_STORAGE_CONNECTION_STRING = "my key";
    
    const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);
    
    
    const containerName = "tempp";
    
    console.log('\nCreating container...');
    console.log('\t', containerName);
    
    // Get a reference to a container
    const containerClient = blobServiceClient.getContainerClient(containerName);

const blobName = 'test' + uuidv1() + '.txt';

// Get a block blob client
const blockBlobClient = containerClient.getBlockBlobClient(blobName);

console.log('\nUploading to Azure storage as blob:\n\t', blobName);



const data = "message";
const uploadBlobResponse =   blockBlobClient.upload(data, data.length);


console.log("Blob was uploaded successfully. requestId: ", uploadBlobResponse.requestId);

我按照文档从 /security + networking/Access Keys 获得了 azure_storage 连接字符串,但是在运行项目时出现错误

Error: Credentials must be provided when creating a service client.

【问题讨论】:

我认为您将旧的 SDK 代码 (azure-storage) 与新的 SDK 代码 (@azure/storage-blob) 混合在一起。 【参考方案1】:

正如Gaurav Mantri 所建议的那样,我尝试在我的本地进行复制,并能够将文件部署到我的 Azure 容器中。

确保您已安装以下nuget packages

Azure.Storage.Blobs(12.10.0) 最新 sdk、Microsoft.Extensions.ConfigurationMicrosoft.Extensions.Configuration.Json

这是我将文件上传到容器的 Open source code。

更多信息请参考MS DOC for.net & MS DOC for Node.js。

【讨论】:

当您的代码使用 .NET 时,OP 询问了 node.js。 谢谢@Jeremy Meng。我已经更新了 Node.js 提供的链接。

以上是关于连接到 Azure 容器时出错的主要内容,如果未能解决你的问题,请参考以下文章