此頁面顯示支援的驗證方法和用戶端,並顯示範例程式碼,讓您可用來使用服務連接器,將 Azure Cosmos DB for NoSQL 連線到其他雲端服務。 在未使用服務連接器的情況下,您仍可透過其他程式設計語言連線至 Azure Cosmos DB for NoSQL。 此頁面也顯示您在建立服務連線時取得的預設環境變數名稱和值 (或 Spring Boot 設定)。
支援的計算服務
服務連接器可用來將下列計算服務連線至 Azure Cosmos DB for NoSQL:
- Azure App Service
- Azure 容器應用程式
- Azure Functions
- Azure Kubernetes Service (AKS)
- Azure Spring Apps
支援的驗證類型和用戶端類型
下表說明使用服務連接器將計算服務連線至 Azure Cosmos DB for NoSQL 時,支援哪些用戶端類型和驗證方法的組合。 「是」表示支援的組合,而「否」則表示不支援。
用戶端類型 |
系統指派的受控識別 |
使用者指派的受控識別 |
祕密 / 連接字串 |
服務主體 |
.NET |
Yes |
.是 |
.是 |
Yes |
Java |
Yes |
.是 |
.是 |
Yes |
Java - Spring Boot |
Yes |
.是 |
.是 |
Yes |
Node.js |
Yes |
.是 |
.是 |
Yes |
Python |
Yes |
.是 |
.是 |
Yes |
Go |
Yes |
.是 |
.是 |
Yes |
無 |
Yes |
.是 |
.是 |
Yes |
下表指出支源表格中所有用戶端類型和驗證方法組合。 所有用戶端類型都可以使用任何驗證方法,使用服務連接器連線到 Azure Cosmos DB for NoSQL。
預設環境變數名稱或應用程式屬性和範例程式碼
使用下列連線的詳細資料,將您的計算服務連線至 Azure Cosmos DB for NoSQL。 針對下列範例,將預留位置文字 <database-server>
、<database-name>
、<account-key>
、<resource-group-name>
、<subscription-ID>
、<client-ID>
、<SQL-server>
、<client-secret>
、<tenant-id>
和 <access-key>
取代為您自己的資訊。 如需命名慣例的詳細資訊,請參閱服務連接器內部一文。
系統指派的受控識別
SpringBoot 用戶端類型
使用系統指派的受控識別作為驗證類型僅適用於 Spring Cloud Azure 4.0 版或更高版本。
預設環境變數名稱 |
描述 |
範例值 |
spring.cloud.azure.cosmos.credential.managed-identity-enabled |
是否要啟用受控識別 |
true |
spring.cloud.azure.cosmos.database |
您的資料庫 |
https://management.azure.com/.default |
spring.cloud.azure.cosmos.endpoint |
您的資源端點 |
https://<database-server>.documents.azure.com:443/ |
其他用戶端類型
預設環境變數名稱 |
描述 |
範例值 |
AZURE_COSMOS_LISTCONNECTIONSTRINGURL |
要取得連接字串的 URL |
https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/databaseAccounts/<database-server>/listConnectionStrings?api-version=2021-04-15 |
AZURE_COSMOS_SCOPE |
您的受控識別範圍 |
https://management.azure.com/.default |
AZURE_COSMOS_RESOURCEENDPOINT |
您的資源端點 |
https://<database-server>.documents.azure.com:443/ |
範例指令碼
請參閱下列步驟和程式碼,使用系統指派的身分識別連線至 Azure Cosmos DB for NoSQL。
安裝相依性。
dotnet add package Microsoft.Azure.Cosmos
dotnet add package Azure.Identity
使用 Azure.Identity
NuGet 套件進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
using Microsoft.Azure.Cosmos;
using Azure.Core;
using Azure.Identity;
using System;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// TokenCredential credential = new DefaultAzureCredential();
// For user-assigned identity.
// TokenCredential credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// }
// );
// For service principal.
// TokenCredential credential = new ClientSecretCredential(
// tenantId: Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID")!,
// clientId: Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID")!,
// clientSecret: Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET")!,
// options: new TokenCredentialOptions()
// );
// Create a new instance of CosmosClient using the credential above
using CosmosClient client = new(
accountEndpoint: Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT")!,
tokenCredential: credential
);
在您的 pom.xml 中新增下列相依性:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-cosmos</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.5</version>
</dependency>
透過 azure-identity
進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_COSMOS_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_COSMOS_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_COSMOS_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_COSMOS_TENANTID>"))
// .build();
String endpoint = System.getenv("AZURE_COSMOS_RESOURCEENDPOINT");
CosmosClient cosmosClient = new CosmosClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
- 安裝相依性。
pip install azure-identity
pip install azure-cosmos
- 使用
azure-identity
程式庫進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
import os
from azure.cosmos import CosmosClient
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_COSMOS_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_COSMOS_TENANTID')
# client_id = os.getenv('AZURE_COSMOS_CLIENTID')
# client_secret = os.getenv('AZURE_COSMOS_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
endpoint = os.environ["AZURE_COSMOS_RESOURCEENDPOINT"]
client = CosmosClient(url=endpoint, credential=cred)
- 安裝相依性。
go get t github.com/Azure/azure-sdk-for-go/sdk/azidentity
go get github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos
- 使用
azidentity
npm 套件進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
import (
"os"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)
func main() {
endpoint = os.Getenv("AZURE_COSMOS_RESOURCEENDPOINT")
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// For user-assigned identity.
// clientid := os.Getenv("AZURE_COSMOS_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// For service principal.
// clientid := os.Getenv("AZURE_COSMOS_CLIENTID")
// tenantid := os.Getenv("AZURE_COSMOS_TENANTID")
// clientsecret := os.Getenv("AZURE_COSMOS_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
client, err := azcosmos.NewClient(endpoint, cred, nil)
}
安裝相依性。
npm install @azure/identity
npm install @azure/cosmos
使用 @azure/identity
npm 套件進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
import { CosmosClient } from "@azure/cosmos";
const { DefaultAzureCredential } = require("@azure/identity");
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// const credential = new DefaultAzureCredential();
// For user-assigned managed identity.
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: process.env.AZURE_COSMOS_CLIENTID
// });
// For service principal.
// const credential = new ClientSecretCredential(
// tenantId: process.env.AZURE_COSMOS_TENANTID,
// clientId: process.env.AZURE_COSMOS_CLIENTID,
// clientSecret: process.env.AZURE_COSMOS_CLIENTSECRET
// );
// Create a new instance of CosmosClient using the credential above
const cosmosClient = new CosmosClient({
process.env.AZURE_COSMOS_RESOURCEENDPOINT,
aadCredentials: credential
});
使用者指派的受控識別
SpringBoot 用戶端類型
使用使用者指派的受控識別作為驗證類型僅適用於 Spring Cloud Azure 4.0 版或更高版本。
預設環境變數名稱 |
描述 |
範例值 |
spring.cloud.azure.cosmos.credential.managed-identity-enabled |
是否要啟用受控識別 |
true |
spring.cloud.azure.cosmos.database |
您的資料庫 |
https://management.azure.com/.default |
spring.cloud.azure.cosmos.endpoint |
您的資源端點 |
https://<database-server>.documents.azure.com:443/ |
spring.cloud.azure.cosmos.credential.client-id |
您的用戶端識別碼 |
<client-ID> |
其他用戶端類型
預設環境變數名稱 |
描述 |
範例值 |
AZURE_COSMOS_LISTCONNECTIONSTRINGURL |
要取得連接字串的 URL |
https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/databaseAccounts/<database-server>/listConnectionStrings?api-version=2021-04-15 |
AZURE_COSMOS_SCOPE |
您的受控識別範圍 |
https://management.azure.com/.default |
AZURE_COSMOS_CLIENTID |
您的用戶端識別碼 |
<client-ID> |
AZURE_COSMOS_RESOURCEENDPOINT |
您的資源端點 |
https://<database-server>.documents.azure.com:443/ |
範例指令碼
請參閱下列步驟和程式碼,使用使用者指派的身分識別連線至 Azure Cosmos DB for NoSQL。
安裝相依性。
dotnet add package Microsoft.Azure.Cosmos
dotnet add package Azure.Identity
使用 Azure.Identity
NuGet 套件進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
using Microsoft.Azure.Cosmos;
using Azure.Core;
using Azure.Identity;
using System;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// TokenCredential credential = new DefaultAzureCredential();
// For user-assigned identity.
// TokenCredential credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// }
// );
// For service principal.
// TokenCredential credential = new ClientSecretCredential(
// tenantId: Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID")!,
// clientId: Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID")!,
// clientSecret: Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET")!,
// options: new TokenCredentialOptions()
// );
// Create a new instance of CosmosClient using the credential above
using CosmosClient client = new(
accountEndpoint: Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT")!,
tokenCredential: credential
);
在您的 pom.xml 中新增下列相依性:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-cosmos</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.5</version>
</dependency>
透過 azure-identity
進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_COSMOS_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_COSMOS_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_COSMOS_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_COSMOS_TENANTID>"))
// .build();
String endpoint = System.getenv("AZURE_COSMOS_RESOURCEENDPOINT");
CosmosClient cosmosClient = new CosmosClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
- 安裝相依性。
pip install azure-identity
pip install azure-cosmos
- 使用
azure-identity
程式庫進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
import os
from azure.cosmos import CosmosClient
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_COSMOS_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_COSMOS_TENANTID')
# client_id = os.getenv('AZURE_COSMOS_CLIENTID')
# client_secret = os.getenv('AZURE_COSMOS_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
endpoint = os.environ["AZURE_COSMOS_RESOURCEENDPOINT"]
client = CosmosClient(url=endpoint, credential=cred)
- 安裝相依性。
go get t github.com/Azure/azure-sdk-for-go/sdk/azidentity
go get github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos
- 使用
azidentity
npm 套件進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
import (
"os"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)
func main() {
endpoint = os.Getenv("AZURE_COSMOS_RESOURCEENDPOINT")
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// For user-assigned identity.
// clientid := os.Getenv("AZURE_COSMOS_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// For service principal.
// clientid := os.Getenv("AZURE_COSMOS_CLIENTID")
// tenantid := os.Getenv("AZURE_COSMOS_TENANTID")
// clientsecret := os.Getenv("AZURE_COSMOS_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
client, err := azcosmos.NewClient(endpoint, cred, nil)
}
安裝相依性。
npm install @azure/identity
npm install @azure/cosmos
使用 @azure/identity
npm 套件進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
import { CosmosClient } from "@azure/cosmos";
const { DefaultAzureCredential } = require("@azure/identity");
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// const credential = new DefaultAzureCredential();
// For user-assigned managed identity.
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: process.env.AZURE_COSMOS_CLIENTID
// });
// For service principal.
// const credential = new ClientSecretCredential(
// tenantId: process.env.AZURE_COSMOS_TENANTID,
// clientId: process.env.AZURE_COSMOS_CLIENTID,
// clientSecret: process.env.AZURE_COSMOS_CLIENTSECRET
// );
// Create a new instance of CosmosClient using the credential above
const cosmosClient = new CosmosClient({
process.env.AZURE_COSMOS_RESOURCEENDPOINT,
aadCredentials: credential
});
Connection string
警告
Microsoft 建議您使用最安全的可用驗證流程。 這個程序描述的驗證流程需要在應用程式中具備極高的信任度,且伴隨著其他流程並未面臨的風險。 請僅在其他較安全的流程 (例如受控身分識別) 皆不具可行性的情況下,才使用這個流程。
SpringBoot 用戶端類型
預設環境變數名稱 |
描述 |
範例值 |
azure.cosmos.key |
適用於 Spring Cloud Azure 版本低於 4.0 的資料庫存取金鑰 |
<access-key> |
azure.cosmos.database |
適用於 Spring Cloud Azure 版本低於 4.0 的資料庫 |
<database-name> |
azure.cosmos.uri |
適用於 Spring Cloud Azure 版本低於 4.0 的資料庫 URI |
https://<database-server>.documents.azure.com:443/ |
spring.cloud.azure.cosmos.key |
適用於 Spring Cloud Azure 版本高於 4.0 的資料庫存取金鑰 |
<access-key> |
spring.cloud.azure.cosmos.database |
適用於 Spring Cloud Azure 版本高於 4.0 的資料庫 |
<database-name> |
spring.cloud.azure.cosmos.endpoint |
適用於 Spring Cloud Azure 版本高於 4.0 的資料庫 URI |
https://<database-server>.documents.azure.com:443/ |
其他用戶端類型
預設環境變數名稱 |
描述 |
範例值 |
AZURE_COSMOS_CONNECTIONSTRING |
選取 Azure Cosmos DB for NoSQL 連接字串 |
AccountEndpoint=https://<database-server>.documents.azure.com:443/;AccountKey=<account-key> |
範例指令碼
請參閱下列步驟和程式碼,使用連接字串連線至 Azure Cosmos DB for NoSQL。
安裝相依性。
dotnet add package Microsoft.Azure.Cosmos
從服務連接器新增的環境變數取得連接字串。
using Microsoft.Azure.Cosmos;
using System;
// Create a new instance of CosmosClient using a connection string
using CosmosClient client = new(
connectionString: Environment.GetEnvironmentVariable("AZURE_COSMOS_CONNECTIONSTRING")!
);
- 在您的 pom.xml 中新增下列相依性:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-cosmos</artifactId>
<version>LATEST</version>
</dependency>
- 從服務連接器新增的環境變數取得連接字串。
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
String connectionStr = System.getenv("AZURE_COSMOS_CONNECTIONSTRING");
String[] connInfo = connectionStr.split(";");
String endpoint = connInfo[0].split("=")[1];
String accountKey = connInfo[1].split("=")[1];
CosmosClient cosmosClient = new CosmosClientBuilder()
.endpoint(endpoint)
.key(accountKey)
.buildClient();
- 安裝相依性。
pip install azure-cosmos
- 從服務連接器新增的環境變數取得連接字串。
import os
from azure.cosmos import CosmosClient
# Create a new instance of CosmosClient using a connection string
CONN_STR = os.environ["AZURE_COSMOS_CONNECTIONSTRING"]
client = CosmosClient.from_connection_string(conn_str=CONN_STR)
- 安裝相依性。
go get github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos
- 從服務連接器新增的環境變數取得連接字串。
import {
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
}
connectionString := os.Getenv("AZURE_COSMOS_CONNECTIONSTRING")
client, err := azcosmos.NewClientFromConnectionString(connectionString, nil)
- 安裝相依性。
npm install @azure/cosmos
- 從服務連接器新增的環境變數取得連接字串。
import { CosmosClient } from "@azure/cosmos";
// Create a new instance of CosmosClient using a connection string
const cosmosClient = new CosmosClient(process.env.AZURE_COSMOS_CONNECTIONSTRING);
服務主體
SpringBoot 用戶端類型
預設環境變數名稱 |
描述 |
範例值 |
spring.cloud.azure.cosmos.credential.client-id |
您的用戶端識別碼 |
<client-ID> |
spring.cloud.azure.cosmos.credential.client-secret |
您的用戶端密碼 |
<client-secret> |
spring.cloud.azure.cosmos.profile.tenant-id |
您的租用戶識別碼 |
<tenant-ID> |
spring.cloud.azure.cosmos.database |
您的資料庫 |
<database-name> |
spring.cloud.azure.cosmos.endpoint |
您的資源端點 |
https://<database-server>.documents.azure.com:443/ |
其他用戶端類型
預設環境變數名稱 |
描述 |
範例值 |
AZURE_COSMOS_LISTCONNECTIONSTRINGURL |
要取得連接字串的 URL |
https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/databaseAccounts/<database-server>/listConnectionStrings?api-version=2021-04-15 |
AZURE_COSMOS_SCOPE |
您的受控識別範圍 |
https://management.azure.com/.default |
AZURE_COSMOS_CLIENTID |
您的用戶端密碼識別碼 |
<client-ID> |
AZURE_COSMOS_CLIENTSECRET |
您的用戶端密碼 |
<client-secret> |
AZURE_COSMOS_TENANTID |
您的租用戶識別碼 |
<tenant-ID> |
AZURE_COSMOS_RESOURCEENDPOINT |
您的資源端點 |
https://<database-server>.documents.azure.com:443/ |
範例指令碼
請參閱下列步驟和程式碼,使用服務主體連線至 Azure Cosmos DB for NoSQL。
安裝相依性。
dotnet add package Microsoft.Azure.Cosmos
dotnet add package Azure.Identity
使用 Azure.Identity
NuGet 套件進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
using Microsoft.Azure.Cosmos;
using Azure.Core;
using Azure.Identity;
using System;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// TokenCredential credential = new DefaultAzureCredential();
// For user-assigned identity.
// TokenCredential credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// }
// );
// For service principal.
// TokenCredential credential = new ClientSecretCredential(
// tenantId: Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID")!,
// clientId: Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID")!,
// clientSecret: Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET")!,
// options: new TokenCredentialOptions()
// );
// Create a new instance of CosmosClient using the credential above
using CosmosClient client = new(
accountEndpoint: Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT")!,
tokenCredential: credential
);
在您的 pom.xml 中新增下列相依性:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-cosmos</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.5</version>
</dependency>
透過 azure-identity
進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_COSMOS_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_COSMOS_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_COSMOS_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_COSMOS_TENANTID>"))
// .build();
String endpoint = System.getenv("AZURE_COSMOS_RESOURCEENDPOINT");
CosmosClient cosmosClient = new CosmosClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
- 安裝相依性。
pip install azure-identity
pip install azure-cosmos
- 使用
azure-identity
程式庫進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
import os
from azure.cosmos import CosmosClient
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_COSMOS_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_COSMOS_TENANTID')
# client_id = os.getenv('AZURE_COSMOS_CLIENTID')
# client_secret = os.getenv('AZURE_COSMOS_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
endpoint = os.environ["AZURE_COSMOS_RESOURCEENDPOINT"]
client = CosmosClient(url=endpoint, credential=cred)
- 安裝相依性。
go get t github.com/Azure/azure-sdk-for-go/sdk/azidentity
go get github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos
- 使用
azidentity
npm 套件進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
import (
"os"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)
func main() {
endpoint = os.Getenv("AZURE_COSMOS_RESOURCEENDPOINT")
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// For user-assigned identity.
// clientid := os.Getenv("AZURE_COSMOS_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// For service principal.
// clientid := os.Getenv("AZURE_COSMOS_CLIENTID")
// tenantid := os.Getenv("AZURE_COSMOS_TENANTID")
// clientsecret := os.Getenv("AZURE_COSMOS_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
client, err := azcosmos.NewClient(endpoint, cred, nil)
}
安裝相依性。
npm install @azure/identity
npm install @azure/cosmos
使用 @azure/identity
npm 套件進行驗證,並從服務連接器新增的環境變數取得端點 URL。 使用下列程式代碼時,請取消註解您想要使用的驗證類型代碼段部分。
import { CosmosClient } from "@azure/cosmos";
const { DefaultAzureCredential } = require("@azure/identity");
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// const credential = new DefaultAzureCredential();
// For user-assigned managed identity.
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: process.env.AZURE_COSMOS_CLIENTID
// });
// For service principal.
// const credential = new ClientSecretCredential(
// tenantId: process.env.AZURE_COSMOS_TENANTID,
// clientId: process.env.AZURE_COSMOS_CLIENTID,
// clientSecret: process.env.AZURE_COSMOS_CLIENTSECRET
// );
// Create a new instance of CosmosClient using the credential above
const cosmosClient = new CosmosClient({
process.env.AZURE_COSMOS_RESOURCEENDPOINT,
aadCredentials: credential
});
下一步
請遵循下方列出的教學課程以深入了解服務連接器。