Python용 Azure 라이브러리를 사용하여 Azure를 사용할 수 있는 모든 지역에 연결할 수 있습니다.
기본적으로 Azure 라이브러리는 글로벌 Azure 클라우드에 연결하도록 구성됩니다.
미리 정의된 소버린 클라우드 상수 사용
미리 정의된 소버린 클라우드 상수는 AzureAuthorityHosts 라이브러리의 azure.identity 모듈에서 제공합니다.
AZURE_CHINAAZURE_GOVERNMENTAZURE_PUBLIC_CLOUD
정의를 사용하려면 클라이언트 개체를 만들 때 적절한 상수에서 azure.identity.AzureAuthorityHosts 가져와 적용합니다.
다음 예제와 같이 사용하는 DefaultAzureCredential경우 적절한 값을 azure.identity.AzureAuthorityHosts사용하여 클라우드를 지정할 수 있습니다.
import os
from azure.mgmt.resource import ResourceManagementClient, SubscriptionClient
from azure.identity import DefaultAzureCredential, AzureAuthorityHosts
from azure.core import AzureClouds
authority = AzureAuthorityHosts.AZURE_CHINA
# Set environment variable AZURE_SUBSCRIPTION_ID as well as environment variables
# for DefaultAzureCredential. For combinations of environment variables, see
# https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#environment-variables
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
# When using sovereign domains (that is, any cloud other than AZURE_PUBLIC_CLOUD),
# you must use an authority with DefaultAzureCredential.
credential = DefaultAzureCredential(authority=authority)
resource_client = ResourceManagementClient(
credential, subscription_id, cloud_setting=AzureClouds.AZURE_CHINA_CLOUD
)
subscription_client = SubscriptionClient(
credential, cloud_setting=AzureClouds.AZURE_CHINA_CLOUD
)
비고
이 cloud_setting 기능은 새로 추가되었으며 Azure SDK 관리 라이브러리에서 배포되고 있습니다. 이 기간 동안 일부 클라이언트는 지원하지만 다른 클라이언트는 지원하지 않습니다. 지원을 확인하려면 클라이언트 생성자에서 cloud_setting 매개 변수를 찾습니다. 서비스의 클라이언트가 아직 cloud_setting가 공개되지 않은 경우, 아래 예제에 표시된 이전 방법을 사용하여 소버린 클라우드를 대상으로 지정할 수 있습니다.
import os
from azure.mgmt.resource import ResourceManagementClient, SubscriptionClient
from azure.identity import DefaultAzureCredential, AzureAuthorityHosts
authority = AzureAuthorityHosts.AZURE_CHINA
resource_manager = "https://management.chinacloudapi.cn"
# Set environment variable AZURE_SUBSCRIPTION_ID as well as environment variables
# for DefaultAzureCredential. For combinations of environment variables, see
# https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#environment-variables
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
# When using sovereign domains (that is, any cloud other than AZURE_PUBLIC_CLOUD),
# you must use an authority with DefaultAzureCredential.
credential = DefaultAzureCredential(authority=authority)
resource_client = ResourceManagementClient(
credential,
subscription_id,
base_url=resource_manager,
credential_scopes=[resource_manager + "/.default"],
)
subscription_client = SubscriptionClient(
credential,
base_url=resource_manager,
credential_scopes=[resource_manager + "/.default"],
)
사용자 고유의 클라우드 정의 사용
다음 코드에서 , authority및 endpoint 변수의 audience값을 프라이빗 클라우드에 적합한 값으로 바꿉니다.
import os
from azure.mgmt.resource import ResourceManagementClient, SubscriptionClient
from azure.identity import DefaultAzureCredential
from azure.profiles import KnownProfiles
# Set environment variable AZURE_SUBSCRIPTION_ID as well as environment variables
# for DefaultAzureCredential. For combinations of environment variables, see
# https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#environment-variables
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
authority = "<your authority>"
endpoint = "<your endpoint>"
audience = "<your audience>"
# When using a private cloud, you must use an authority with DefaultAzureCredential.
# The active_directory endpoint should be a URL like https://login.microsoftonline.com.
credential = DefaultAzureCredential(authority=authority)
resource_client = ResourceManagementClient(
credential, subscription_id,
base_url=endpoint,
profile=KnownProfiles.v2019_03_01_hybrid,
credential_scopes=[audience])
subscription_client = SubscriptionClient(
credential,
base_url=endpoint,
profile=KnownProfiles.v2019_03_01_hybrid,
credential_scopes=[audience])
예를 들어 Azure Stack의 경우 az cloud show CLI 명령을 사용하여 등록된 클라우드의 세부 정보를 반환할 수 있습니다. 다음 출력은 Azure 퍼블릭 클라우드에 대해 반환된 값을 보여 주지만 Azure Stack 프라이빗 클라우드의 출력은 비슷해야 합니다.
{
"endpoints": {
"activeDirectory": "https://login.microsoftonline.com",
"activeDirectoryDataLakeResourceId": "https://datalake.azure.net/",
"activeDirectoryGraphResourceId": "https://graph.windows.net/",
"activeDirectoryResourceId": "https://management.core.windows.net/",
"appInsightsResourceId": "https://api.applicationinsights.io",
"appInsightsTelemetryChannelResourceId": "https://dc.applicationinsights.azure.com/v2/track",
"attestationResourceId": "https://attest.azure.net",
"azmirrorStorageAccountResourceId": null,
"batchResourceId": "https://batch.core.windows.net/",
"gallery": "https://gallery.azure.com/",
"logAnalyticsResourceId": "https://api.loganalytics.io",
"management": "https://management.core.windows.net/",
"mediaResourceId": "https://rest.media.azure.net",
"microsoftGraphResourceId": "https://graph.microsoft.com/",
"ossrdbmsResourceId": "https://ossrdbms-aad.database.windows.net",
"portal": "https://portal.azure.com",
"resourceManager": "https://management.azure.com/",
"sqlManagement": "https://management.core.windows.net:8443/",
"synapseAnalyticsResourceId": "https://dev.azuresynapse.net",
"vmImageAliasDoc": "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/arm-compute/quickstart-templates/aliases.json"
},
"isActive": true,
"name": "AzureCloud",
"profile": "latest",
"suffixes": {
"acrLoginServerEndpoint": ".azurecr.io",
"attestationEndpoint": ".attest.azure.net",
"azureDatalakeAnalyticsCatalogAndJobEndpoint": "azuredatalakeanalytics.net",
"azureDatalakeStoreFileSystemEndpoint": "azuredatalakestore.net",
"keyvaultDns": ".vault.azure.net",
"mariadbServerEndpoint": ".mariadb.database.azure.com",
"mhsmDns": ".managedhsm.azure.net",
"mysqlServerEndpoint": ".mysql.database.azure.com",
"postgresqlServerEndpoint": ".postgres.database.azure.com",
"sqlServerHostname": ".database.windows.net",
"storageEndpoint": "core.windows.net",
"storageSyncEndpoint": "afs.azure.net",
"synapseAnalyticsEndpoint": ".dev.azuresynapse.net"
}
}
앞의 코드에서 authority를 endpoints.activeDirectory 속성의 값으로 설정할 수 있으며, endpoint는 endpoints.resourceManager 속성의 값으로, audience는 endpoints.activeDirectoryResourceId 속성의 값에 ".default"를 추가한 값으로 설정할 수 있습니다.
자세한 내용은 Azure Stack Hub에서 Azure CLI 사용 및 Azure Stack Hub에 대한 인증 정보 가져오기를 참조하세요.