关于这个问题,请参考以下步骤
- 创建服务主体并将
Owner
角色分配给 sp
az login
az ad sp create-for-rbac -n "MyApp" --role "Owner"\
--scopes /subscriptions/{SubID} \
--sdk-auth
- 项目
一种。sdk
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.2.0</version>
</dependency>
湾。代码
AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
String clientId="<sp appid>";
String clientSecret="<sp password>";
String tenant="";
String subscriptionId=""
TokenCredential credential = new ClientSecretCredentialBuilder()
.clientId(clientId)
.clientSecret(clientSecret)
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
.tenantId(tenant)
.build();
AzureResourceManager azureResourceManager = AzureResourceManager
.configure()
.withLogLevel(HttpLogDetailLevel.BASIC)
.authenticate(credential, profile)
.withSubscription(subscriptionId);
// get storage account
String accountGroup="";
String accountName="";
StorageAccount account = azureResourceManager.storageAccounts().getByResourceGroup(accountGroup,accountName);
// get vm
String vmGroup="";
String vmName="test";
VirtualMachine virtualMachine = azureResourceManager.virtualMachines().getByResourceGroup(vmGroup,vmName);
virtualMachine.update()
.withSystemAssignedManagedServiceIdentity()
.withSystemAssignedIdentityBasedAccessTo(account.id(), BuiltInRole.fromString("Storage Blob Data Owner"))
.apply();
}
