You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Python WebSiteManagementClient 7.0.0:如何通过ID获取Web应用?

如何通过完整资源ID获取Azure Web应用(azure-mgmt-web 7.0.0)

问题场景

我正在使用Python的azure-mgmt-web 7.0.0版本,目前已知可通过以下代码,传入资源组名称和站点名称获取Web应用:

from azure.mgmt.web import WebSiteManagementClient

web_client = WebSiteManagementClient(...credentials here...)
site = web_client.web_apps.get(RESOURCE_GROUP_NAME, SITE_NAME)

但我想了解是否支持通过完整资源ID(格式为/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>/providers/Microsoft.Web/sites/SITE_NAME)获取该资源?我可以自行用Python解析ID,但认为Azure提供的方式会更安全,请问是否有此可用功能?


解决方案

azure-mgmt-web 7.0.0本身没有提供直接通过完整资源ID获取Web应用的API方法,但Azure SDK内置了可靠的资源ID解析工具,无需手动分割字符串,能避免格式解析错误。

具体步骤如下:

  1. 导入Azure SDK的ResourceId类来解析资源ID
  2. 从解析后的结果中提取订阅ID、资源组名称和站点名称
  3. 使用提取出的参数调用web_apps.get()方法获取Web应用

代码示例:

from azure.mgmt.resource.resources.models import ResourceId
from azure.mgmt.web import WebSiteManagementClient

# 你的完整资源ID
resource_id = "/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>/providers/Microsoft.Web/sites/SITE_NAME"

# 解析资源ID
parsed_id = ResourceId.from_string(resource_id)

# 提取所需参数
subscription_id = parsed_id.subscription_id
resource_group_name = parsed_id.resource_group
site_name = parsed_id.name

# 初始化客户端并获取Web应用
web_client = WebSiteManagementClient(your_credentials, subscription_id)
site = web_client.web_apps.get(resource_group_name, site_name)

说明

ResourceId类由Azure官方维护,能处理各种合法的Azure资源ID格式,比手动解析字符串更稳定、安全,不用担心因ID格式变体导致的解析失败。


内容的提问来源于stack exchange,提问作者Roméo Desprès

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 19:47:23