使用 Azure SDK for Python 创建 Python 应用

本文档以示例方式演示了如何使用 Azure SDK for Python 访问 Azure 应用配置中的数据。

提示

应用配置提供基于 Python SDK 构建的 Python 提供程序库,旨在通过更丰富的功能来方便用户使用。 它使配置设置能够像字典一样使用,并提供其他功能,例如从多个标签进行配置组合、键名称剪裁和自动解析 Key Vault 引用的功能。 若要了解详细信息,请转到 Python 快速入门

先决条件

创建键值

将以下键值添加到应用程序配置存储区,并让“标签”和“内容类型”保留默认值。 有关如何使用 Azure 门户或 CLI 将键值添加到存储区的详细信息,请转到创建键值

密钥
TestApp:Settings:Message Azure 应用配置的数据

设置 Python 应用

  1. 为名为 app-configuration-example 的项目创建一个新目录。

    mkdir app-configuration-example
    
  2. 切换到新创建的 app-configuration-example 目录。

    cd app-configuration-example
    
  3. 使用 pip install 命令安装 Azure 应用程序配置客户端库。

    pip install azure-appconfiguration
    
  4. 在 app-configuration-example 目录中,创建名为 app-configuration-example.py 的新文件,并添加以下代码:

    import os
    from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting
    
    try:
        print("Azure App Configuration - Python example")
        # Example code goes here
    except Exception as ex:
        print('Exception:')
        print(ex)
    

注意

本示例中的代码片段将帮助你开始使用适用于 Python 的应用配置客户端库。 对于应用程序,你还应考虑根据需要处理异常。 若要详细了解异常处理,请参阅 Python SDK 文档

配置应用程序配置连接字符串

  1. 设置名为 AZURE_APPCONFIG_CONNECTION_STRING 的环境变量,并将其设置为应用程序配置存储的连接字符串。 在命令行中运行以下命令:

    要使用 Windows 命令提示符在本地运行应用,请运行以下命令并将 <app-configuration-store-connection-string> 替换为你的应用程序配置存储的连接字符串:

    setx AZURE_APPCONFIG_CONNECTION_STRING "connection-string-of-your-app-configuration-store"
    
  2. 使用以下命令输出环境变量的值,以验证其设置是否正确。

    使用 Windows 命令提示符时,重启命令提示符使更改生效,然后运行以下命令:

    echo %AZURE_APPCONFIG_CONNECTION_STRING%
    

代码示例

本部分中的示例代码片段演示如何通过适用于 Python 的应用程序配置客户端库执行常见操作。 将这些代码片段添加到之前创建的 app-configuration-example.py 文件中的 try 块。

注意

应用程序配置客户端库将键-值对象称为 ConfigurationSetting。 因此在本文中,应用程序配置存储区中的“键-值”将被称为为“配置设置” 。

参阅以下文章了解相关操作:

连接到应用程序配置存储区

以下代码片段使用环境变量中存储的连接字符串创建 AzureAppConfigurationClient 实例。

    connection_string = os.getenv('AZURE_APPCONFIG_CONNECTION_STRING')
    app_config_client = AzureAppConfigurationClient.from_connection_string(connection_string)

获取配置设置

以下代码片段按 key 名称检索配置设置。

    retrieved_config_setting = app_config_client.get_configuration_setting(key='TestApp:Settings:Message')
    print("\nRetrieved configuration setting:")
    print("Key: " + retrieved_config_setting.key + ", Value: " + retrieved_config_setting.value)

添加配置设置

以下代码片段使用 keyvalue 字段创建 ConfigurationSetting 对象,并调用 add_configuration_setting 方法。 如果尝试添加存储区中已存在的配置设置,此方法将引发异常。 如果要避免此异常,可以改为使用 set_configuration_setting 方法。

    config_setting = ConfigurationSetting(
        key='TestApp:Settings:NewSetting',
        value='New setting value'
    )
    added_config_setting = app_config_client.add_configuration_setting(config_setting)
    print("\nAdded configuration setting:")
    print("Key: " + added_config_setting.key + ", Value: " + added_config_setting.value)

获取配置设置列表

以下代码片段将检索配置设置列表。 可提供 key_filterlabel_filter 参数,分别基于 keylabel 筛选键-值。 有关筛选的详细信息,请参阅如何查询配置设置

    filtered_settings_list = app_config_client.list_configuration_settings(key_filter="TestApp*")
    print("\nRetrieved list of configuration settings:")
    for item in filtered_settings_list:
        print("Key: " + item.key + ", Value: " + item.value)

锁定配置设置

应用程序配置中键-值的锁定状态由 ConfigurationSetting 对象的 read_only 属性表示。 如果 read_onlyTrue,则该设置为锁定状态。 可使用 read_only=True 参数调用 set_read_only 方法来锁定配置设置。

    locked_config_setting = app_config_client.set_read_only(added_config_setting, read_only=True)
    print("\nRead-only status for " + locked_config_setting.key + ": " + str(locked_config_setting.read_only))

解锁配置设置

如果 ConfigurationSettingread_only 属性为 False,则该设置已解锁。 可使用 read_only=False 参数调用 set_read_only 方法来解锁配置设置。

    unlocked_config_setting = app_config_client.set_read_only(locked_config_setting, read_only=False)
    print("\nRead-only status for " + unlocked_config_setting.key + ": " + str(unlocked_config_setting.read_only))

更新配置设置

set_configuration_setting 方法可用于更新现有设置或创建新的设置。 以下代码片段可更改现有配置设置的值。

    added_config_setting.value = "Value has been updated!"
    updated_config_setting = app_config_client.set_configuration_setting(added_config_setting)
    print("\nUpdated configuration setting:")
    print("Key: " + updated_config_setting.key + ", Value: " + updated_config_setting.value)

删除配置设置

以下代码片段按 key 名称删除配置设置。


    deleted_config_setting = app_config_client.delete_configuration_setting(key="TestApp:Settings:NewSetting")
    print("\nDeleted configuration setting:")
    print("Key: " + deleted_config_setting.key + ", Value: " + deleted_config_setting.value)

运行应用

在本示例中,你创建了一个 Python 应用,它使用 Azure 应用配置客户端库来检索通过 Azure 门户创建的配置设置、添加新设置、检索现有设置的列表、锁定和解锁设置、更新设置和最终删除设置。

此时,app-configuration-example.py 文件应该有以下代码:

import os
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting

try:
    print("Azure App Configuration - Python example")
    # Example code goes here

    connection_string = os.getenv('AZURE_APPCONFIG_CONNECTION_STRING')
    app_config_client = AzureAppConfigurationClient.from_connection_string(connection_string)

    retrieved_config_setting = app_config_client.get_configuration_setting(key='TestApp:Settings:Message')
    print("\nRetrieved configuration setting:")
    print("Key: " + retrieved_config_setting.key + ", Value: " + retrieved_config_setting.value)

    config_setting = ConfigurationSetting(
        key='TestApp:Settings:NewSetting',
        value='New setting value'
    )
    added_config_setting = app_config_client.add_configuration_setting(config_setting)
    print("\nAdded configuration setting:")
    print("Key: " + added_config_setting.key + ", Value: " + added_config_setting.value)

    filtered_settings_list = app_config_client.list_configuration_settings(key_filter="TestApp*")
    print("\nRetrieved list of configuration settings:")
    for item in filtered_settings_list:
        print("Key: " + item.key + ", Value: " + item.value)

    locked_config_setting = app_config_client.set_read_only(added_config_setting, read_only=True)
    print("\nRead-only status for " + locked_config_setting.key + ": " + str(locked_config_setting.read_only))

    unlocked_config_setting = app_config_client.set_read_only(locked_config_setting, read_only=False)
    print("\nRead-only status for " + unlocked_config_setting.key + ": " + str(unlocked_config_setting.read_only))

    added_config_setting.value = "Value has been updated!"
    updated_config_setting = app_config_client.set_configuration_setting(added_config_setting)
    print("\nUpdated configuration setting:")
    print("Key: " + updated_config_setting.key + ", Value: " + updated_config_setting.value)

    deleted_config_setting = app_config_client.delete_configuration_setting(key="TestApp:Settings:NewSetting")
    print("\nDeleted configuration setting:")
    print("Key: " + deleted_config_setting.key + ", Value: " + deleted_config_setting.value)

except Exception as ex:
    print('Exception:')
    print(ex)

在控制台窗口中,导航到包含 app-configuration-example.py 文件的目录,然后执行以下 Python 命令来运行应用:

python app-configuration-example.py

应该会看到以下输出:

Azure App Configuration - Python example

Retrieved configuration setting:
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration

Added configuration setting:
Key: TestApp:Settings:NewSetting, Value: New setting value

Retrieved list of configuration settings:
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
Key: TestApp:Settings:NewSetting, Value: New setting value

Read-only status for TestApp:Settings:NewSetting: True

Read-only status for TestApp:Settings:NewSetting: False

Updated configuration setting:
Key: TestApp:Settings:NewSetting, Value: Value has been updated!

Deleted configuration setting:
Key: TestApp:Settings:NewSetting, Value: Value has been updated!

清理资源

如果不想继续使用本文中创建的资源,请删除此处创建的资源组以避免产生费用。

重要

删除资源组的操作不可逆。 将永久删除资源组以及其中的所有资源。 请确保不要意外删除错误的资源组或资源。 如果在包含要保留的其他资源的资源组中创建了本文的资源,请从相应的窗格中单独删除每个资源,而不是删除该资源组。

  1. 登录到 Azure 门户,然后选择“资源组”。
  2. 在“按名称筛选”框中,输入资源组的名称
  3. 在结果列表中,选择资源组名称以查看概述。
  4. 选择“删除资源组”。
  5. 系统会要求确认是否删除资源组。 重新键入资源组的名称进行确认,然后选择“删除” 。

片刻之后,将会删除该资源组及其所有资源。

后续步骤

本指南演示了如何使用 Azure SDK for Python 访问 Azure 应用配置中的数据。

有关其他代码示例,请访问:

若要了解如何将 Azure 应用配置与 Python 应用配合使用,请转到: