Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Du kan använda Azure-biblioteken för Python för att ansluta till alla regioner där Azure är tillgängligt.
Som standard är Azure-biblioteken konfigurerade för att ansluta till det globala Azure-molnet.
Använda fördefinierade nationella molnkonstanter
Fördefinierade nationella molnkonstanter tillhandahålls av modulen AzureAuthorityHosts i azure.identity biblioteket:
- AZURE_CHINA
- AZURE_GOVERNMENT
- AZURE_PUBLIC_CLOUD
Om du vill använda en definition importerar du lämplig konstant från azure.identity.AzureAuthorityHosts och tillämpar den när du skapar klientobjekt.
När du använder DefaultAzureCredential, som du ser i följande exempel, kan du ange molnet med hjälp av lämpligt värde från 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
)
Anmärkning
Funktionen cloud_setting har nyligen lagts till och lanseras i Azure SDK-hanteringsbibliotek. Under den här perioden stöder vissa klienter det medan andra inte gör det. Om du vill kontrollera stödet letar du efter en cloud_setting parameter i klientkonstruktorn. Om tjänstens klient inte exponerar cloud_setting ännu, kan du fortfarande rikta in dig på suveräna moln genom att använda den tidigare metoden som visas i exemplen nedan.
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"],
)
Använda din egen molndefinition
I följande kod ersätter du värdena för variablerna authority, endpointoch audience med värden som är lämpliga för ditt privata moln.
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])
För Azure Stack kan du till exempel använda kommandot az cloud show CLI för att returnera information om ett registrerat moln. Följande utdata visar de värden som returneras för det offentliga Azure-molnet, men utdata för ett privat Azure Stack-moln bör vara liknande.
{
  "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"
  }
}
I föregående kod kan du ange authority värdet endpoints.activeDirectory för egenskapen, endpoint värdet för endpoints.resourceManager egenskapen och audience värdet endpoints.activeDirectoryResourceId för egenskapen + ".default".
Mer information finns i Använda Azure CLI med Azure Stack Hub och Hämta autentiseringsinformation för Azure Stack Hub.