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

mtk*_*nko 1 c# azure azure-blob-storage

v11 SDK for .NET中,我能够使用托管身份令牌来访问 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);
Run Code Online (Sandbox Code Playgroud)

现在我想切换到v12 SDK,但不明白如何对BlobServiceClient.

juu*_*nas 5

我有一个示例:https://github.com/juunas11/managementidentity-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());
Run Code Online (Sandbox Code Playgroud)

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

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

针对存储模拟器的用法:

client = new BlobServiceClient("UseDevelopmentStorage=true");
Run Code Online (Sandbox Code Playgroud)