使用Python SDK创建Azure Recovery Service Vault出现Bad Request错误
代码问题定位
- 缺失必填的
properties层级:Azure 恢复服务保管库的创建API要求请求体中必须携带properties字段,哪怕无需自定义存储冗余、安全策略等配置,也需要传入空的properties对象,否则会直接触发400 Bad Request错误。 - 参数传递方式不规范:azure-mgmt-recoveryservices==2.0.0版本优先推荐使用官方封装的模型类传递参数,可避免字典拼写错误、结构不匹配的问题。
修正后的代码示例
from azure.mgmt.recoveryservices import RecoveryServicesClient from azure.mgmt.recoveryservices.models import Vault, Sku, IdentityData from azure.identity import ClientSecretCredential # 凭证初始化逻辑保持不变 client = RecoveryServicesClient(client_secret_credential, subscription_id) poller = client.vaults.begin_create_or_update( resource_group_name="my-custom-rg", vault_name="name_of_the_vault", vault=Vault( location="centralus", sku=Sku(name="Standard"), identity=IdentityData(type="SystemAssigned"), properties={} # 必填层级,可按需添加存储类型、加密配置等参数 ) ) # 等待创建完成 vault_info = poller.result()
通用错误排查步骤
- 打印完整错误详情:
HttpResponseError内置了Azure返回的具体错误信息,捕获异常后可直接打印精准定位问题:
from azure.core.exceptions import HttpResponseError try: # 你的创建逻辑 poller = client.vaults.begin_create_or_update(...) poller.result() except HttpResponseError as e: print(f"错误码:{e.error.code}") print(f"错误描述:{e.error.message}") # 打印详细错误项 if e.error.details: for item in e.error.details: print(f"子错误:{item.code} - {item.message}")
- 验证参数合法性:
- 保管库名称仅可包含小写字母、数字、连字符,首尾必须为字母数字,长度在2-50位之间
- 指定的
location区域支持创建恢复服务保管库 - SKU取值合规,可选值为
Standard(标准保管库)、RS0(跨区域恢复专用保管库)
- 验证权限:确认当前使用的服务主体对目标资源组有
Microsoft.RecoveryServices/vaults/write权限 - 检查资源组状态:确认指定的资源组存在且状态正常
内容的提问来源于stack exchange,提问作者Robert
相关产品推荐
相关产品推荐

