Azure中如何将API连接从源资源组复制到备份资源组
复制Azure资源组API连接至备份组的实现方案
问题背景
需要将源资源组中的200+个API连接复制到目标资源组作为备份,但现有代码仅能实现资源移动(会移除源资源),且已调整为分批20个处理的逻辑,仍未实现复制功能。尝试调整代码仅得到update方法的无效建议。原移动代码如下:
from azure.identity import ClientSecretCredential from azure.mgmt.resource import ResourceManagementClient # Define your subscription ID and resource group names subscription_id = [REDACTED] tenant_id = [REDACTED] client_id = [REDACTED] client_secret = [REDACTED] # Authenticate using Azure Identity credential = ClientSecretCredential(tenant_id, client_id, client_secret) resource_client = ResourceManagementClient(credential, subscription_id) # Resource groups # API Connections must be retained in source_api resource_group_name_source = "azdnaeurgrgp1dev_source_api" resource_group_name_target = "azmgmteurgrgp_backup_api" # Get all resources in the source resource group resources = list(resource_client.resources.list_by_resource_group( resource_group_name_source, filter="resourceType eq 'Microsoft.Web/connections'" )) # Debug: Print the resources retrieved for resource in resources: print(resource.id) # Prepare the target resource group ID target_resource_group_id = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name_target}" # Move resources in batches of 20 for i in range(0, len(resources), 20): batch_resources = resources[i:i+20] resource_ids = [resource.id for resource in batch_resources] # Move resources to the target resource group move_resources_poller = resource_client.resources.begin_move_resources( resource_group_name_source, { "targetResourceGroup": target_resource_group_id, "resources": resource_ids } ) # Wait for the move operation to complete move_resources_poller.result() print(f"Moved {len(resource_ids)} resources.") print("All resources moved successfully to", resource_group_name_target)
解决方案
要实现API连接的复制,需读取源资源的完整配置,在目标资源组重新创建同名资源。以下是修改后的分批复制代码:
from azure.identity import ClientSecretCredential from azure.mgmt.resource import ResourceManagementClient # 配置Azure认证信息 subscription_id = [REDACTED] tenant_id = [REDACTED] client_id = [REDACTED] client_secret = [REDACTED] # 初始化认证与资源管理客户端 credential = ClientSecretCredential(tenant_id, client_id, client_secret) resource_client = ResourceManagementClient(credential, subscription_id) # 源与目标资源组名称 resource_group_name_source = "azdnaeurgrgp1dev_source_api" resource_group_name_target = "azmgmteurgrgp_backup_api" # 获取源资源组中所有API连接 resources = list(resource_client.resources.list_by_resource_group( resource_group_name_source, filter="resourceType eq 'Microsoft.Web/connections'" )) # 分批处理,每批20个 for i in range(0, len(resources), 20): batch_resources = resources[i:i+20] copied_count = 0 for resource in batch_resources: # 获取源资源的完整配置(包含properties、sku等核心参数) source_resource = resource_client.resources.get( resource_group_name_source, resource.name, resource.resource_type ) # 构造目标资源的定义,复用源资源的所有属性 resource_definition = { "location": source_resource.location, "sku": source_resource.sku.as_dict() if source_resource.sku else None, "properties": source_resource.properties.as_dict(), "tags": source_resource.tags } # 在目标资源组创建/更新API连接 create_poller = resource_client.resources.begin_create_or_update( resource_group_name_target, resource.name, resource.resource_type, resource_definition ) # 等待创建完成 create_poller.result() copied_count += 1 print(f"已复制 {copied_count} 个资源至目标资源组") print("所有API连接复制完成")
关键说明
- 使用
resource_client.resources.get获取源资源的完整配置,确保复制的资源与源资源属性一致 - 通过
begin_create_or_update方法实现资源创建,兼容目标组已有同名资源的更新场景 - 保留分批20个的处理逻辑,避免单次操作触发Azure API调用限制或超时
注意事项
- API连接复制完成后,部分连接需要重新进行身份验证(敏感凭据不会被自动复制)
- API连接不支持跨区域复制,若目标资源组与源组区域不同,需修改
location参数并重新配置连接
内容的提问来源于stack exchange,提问作者Nathanael Tom Aterado
相关产品推荐
相关产品推荐

