【Azure APIM】调用APIM的备份接口时候遇见InvalidParameters错误 - LuBu0505/My-Code GitHub Wiki

问题描述

根据官方文档,可以调用REST API来对APIM执行备份操作。

要备份 API 管理服务,请发出以下 HTTP 请求:

POST https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/backup?api-version={api-version}

其中:

  • subscriptionId - 订阅的 ID,该订阅包含的 API 管理服务是你尝试备份的
  • resourceGroupName - Azure API 管理服务的资源组名称
  • serviceName - 正在创建其备份的 API 管理服务的名称,在创建时指定
  • api-version - 有效的 REST API 版本,例如 2021-08-01 或 2021-04-01-preview

在请求正文中,指定目标存储帐户名称、Blob 容器名称、备份名称和存储访问类型。 如果存储容器不存在,备份操作将创建存储容器。

*{*
*"storageAccount": "{storage account name for the backup}",*
*"containerName": "{backup container name}",*
*"backupName": "{backup blob name}",*
*"accessKey": "{access key for the account}"*
*}*

但是,总是遇见了如下错误:

{ "error": { "code": "InvalidParameters", "message": "Invalid parameter: This request is not authorized to perform this operation.\r\nParameter name: backupContainerName (value: [backupcontainer])", "details": null, "innerError": null }
}

问题解答

出现错误 “This request is not authorized to perform this operation.\r\nParameter name: backupContainerName”, 说明APIM资源没有权限对Storage Account Container进行读取操作。

需要检查以下几点:

1) 因为请求使用的Access Key方式访问,所以需要检查 Storage Account 是否启用了防火墙,是否允许公网访问? image.png

2) 如果启用了防火墙,则需要根据文档,开启 APIM 的Resource Instance访问,并且选择指定的APIM Managed Identity。 image.png

3) 检查APIM Managed Identity是否有写入Blob的权限,比如需要配置:Storage Blob Data Contributor image.png

4) 在APIM的备份请求体中,需要用 "accessType": "SystemAssignedManagedIdentity" 替换 "accessKey": "{access key for the account}"

只要以上都检查后,就能解决not authorized to perform this operation的错误。

参考资料

如何使用 Azure API 管理中的服务备份和还原实现灾难恢复 : https://docs.azure.cn/zh-cn/api-management/api-management-howto-disaster-recovery-backup-restore?tabs=rest

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!