【Azure Service Bus】使用Python SDK创建Service Bus Namespace资源(中国区)

简介: 本文介绍了如何使用Python SDK创建Azure Service Bus Namespace资源。首先,通过Microsoft Entra ID注册应用获取Client ID、Client Secret和Tenant ID,完成中国区Azure认证。接着,初始化ServiceBusManagementClient对象,并调用`begin_create_or_update`方法创建资源。

问题描述

使用Azure云服务,可以通过多种方式来创建资源,如Azure门户, az cli指令, ARM模板,REST API或这各类语言的SDK。

本文将介绍使用Python SDK来创建Service Bus Namespace资源。

注意,Service Bus有两种SDK,一种是面向业务使用,消费/生产消息,使用 azure.servicebus 包;另一种就是对资源本身进行管理,使用 azure.mgmt.servicebus包。

 

问题解答

下面分享Python代码,从使用 Microsoft Entra ID的注册应用开始,获取Client ID, Client Secret及Teanant ID。然后完成中国区Azure的认证,创建 ServiceBusManagementClient 对象。最后,调用 client.namespaces.begin_create_or_update 方法,创建Service Bus Namespace资源。

 

from azure.identity import DefaultAzureCredential,AzureAuthorityHosts ,ClientSecretCredential
from azure.mgmt.servicebus import ServiceBusManagementClient
tenant_id = 'xxxxxxxxxxx'
client_id = 'xxxxxx'
client_secret = 'xxxxxxxx'
"""
# PREREQUISITES
    pip install azure-identity
    pip install azure-mgmt-servicebus
# USAGE
    python sb_name_space_create.py
    Before run the sample, please set the values of the client ID, tenant ID and client secret
    of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
    AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
    https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
    credential = ClientSecretCredential(
        tenant_id=tenant_id,
        client_id=client_id,
        client_secret=client_secret,
        authority=AzureAuthorityHosts.AZURE_CHINA
    )
     
    client = ServiceBusManagementClient(
        credential=credential,
        base_url="https://management.chinacloudapi.cn",
        subscription_id="a9dc7515-7692-4316-9ad4-762f383eec10",
        credential_scopes=["https://management.chinacloudapi.cn/.default"]
    )
    response = client.namespaces.begin_create_or_update(
        resource_group_name="armtest-rg",
        namespace_name="sdk-Namespace2925",
        api_version ="2022-10-01-preview",
        parameters={
            "location": "chinanorth3",
            "sku": {"name": "Standard", "tier": "Standard"},
            "tags": {"tag1": "value1", "tag2": "value2"},
        },
    ).result()
    print(response)
# x-ms-original-file: specification/servicebus/resource-manager/Microsoft.ServiceBus/stable/2021-11-01/examples/NameSpaces/SBNameSpaceCreate.json
if __name__ == "__main__":
    main()

以上代码中,特别需要注意有:

1)ClientSecretCredential 在初始化时,指定 authority=AzureAuthorityHosts.AZURE_CHINA

2)ServiceBusManagementClient 在初始化时,必须指定  base_url="https://management.chinacloudapi.cn" 和 credential_scopes=["https://management.chinacloudapi.cn/.default"]

3)指定 api_version ="2022-10-01-preview"。 需要根据 azure.mgmt.servicebus 中支持的Version来选择,此处指定2022-10-01-preview版本后,创建的Namespace资源默认使用TLS version为1.2。

 

如果在创建的过程中,也遇见的如下的错误,解决办法为:

错误一:azure.core.exceptions.ResourceNotFoundError: (SubscriptionNotFound) The subscription 'xx-x-x-x-xxx' could not be found.

解决办法是:在初始化 Service Bus Management Client对象时,在构造函数中传递 base_url="https://management.chinacloudapi.cn"

 

错误二:azure.core.exceptions.ClientAuthenticationError: Authentication failed: AADSTS500011

错误的完整信息是:azure.core.exceptions.ClientAuthenticationError: Authentication failed: AADSTS500011: The resource principal named https://management.azure.com was not found in the tenant named xxxxxxxx. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You might have sent your authentication request to the wrong tenant.

解决办法是:在初始化 Service Bus Management Client对象时,在构造函数中传递 credential_scopes=["https://management.chinacloudapi.cn/.default"]

 

错误三:创建的Service Bus Namespace的默认TLS Version为1.0

需要在 client.namespaces.begin_create_or_update()方法中指定api_version 参数,指定值为 2022-10-01-preview

 

以上错误会发生在下图画线处:

 

 

 

参考资料

无

 



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

相关文章
|
搜索推荐 API 开发工具
百宝箱开放平台 ✖️ Python SDK
百宝箱提供Python SDK,支持开发者集成其开放能力。需先发布应用,安装Python 3.6+环境后,通过pip安装tboxsdk,即可调用对话型、生成型智能体及文件上传等功能。
1188 87
百宝箱开放平台 ✖️  Python SDK
|
SQL 自然语言处理 数据库
【Azure Developer】分享两段Python代码处理表格(CSV格式)数据 : 根据每列的内容生成SQL语句
本文介绍了使用Python Pandas处理数据收集任务中格式不统一的问题。针对两种情况:服务名对应多人拥有状态(1/0表示),以及服务名与人名重复列的情况,分别采用双层for循环和字典数据结构实现数据转换,最终生成Name对应的Services列表(逗号分隔)。此方法高效解决大量数据的人工处理难题,减少错误并提升效率。文中附带代码示例及执行结果截图,便于理解和实践。
502 5
|
JavaScript 前端开发 机器人
【Azure Bot Service】在中国区Azure上部署机器人的 Python 版配置
本文介绍了在中国区Azure上使用Python SDK配置Azure Bot Service时遇到的问题及解决方案,涵盖参数设置与适配器配置,适用于希望在Azure中国区部署Python机器人的开发者。
382 9
|
存储 监控 API
【Azure App Service】分享使用Python Code获取App Service的服务器日志记录管理配置信息
本文介绍了如何通过Python代码获取App Service中“Web服务器日志记录”的配置状态。借助`azure-mgmt-web` SDK,可通过初始化`WebSiteManagementClient`对象、调用`get_configuration`方法来查看`http_logging_enabled`的值,从而判断日志记录是否启用及存储方式(关闭、存储或文件系统)。示例代码详细展示了实现步骤,并附有执行结果与官方文档参考链接,帮助开发者快速定位和解决问题。
485 22
|
Java 开发工具 Spring
【Azure Application Insights】为Spring Boot应用集成Application Insight SDK
本文以Java Spring Boot项目为例,详细说明如何集成Azure Application Insights SDK以收集和展示日志。内容包括三步配置:1) 在`pom.xml`中添加依赖项`applicationinsights-runtime-attach`和`applicationinsights-core`;2) 在main函数中调用`ApplicationInsights.attach()`;3) 配置`applicationinsights.json`文件。同时提供问题排查建议及自定义日志方法示例,帮助用户顺利集成并使用Application Insights服务。
527 8
|
人工智能 API 开发工具
【AI大模型】使用Python调用DeepSeek的API,原来SDK是调用这个,绝对的一分钟上手和使用
本文详细介绍了如何使用Python调用DeepSeek的API,从申请API-Key到实现代码层对话,手把手教你快速上手。DeepSeek作为领先的AI大模型,提供免费体验机会,帮助开发者探索其语言生成能力。通过简单示例代码与自定义界面开发,展示了API的实际应用,让对接过程在一分钟内轻松完成,为项目开发带来更多可能。
|
XML 算法 测试技术
【资源篇】Python那么火,你还不知道如何人门?
Python 是一种面向对象的解释型计算机程序设计语言。Python具有丰富和强大的库。它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起 。
307 0
【资源篇】Python那么火,你还不知道如何人门?
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1925 102
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
644 104

推荐镜像

更多