Share via


빠른 시작: Azure App Configuration을 사용하여 Python 앱 만들기

이 빠른 시작에서는 Azure App Configuration용 Python 공급자에서 Azure App Configuration Python 공급자 클라이언트 라이브러리를 사용하여 애플리케이션 설정의 저장 및 관리를 중앙 집중화합니다.

Python 앱 구성 공급자는 Python용 Azure SDK를 기준으로 실행되는 라이브러리로, Python 개발자가 App Configuration 서비스를 쉽게 사용할 수 있도록 지원합니다. 이를 통해 구성 설정을 사전처럼 사용할 수 있습니다.

필수 조건

키-값 추가

App Configuration 저장소에 다음 키-값을 추가합니다. Azure Portal 또는 CLI를 사용하여 저장소에 키-값을 추가하는 방법에 대한 자세한 내용은 키-값 만들기로 이동합니다.

Label 내용 유형
message Hello 비워 둡니다. 비워 둡니다.
test.message Hello test 비워 둡니다. 비워 둡니다.
my_json {"key":"value"} 비워 둡니다. application/json

콘솔 애플리케이션

이 섹션에서는 콘솔 애플리케이션을 만들고 App Configuration 저장소에서 데이터를 로드합니다.

App Configuration에 연결

  1. app-configuration-quickstart라는 프로젝트에 대한 새 디렉터리를 만듭니다.

    mkdir app-configuration-quickstart
    
  2. 새로 만든 app-configuration-quickstart 디렉터리로 전환합니다.

    cd app-configuration-quickstart
    
  3. pip install 명령을 사용하여 Azure App Configuration 공급자를 설치합니다.

    pip install azure-appconfiguration-provider
    
  4. app-configuration-quickstart.py라는 새 파일을 app-configuration-quickstart 디렉터리에 만들고 다음 코드를 추가합니다.

    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이라는 환경 변수를 설정하고, App Configuration 저장소에 대한 연결 문자열로 설정합니다. 명령줄에서 다음 명령을 실행합니다.

    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
    

웹 애플리케이션

App Configuration 공급자는 다양한 Python 프레임워크의 기존 구성과 함께 사용할 수 있는 사전으로 액세스할 수 있는 Mapping 개체에 데이터를 로드합니다. 이 섹션에서는 Flask 및 Django와 같은 인기 있는 웹 프레임워크에서 App Configuration 공급자를 사용하는 방법을 보여 줍니다.

기본 제공 구성을 업데이트하여 기존 Flask 웹앱에서 Azure App Configuration을 사용할 수 있습니다. App Configuration 공급자 개체를 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 웹 애플리케이션에서 Azure App Configuration을 사용하는 방법에 대한 전체 코드 샘플은 Azure App Configuration GitHub 리포지토리에서 찾을 수 있습니다.

리소스 정리

이 문서에서 만든 리소스를 계속 사용하지 않으려면 여기서 만든 리소스 그룹을 삭제하여 요금이 부과되지 않도록 합니다.

Important

리소스 그룹을 삭제하면 다시 되돌릴 수 없습니다. 리소스 그룹 및 포함된 모든 리소스가 영구적으로 삭제됩니다. 잘못된 리소스 그룹 또는 리소스를 자동으로 삭제하지 않도록 합니다. 유지하려는 다른 리소스가 포함된 리소스 그룹 내에서 이 문서에 대한 리소스를 만든 경우 리소스 그룹을 삭제하는 대신 해당 창에서 각 리소스를 개별적으로 삭제합니다.

  1. Azure Portal에 로그인하고 리소스 그룹을 선택합니다.
  2. 이름으로 필터링 상자에서 리소스 그룹의 이름을 입력합니다.
  3. 결과 목록에서 리소스 그룹 이름을 선택하여 개요를 확인합니다.
  4. 리소스 그룹 삭제를 선택합니다.
  5. 리소스 그룹 삭제를 확인하는 메시지가 표시됩니다. 리소스 그룹의 이름을 입력하여 확인하고 삭제를 선택합니다.

잠시 후, 리소스 그룹 및 모든 해당 리소스가 삭제됩니다.

다음 단계

이 빠른 시작에서는 새 App Configuration 저장소를 만들고 Python 앱에서 키-값에 액세스하는 방법을 알아보았습니다.

추가 코드 샘플은 다음을 방문하세요.