如何使用 ManagedIdentityCredential 连接到 Azure Blob 存储

How to connect to Azure Blob Storage using ManagedIdentityCredential

目前我有一个简单的代码来使用连接字符串列出 Azure 存储容器上的文件:

String connectStr = "<CONNTECTION_STRING>";
              
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();

BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(<CONTAINER_NAME>);
System.out.println("Listing files in container:");
for (BlobItem blobItem : containerClient.listBlobs()) {
    System.out.println("\t" + blobItem.getName());
}

我正在尝试更新代码以使用 ManagedIdentityCredential 进行身份验证:

ManagedIdentityCredential managedIdentityCredential = new ManagedIdentityCredentialBuilder()
                .clientId(<MANAGED_IDENTITY_CLIENT_ID>)
                .build();

但我不确定如何将 managedIdentityCredential 替换为 BlobServiceClient...

感谢任何帮助,谢谢!

给定 managedIdentityCredential,您可以使用 BlobServiceClientBuilder 中的凭据方法创建 BlobServiceClient 实例:

BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().credential(managedIdentityCredential).buildClient();