azure-appconfiguration-py

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Azure App Configuration SDK for Python

适用于Python的Azure App Configuration SDK

Centralized configuration management with feature flags and dynamic settings.
带有功能标志和动态设置的集中式配置管理。

Installation

安装

bash
pip install azure-appconfiguration
bash
pip install azure-appconfiguration

Environment 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
undefined
AZURE_APPCONFIGURATION_ENDPOINT=https://<name>.azconfig.io
undefined

Authentication

身份验证

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
undefined
python
undefined

Labels allow environment-specific values

标签支持环境专属值

setting = client.get_configuration_setting( key="app:settings:message", label="production" )
undefined
setting = client.get_configuration_setting( key="app:settings:message", label="production" )
undefined

Set 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
undefined
python
undefined

Make 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 )
undefined
client.set_read_only( configuration_setting=setting, read_only=False )
undefined

Snapshots

快照

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

客户端操作

OperationDescription
get_configuration_setting
Get single setting
set_configuration_setting
Create or update setting
delete_configuration_setting
Delete setting
list_configuration_settings
List with filters
set_read_only
Lock/unlock setting
begin_create_snapshot
Create point-in-time snapshot
list_snapshots
List all snapshots
操作描述
get_configuration_setting
获取单个配置项
set_configuration_setting
创建或更新配置项
delete_configuration_setting
删除配置项
list_configuration_settings
带过滤条件列出配置项
set_read_only
锁定/解锁配置项
begin_create_snapshot
创建时间点快照
list_snapshots
列出所有快照

Best Practices

最佳实践

  1. Use labels for environment separation (dev, staging, prod)
  2. Use key prefixes for logical grouping (app:database:, app:cache:)
  3. Make production settings read-only to prevent accidental changes
  4. Create snapshots before deployments for rollback capability
  5. Use Entra ID instead of connection strings in production
  6. Refresh settings periodically in long-running applications
  7. Use feature flags for gradual rollouts and A/B testing
  1. 使用标签进行环境隔离(开发、预发布、生产)
  2. 使用键前缀进行逻辑分组(app:database:、app:cache:
  3. 将生产环境配置项设为只读以防止意外修改
  4. 在部署前创建快照以具备回滚能力
  5. 在生产环境中使用Entra ID而非连接字符串
  6. 在长期运行的应用中定期刷新配置项
  7. 使用功能标志进行逐步发布和A/B测试