共用方式為


使用適用於 Python 的 Azure SDK 建立 Python 應用程式

本文件顯示如何使用適用於 Python 的 Azure SDK 存取您在 Azure 應用程式組態中資料的範例。

提示

應用程式組態提供以 Python SDK 為基礎建置的 Python 提供者程式庫,其設計目的是更容易與更豐富的功能搭配使用。 這可讓組態設定像字典一樣使用,並提供其他功能,例如來自多個標籤的組態組合、金鑰名稱修剪,以及金鑰保存庫參考的自動解析。 移至 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.py 目錄中,建立名為 app-configuration-example 的新檔案,並新增下列程式碼:

    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 文件

程式碼範例

本節中的範例程式碼片段示範如何使用適用於 Python 的應用程式組態用戶端程式庫來執行一般作業。 將這些程式碼片段新增至您稍早建立的 app-configuration-example.pytry 檔案中的 區塊。

注意

應用程式組態用戶端程式庫會將索引鍵/值物件稱作為 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)

新增組態設定

下列程式碼片段會建立具有 ConfigurationSettingkey 欄位的 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_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)

鎖定組態設定

應用程式組態中索引鍵/值的鎖定狀態是由 read_only 物件的 ConfigurationSetting 屬性所表示。 如果 read_onlyTrue,則會鎖定此設定。 您可以使用 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_onlyConfigurationSetting 屬性為 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)
  1. 設定環境變數

    將名為 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>'
    
  2. 正確設定環境變數之後,請在控制台視窗中流覽至包含 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. 系統將會要求您確認是否刪除資源群組。 輸入您資源群組的名稱以進行確認,然後選取 [刪除]

不久後,系統便會刪除該資源群組及其所有的資源。

下一步

本指南說明如何使用適用於 Python 的 Azure SDK 存取您在 Azure 應用程式組態中的資料。

如需其他程式碼範例,請造訪:

若要了解如何搭配 Python 應用程式使用 Azure 應用程式組態,請移至: