快速入門:使用 Azure 應用程式組態建立 Python 應用程式

在本快速入門中,您將使用 Azure 應用程式組態 Python 提供者用戶端程式庫,利用 Azure 應用程式組態 Python 提供者來集中儲存和管理應用程式設定。

Python 應用程式組態提供者是在適用於 Python 的 Azure SDK 之上執行的程式庫,可協助 Python 開發人員輕鬆取用應用程式組態服務。 其讓組態設定可以像字典一樣使用。

必要條件

新增金鑰值

將下列金鑰值新增至應用程式組態存放區。 如需如何使用 Azure 入口網站或 CLI 將金鑰值新增至存放區的詳細資訊,請移至建立金鑰值

機碼 標籤 內容類型
message 您好 保留空白 保留空白
test.message Hello 測試 保留空白 保留空白
my_json {"key":"value"} 保留空白 application/json

主控台應用程式

在本節中,您將建立主控台應用程式,並從應用程式組態存放區載入資料。

連線至應用程式組態

  1. 為專案建立名為 app-configuration-quickstart 的新目錄。

    mkdir app-configuration-quickstart
    
  2. 切換至新建立的 app-configuration-quickstart 目錄。

    cd app-configuration-quickstart
    
  3. 使用 pip install 命令安裝 Azure 應用程式組態提供者。

    pip install azure-appconfiguration-provider
    
  4. 在 app-configuration-quickstart 目錄中,建立名為 app-configuration-quickstart.py 的新檔案,並新增下列程式碼:

    from azure.appconfiguration.provider import (
        load,
        SettingSelector
    )
    import os
    
    connection_string = os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING")
    
    # Connect to Azure App Configuration using a connection string.
    config = load(connection_string=connection_string)
    
    # Find the key "message" and print its value.
    print(config["message"])
    # Find the key "my_json" and print the value for "key" from the dictionary.
    print(config["my_json"]["key"])
    
    # Connect to Azure App Configuration using a connection string and trimmed key prefixes.
    trimmed = {"test."}
    config = load(connection_string=connection_string, trim_prefixes=trimmed)
    # From the keys with trimmed prefixes, find a key with "message" and print its value.
    print(config["message"])
    
    # Connect to Azure App Configuration using SettingSelector.
    selects = {SettingSelector(key_filter="message*", label_filter="\0")}
    config = load(connection_string=connection_string, selects=selects)
    
    # Print True or False to indicate if "message" is found in Azure App Configuration.
    print("message found: " + str("message" in config))
    print("test.message found: " + str("test.message" in config))
    

執行應用程式

  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%
    
  3. 正確設定環境變數之後,請執行下列命令,在本機執行應用程式:

    python app-configuration-quickstart.py
    

    您應該會看見下列輸出:

    Hello
    value
    Hello test
    message found: True
    test.message found: False
    

Web 應用程式

應用程式組態提供者會將資料載入 Mapping 物件,以字典的形式存取,可與各種 Python 架構的現有設定搭配使用。 本節說明如何在 Flask 和 Django 等熱門 Web 架構中使用應用程式組態提供者。

您可以藉由更新內建的設定,在現有的 Flask Web 應用程式中使用 Azure 應用程式組態。 若要更新內建設定,您可以將應用程式組態提供者物件傳遞至 app.py 中 Flask 應用程式執行個體的 update 函式:

azure_app_config = load(connection_string=os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING"))

# NOTE: This will override all existing configuration settings with the same key name.
app.config.update(azure_app_config)

# Access a configuration setting directly from within Flask configuration
message = app.config.get("message")

如需如何在 Python Web 應用程式中使用 Azure 應用程式組態的完整程式碼範例,請參閱 Azure 應用程式組態 GitHub 存放庫。

清除資源

如果您不想繼續使用本文中建立的資源,請刪除在此處建立的資源群組,以避免產生費用。

重要

刪除資源群組是無法回復的動作。 資源群組和其中的所有資源都將被永久刪除。 請確定您不會誤刪錯誤的資源群組或資源。 如果您是在包含需保留其他資源的資源群組內部,建立本文的資源,則可以從每個資源各自的窗格中個別刪除每個資源,而不必刪除整個資源群組。

  1. 登入 Azure 入口網站,然後選取 [資源群組]
  2. 在 [依名稱篩選] 方塊中,輸入您資源群組的名稱。
  3. 在結果清單中,選取資源群組名稱以查看概觀。
  4. 選取 [刪除資源群組]
  5. 系統將會要求您確認是否刪除資源群組。 輸入您資源群組的名稱以進行確認,然後選取 [刪除]

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

下一步

在本快速入門中,您已建立新的應用程式組態存放區,並學會如何從 Python 應用程式存取索引鍵/值。

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