Multicloud:使用適用于 Python 的 Azure 程式庫連線至所有區域
您可以使用適用于 Python 的 Azure 程式庫來連線到 Azure 可供 使用 的所有區域。
根據預設,Azure 程式庫會設定為連線到全域 Azure 雲端。
使用預先定義的主權雲端常數
預先定義的主權雲端常數是由 AzureAuthorityHosts
程式庫的 azure.identity
模組所提供:
AZURE_CHINA
AZURE_GOVERNMENT
AZURE_PUBLIC_CLOUD
若要使用定義,請從 azure.identity.AzureAuthorityHosts
匯入適當的常數,並在建立用戶端物件時套用它。
使用 DefaultAzureCredential
時,如下列範例所示,您可以使用 來自 azure.identity.AzureAuthorityHosts
的適當值來指定雲端。
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"])
使用您自己的雲端定義
在下列程式碼中,將 、 endpoint
和 audience
變數的值 authority
取代為您私人雲端適用的值。
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 的驗證資訊。