Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
本文档以示例方式演示了如何使用 Azure SDK for Python 访问 Azure 应用配置中的数据。
Sugerencia
应用配置提供基于 Python SDK 构建的 Python 提供程序库,旨在通过更丰富的功能来方便用户使用。 它使配置设置能够像字典一样使用,并提供其他功能,例如从多个标签进行配置组合、键名称剪裁和自动解析 Key Vault 引用的功能。 若要了解详细信息,请转到 Python 快速入门。
- Azure 订阅 - 创建试用版订阅
- Python 3.8 或更高版本 - 有关在 Windows 上设置 Python 的信息,请参阅 Windows 上的 Python 文档
- 应用程序配置存储区。 创建存储区。
将以下键值添加到应用程序配置存储区,并让“标签”和“内容类型”保留默认值。 有关如何使用 Azure 门户或 CLI 将键值添加到存储区的详细信息,请转到创建键值。
密钥 | 值 |
---|---|
TestApp:Settings:Message | Azure 应用配置的数据 |
为名为 app-configuration-example 的项目创建一个新目录。
mkdir app-configuration-example
切换到新创建的 app-configuration-example 目录。
cd app-configuration-example
使用
pip install
命令安装 Azure 应用程序配置客户端库。pip install azure-appconfiguration
在 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)
Nota
本示例中的代码片段将帮助你开始使用适用于 Python 的应用配置客户端库。 对于应用程序,你还应考虑根据需要处理异常。 若要详细了解异常处理,请参阅 Python SDK 文档。
本部分中的示例代码片段演示如何通过适用于 Python 的应用程序配置客户端库执行常见操作。 将这些代码片段添加到之前创建的 app-configuration-example.py 文件中的 try
块。
Nota
应用程序配置客户端库将键-值对象称为 ConfigurationSetting
。 因此在本文中,应用程序配置存储区中的“键-值”将被称为为“配置设置” 。
参阅以下文章了解相关操作:
以下代码片段创建 AzureAppConfigurationClient 的实例。 你可以使用 Microsoft Entra ID(推荐)或连接字符串连接到应用程序配置存储区。
使用 DefaultAzureCredential
向应用程序配置存储区进行身份验证。 按照说明为凭据分配“应用程序配置数据读取者”角色。 运行应用程序前,请务必留出足够的时间让权限生效。
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
endpoint = os.getenv('AZURE_APPCONFIG_ENDPOINT')
app_config_client = AzureAppConfigurationClient(base_url=endpoint, credential=credential)
以下代码片段按 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)
以下代码片段使用 ConfigurationSetting
和 key
字段创建 value
对象,并调用 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_filter
和 label_filter
参数,分别基于 key
和 label
筛选键-值。 有关筛选的详细信息,请参阅如何查询配置设置。
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)
应用程序配置中键-值的锁定状态由 read_only
对象的 ConfigurationSetting
属性表示。 如果 read_only
为 True
,则该设置为锁定状态。 可使用 set_read_only
参数调用 read_only=True
方法来锁定配置设置。
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))
如果 read_only
的 ConfigurationSetting
属性为 False
,则该设置已解锁。 可使用 set_read_only
参数调用 read_only=False
方法来解锁配置设置。
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.identity import DefaultAzureCredential
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting
try:
print("Azure App Configuration - Python example")
# Example code goes here
credential = DefaultAzureCredential()
endpoint = os.getenv('AZURE_APPCONFIG_ENDPOINT')
app_config_client = AzureAppConfigurationClient(base_url=endpoint, credential=credential)
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)
配置环境变量
将名为 AZURE_APPCONFIG_ENDPOINT 的环境变量设置为 Azure 门户中应用商店的“概述”下找到的应用程序配置存储区的终结点。
如果使用 Windows 命令提示符,则请运行以下命令并重启命令提示符,这样更改才会生效:
setx AZURE_APPCONFIG_ENDPOINT "endpoint-of-your-app-configuration-store"
如果使用 PowerShell,请运行以下命令:
$Env:AZURE_APPCONFIG_ENDPOINT = "endpoint-of-your-app-configuration-store"
如果使用 macOS 或 Linux,则请运行以下命令:
export AZURE_APPCONFIG_ENDPOINT='<endpoint-of-your-app-configuration-store>'
正确设置环境变量后,在控制台窗口中,导航到包含 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!
如果不想继续使用本文中创建的资源,请删除此处创建的资源组以避免产生费用。
Importante
删除资源组的操作不可逆。 将永久删除资源组以及其中的所有资源。 请确保不要意外删除错误的资源组或资源。 如果在包含要保留的其他资源的资源组中创建了本文的资源,请从相应的窗格中单独删除每个资源,而不是删除该资源组。
- 登录到 Azure 门户,然后选择“资源组”。
- 在“按名称筛选”框中,输入资源组的名称。
- 在结果列表中,选择资源组名称以查看概述。
- 选择“删除资源组”。
- 系统会要求确认是否删除资源组。 重新键入资源组的名称进行确认,然后选择“删除”。
片刻之后,将会删除该资源组及其所有资源。
本指南演示了如何使用 Azure SDK for Python 访问 Azure 应用配置中的数据。
有关其他代码示例,请访问:
若要了解如何将 Azure 应用配置与 Python 应用配合使用,请转到: