【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

 

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

 

 

 

参考资料

 



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

相关文章
|
7月前
|
搜索推荐 API 开发工具
百宝箱开放平台 ✖️ Python SDK
百宝箱提供Python SDK,支持开发者集成其开放能力。需先发布应用,安装Python 3.6+环境后,通过pip安装tboxsdk,即可调用对话型、生成型智能体及文件上传等功能。
920 87
百宝箱开放平台 ✖️  Python SDK
|
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服务。
365 8
|
人工智能 API 开发工具
【AI大模型】使用Python调用DeepSeek的API,原来SDK是调用这个,绝对的一分钟上手和使用
本文详细介绍了如何使用Python调用DeepSeek的API,从申请API-Key到实现代码层对话,手把手教你快速上手。DeepSeek作为领先的AI大模型,提供免费体验机会,帮助开发者探索其语言生成能力。通过简单示例代码与自定义界面开发,展示了API的实际应用,让对接过程在一分钟内轻松完成,为项目开发带来更多可能。
|
Python
第22天:Python NameSpace & Scope
第22天:Python NameSpace & Scope
268 0
第22天:Python NameSpace & Scope
|
8月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1244 102
|
8月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
458 104
|
8月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
355 103
|
8月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
364 82
|
7月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
460 3
|
7月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
680 3

推荐镜像

更多