このクイックスタートでは、Azure App Configuration Python プロバイダー クライアント ライブラリを使用して、Azure App Configuration 用の Python プロバイダーでアプリケーション設定のストレージと管理を一元化します。
Python App Configuration プロバイダーは、Azure SDK for Python 上で実行されているライブラリで、Python 開発者が App Configuration サービスを簡単に使用するのに役立ちます。 これにより、構成設定をディクショナリのように使用できます。
前提条件
- アクティブなサブスクリプションが含まれる Azure アカウント。 無料で作成できます。
- App Configuration ストア。 ストアを作成する。
- Python 3.8 以降 - Windows での Python の設定について詳しくは、Windows での Python に関するドキュメントをご覧ください
キーと値を追加する
App Configuration ストアに次のキーと値を追加します。 Azure portal または CLI を使用してストアにキーと値を追加する方法の詳細については、「キーと値を作成する」を参照してください。
キー | 値 | Label | Content type |
---|---|---|---|
message | Hello | 空のままにします | 空のままにします |
test.message | Hello test | 空のままにします | 空のままにします |
my_json | {"key":"value"} | 空のままにします | application/json |
コンソール アプリケーション
このセクションでは、コンソール アプリケーションを作成し、App Configuration ストアからデータを読み込みます。
App Configuration に接続する
プロジェクト用に app-configuration-quickstart という名前の新しいディレクトリを作成します。
mkdir app-configuration-quickstart
新しく作成した app-configuration-quickstart ディレクトリに切り替えます。
cd app-configuration-quickstart
pip install
コマンドを使用して、Azure App Configuration プロバイダーをインストールします。pip install azure-appconfiguration-provider
app-configuration-quickstart ディレクトリに app-configuration-quickstart.py という名前の新しいファイルを作成し、次のコードを追加します。
DefaultAzureCredential
を使って、App Configuration ストアに対する認証を行います。 手順に従って、資格情報に App Configuration データ閲覧者ロールを割り当てます。 アプリケーションを実行する前に、アクセス許可が伝わるのに十分な時間をおいてください。from azure.appconfiguration.provider import ( load, SettingSelector ) from azure.identity import DefaultAzureCredential import os endpoint = os.environ.get("AZURE_APPCONFIG_ENDPOINT") # Connect to Azure App Configuration using Microsoft Entra ID. config = load(endpoint=endpoint, credential=credential) credential = DefaultAzureCredential() # 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 Entra ID and trimmed key prefixes. trimmed = {"test."} config = load(endpoint=endpoint, credential=credential, 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(endpoint=endpoint, credential=credential, 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))
アプリケーションの実行
環境変数を設定します。
AZURE_APPCONFIG_ENDPOINT という名前の環境変数に、Azure portal でストアの [概要] にある App Configuration ストアのエンドポイントを設定します。
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>'
環境変数が適切に設定されたら、次のコマンドを実行してアプリをローカルで実行します。
python app-configuration-quickstart.py
次の出力が表示されます。
Hello value Hello test message found: True test.message found: False
Web アプリケーション
App Configuration プロバイダーは、ディクショナリとしてアクセスできる Mapping
オブジェクトにデータを読み込みます。これは、さまざまな Python フレームワークの既存の構成と組み合わせて使用できます。 このセクションでは、Flask や Django などの一般的な Web フレームワークで App Configuration プロバイダーを使用する方法について説明します。
既存の Flask Web アプリで Azure App Configuration を使用するには、組み込みの構成を更新します。 これを行うには、app.py
で、App Configuration プロバイダー オブジェクトを Flask アプリ インスタンスの update
関数に渡します。
azure_app_config = load(endpoint=os.environ.get("AZURE_APPCONFIG_ENDPOINT"), credential=credential)
# 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 App Configuration を使用する方法に関する完全なコード サンプルについては、Azure App Configuration の GitHub リポジトリを参照してください。
リソースをクリーンアップする
この記事で作成したリソースを継続して使用しない場合は、ここで作成したリソース グループを削除して課金されないようにしてください。
重要
リソース グループを削除すると、元に戻すことができません。 リソース グループとそのすべてのリソースは完全に削除されます。 間違ったリソース グループやリソースをうっかり削除しないようにしてください。 この記事のリソースを、保持したい他のリソースを含むリソース グループ内に作成した場合は、リソース グループを削除する代わりに、各リソースをそれぞれのペインから個別に削除します。
- Azure portal にサインインし、 [リソース グループ] を選択します。
- [名前でフィルター] ボックスにリソース グループの名前を入力します。
- 結果一覧でリソース グループ名を選択し、概要を表示します。
- [リソース グループの削除] を選択します。
- リソース グループの削除の確認を求めるメッセージが表示されます。 確認のためにリソース グループの名前を入力し、 [削除] を選択します。
しばらくすると、リソース グループとそのすべてのリソースが削除されます。
次のステップ
このクイックスタートでは、新しい App Configuration ストアを作成して、Python アプリのキー値を操作する方法について学習しました。
その他のコード サンプルについては、次を参照してください。