azure-appconfiguration-py
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAzure App Configuration SDK for Python
适用于Python的Azure App Configuration SDK
Centralized configuration management with feature flags and dynamic settings.
带有功能标志和动态设置的集中式配置管理。
Installation
安装
bash
pip install azure-appconfigurationbash
pip install azure-appconfigurationEnvironment Variables
环境变量
bash
AZURE_APPCONFIGURATION_CONNECTION_STRING=Endpoint=https://<name>.azconfig.io;Id=...;Secret=...bash
AZURE_APPCONFIGURATION_CONNECTION_STRING=Endpoint=https://<name>.azconfig.io;Id=...;Secret=...Or for Entra ID:
或者使用Entra ID:
AZURE_APPCONFIGURATION_ENDPOINT=https://<name>.azconfig.io
undefinedAZURE_APPCONFIGURATION_ENDPOINT=https://<name>.azconfig.io
undefinedAuthentication
身份验证
Connection String
连接字符串
python
from azure.appconfiguration import AzureAppConfigurationClient
client = AzureAppConfigurationClient.from_connection_string(
os.environ["AZURE_APPCONFIGURATION_CONNECTION_STRING"]
)python
from azure.appconfiguration import AzureAppConfigurationClient
client = AzureAppConfigurationClient.from_connection_string(
os.environ["AZURE_APPCONFIGURATION_CONNECTION_STRING"]
)Entra ID
Entra ID
python
from azure.appconfiguration import AzureAppConfigurationClient
from azure.identity import DefaultAzureCredential
client = AzureAppConfigurationClient(
base_url=os.environ["AZURE_APPCONFIGURATION_ENDPOINT"],
credential=DefaultAzureCredential()
)python
from azure.appconfiguration import AzureAppConfigurationClient
from azure.identity import DefaultAzureCredential
client = AzureAppConfigurationClient(
base_url=os.environ["AZURE_APPCONFIGURATION_ENDPOINT"],
credential=DefaultAzureCredential()
)Configuration Settings
配置设置
Get Setting
获取设置
python
setting = client.get_configuration_setting(key="app:settings:message")
print(f"{setting.key} = {setting.value}")python
setting = client.get_configuration_setting(key="app:settings:message")
print(f"{setting.key} = {setting.value}")Get with Label
按标签获取
python
undefinedpython
undefinedLabels allow environment-specific values
标签支持环境专属值
setting = client.get_configuration_setting(
key="app:settings:message",
label="production"
)
undefinedsetting = client.get_configuration_setting(
key="app:settings:message",
label="production"
)
undefinedSet Setting
设置配置项
python
from azure.appconfiguration import ConfigurationSetting
setting = ConfigurationSetting(
key="app:settings:message",
value="Hello, World!",
label="development",
content_type="text/plain",
tags={"environment": "dev"}
)
client.set_configuration_setting(setting)python
from azure.appconfiguration import ConfigurationSetting
setting = ConfigurationSetting(
key="app:settings:message",
value="Hello, World!",
label="development",
content_type="text/plain",
tags={"environment": "dev"}
)
client.set_configuration_setting(setting)Delete Setting
删除配置项
python
client.delete_configuration_setting(
key="app:settings:message",
label="development"
)python
client.delete_configuration_setting(
key="app:settings:message",
label="development"
)List Settings
列出配置项
All Settings
所有配置项
python
settings = client.list_configuration_settings()
for setting in settings:
print(f"{setting.key} [{setting.label}] = {setting.value}")python
settings = client.list_configuration_settings()
for setting in settings:
print(f"{setting.key} [{setting.label}] = {setting.value}")Filter by Key Prefix
按键前缀过滤
python
settings = client.list_configuration_settings(
key_filter="app:settings:*"
)python
settings = client.list_configuration_settings(
key_filter="app:settings:*"
)Filter by Label
按标签过滤
python
settings = client.list_configuration_settings(
label_filter="production"
)python
settings = client.list_configuration_settings(
label_filter="production"
)Feature Flags
功能标志
Set Feature Flag
设置功能标志
python
from azure.appconfiguration import ConfigurationSetting
import json
feature_flag = ConfigurationSetting(
key=".appconfig.featureflag/beta-feature",
value=json.dumps({
"id": "beta-feature",
"enabled": True,
"conditions": {
"client_filters": []
}
}),
content_type="application/vnd.microsoft.appconfig.ff+json;charset=utf-8"
)
client.set_configuration_setting(feature_flag)python
from azure.appconfiguration import ConfigurationSetting
import json
feature_flag = ConfigurationSetting(
key=".appconfig.featureflag/beta-feature",
value=json.dumps({
"id": "beta-feature",
"enabled": True,
"conditions": {
"client_filters": []
}
}),
content_type="application/vnd.microsoft.appconfig.ff+json;charset=utf-8"
)
client.set_configuration_setting(feature_flag)Get Feature Flag
获取功能标志
python
setting = client.get_configuration_setting(
key=".appconfig.featureflag/beta-feature"
)
flag_data = json.loads(setting.value)
print(f"Feature enabled: {flag_data['enabled']}")python
setting = client.get_configuration_setting(
key=".appconfig.featureflag/beta-feature"
)
flag_data = json.loads(setting.value)
print(f"Feature enabled: {flag_data['enabled']}")List Feature Flags
列出功能标志
python
flags = client.list_configuration_settings(
key_filter=".appconfig.featureflag/*"
)
for flag in flags:
data = json.loads(flag.value)
print(f"{data['id']}: {'enabled' if data['enabled'] else 'disabled'}")python
flags = client.list_configuration_settings(
key_filter=".appconfig.featureflag/*"
)
for flag in flags:
data = json.loads(flag.value)
print(f"{data['id']}: {'enabled' if data['enabled'] else 'disabled'}")Read-Only Settings
只读配置项
python
undefinedpython
undefinedMake setting read-only
将配置项设为只读
client.set_read_only(
configuration_setting=setting,
read_only=True
)
client.set_read_only(
configuration_setting=setting,
read_only=True
)
Remove read-only
取消只读
client.set_read_only(
configuration_setting=setting,
read_only=False
)
undefinedclient.set_read_only(
configuration_setting=setting,
read_only=False
)
undefinedSnapshots
快照
Create Snapshot
创建快照
python
from azure.appconfiguration import ConfigurationSnapshot, ConfigurationSettingFilter
snapshot = ConfigurationSnapshot(
name="v1-snapshot",
filters=[
ConfigurationSettingFilter(key="app:*", label="production")
]
)
created = client.begin_create_snapshot(
name="v1-snapshot",
snapshot=snapshot
).result()python
from azure.appconfiguration import ConfigurationSnapshot, ConfigurationSettingFilter
snapshot = ConfigurationSnapshot(
name="v1-snapshot",
filters=[
ConfigurationSettingFilter(key="app:*", label="production")
]
)
created = client.begin_create_snapshot(
name="v1-snapshot",
snapshot=snapshot
).result()List Snapshot Settings
列出快照配置项
python
settings = client.list_configuration_settings(
snapshot_name="v1-snapshot"
)python
settings = client.list_configuration_settings(
snapshot_name="v1-snapshot"
)Async Client
异步客户端
python
from azure.appconfiguration.aio import AzureAppConfigurationClient
from azure.identity.aio import DefaultAzureCredential
async def main():
credential = DefaultAzureCredential()
client = AzureAppConfigurationClient(
base_url=endpoint,
credential=credential
)
setting = await client.get_configuration_setting(key="app:message")
print(setting.value)
await client.close()
await credential.close()python
from azure.appconfiguration.aio import AzureAppConfigurationClient
from azure.identity.aio import DefaultAzureCredential
async def main():
credential = DefaultAzureCredential()
client = AzureAppConfigurationClient(
base_url=endpoint,
credential=credential
)
setting = await client.get_configuration_setting(key="app:message")
print(setting.value)
await client.close()
await credential.close()Client Operations
客户端操作
| Operation | Description |
|---|---|
| Get single setting |
| Create or update setting |
| Delete setting |
| List with filters |
| Lock/unlock setting |
| Create point-in-time snapshot |
| List all snapshots |
| 操作 | 描述 |
|---|---|
| 获取单个配置项 |
| 创建或更新配置项 |
| 删除配置项 |
| 带过滤条件列出配置项 |
| 锁定/解锁配置项 |
| 创建时间点快照 |
| 列出所有快照 |
Best Practices
最佳实践
- Use labels for environment separation (dev, staging, prod)
- Use key prefixes for logical grouping (app:database:, app:cache:)
- Make production settings read-only to prevent accidental changes
- Create snapshots before deployments for rollback capability
- Use Entra ID instead of connection strings in production
- Refresh settings periodically in long-running applications
- Use feature flags for gradual rollouts and A/B testing
- 使用标签进行环境隔离(开发、预发布、生产)
- 使用键前缀进行逻辑分组(app:database:、app:cache:)
- 将生产环境配置项设为只读以防止意外修改
- 在部署前创建快照以具备回滚能力
- 在生产环境中使用Entra ID而非连接字符串
- 在长期运行的应用中定期刷新配置项
- 使用功能标志进行逐步发布和A/B测试