gpt4 book ai didi

c# - 如何将 Azure 托管标识与 Azure.Storage.Blobs.BlobServiceClient 结合使用?

转载 作者:行者123 更新时间:2023-12-03 00:47:18 28 4
gpt4 key购买 nike

v11 SDK for .NET我能够使用托管身份 token 来访问 Azure blob:

var token = await new AzureServiceTokenProvider().GetAccessTokenAsync("https://storage.azure.com/");
var tokenCredential = new TokenCredential(token);
var storageCredentials = new StorageCredentials(tokenCredential);
var blob = new CloudBlobContainer(new Uri("https://some_storage.blob.core.windows.net/some_container"), storageCredentials);

现在我想切换到v12 SDK并且无法理解如何对 BlobServiceClient 执行相同的操作。

最佳答案

我有一个样本:https://github.com/juunas11/managedidentity-filesharing/blob/8410ed3f3d4061de7d40531c025bf6e474489135/Joonasw.ManagedIdentityFileSharingDemo/Services/AzureBlobStorageService.cs#L80

以下是它如何与 Azure.Identity 一起使用:

client = new BlobServiceClient(
new Uri($"https://{_options.AccountName}.blob.core.windows.net"),
new ManagedIdentityCredential());

如果您需要在本地运行 Azure 存储帐户,则可以使用自定义 TokenCredential,如下所示:https://github.com/juunas11/Joonasw.ManagedIdentityDemos/blob/3501ee6fff416db7349807e588532da5c3dd24b1/Joonasw.ManagedIdentityDemos/Services/DemoService.cs#L45 .

自定义 token 凭据:https://github.com/juunas11/Joonasw.ManagedIdentityDemos/blob/master/Joonasw.ManagedIdentityDemos/Services/ManagedIdentityStorageTokenCredential.cs .

针对存储模拟器的使用:

client = new BlobServiceClient("UseDevelopmentStorage=true");

关于c# - 如何将 Azure 托管标识与 Azure.Storage.Blobs.BlobServiceClient 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60222516/

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