要管理存储账户的生命周期策略,需要有“Microsoft.Storage/storageAccounts/blobServices/mgmtPolicy”操作的权限。 以下为示例代码:
// 声明资源组和存储账户名称 string resourceGroupName = "myResourceGroup"; string accountName = "myStorageAccount";
// 获取存储账户对象的身份验证标识 StorageCredentials storageCredentials = new StorageCredentials(accountName, "myStorageAccountKey");
// 获取Blob服务客户端 CloudBlobClient blobClient = new CloudBlobClient(new Uri(string.Format("https://{0}.blob.core.windows.net", accountName)), storageCredentials);
// 访问 Blob 服务管理策略需要“Microsoft.Storage/storageAccounts/blobServices/mgmtPolicy”操作的权限 // 验证用户是否拥有该权限 bool hasPermission = await _authorizationClient.CheckAccessAsync("/subscriptions/{subscriptionId}/resourceGroups/" + resourceGroupName + "/providers/Microsoft.Storage/storageAccounts/" + accountName + "/blobServices/default/mgmtPolicy", "Microsoft.Storage/storageAccounts/blobServices/mgmtPolicy", AuthorizationAction.Write, CancellationToken.None);
if (hasPermission) { // 用户已授权,可以执行生命周期策略管理操作 // ... } else { // 用户未授权,无法执行生命周期策略管理操作,需要提示用户获取权限 }