我正在尝试使用 C# 创建数据库和容器我正在尝试使用最新版本的
Azure.ResourceManager.CosmosDB 1.2.0
。
在之前的版本中我使用过:
var client = new CosmosDBManagementClient("someendpoint", new DefaultAzureCredential());
var database = await client.SqlResources.StartCreateUpdateSqlDatabaseAsync(
AzureResourceGroup, AccountName, DatabaseName,
new SqlDatabaseCreateUpdateParameters(
new SqlDatabaseResource(DatabaseName),
new CreateUpdateOptions()
)
);
var armcontainer = await client.SqlResources.StartCreateUpdateSqlContainerAsync(
AzureResourceGroup, AccountName, DatabaseName,
ContainerName,
GetContainerParameters()
);
但是,
CosmosDBManagementClient
已不在图书馆中。
我知道有:
var client = new CosmosClient(endpoint, new DefaultAzureCredential());
await client.CreateDatabaseIfNotExistsAsync("testing",
throughput: null, new RequestOptions());
由于 403 错误,我也无法让它工作,即使有贡献者权限,但我知道这是一个错误,因为你应该使用资源管理器。
如有任何建议,我们将不胜感激。
我没有使用过无密码访问,但您是否尝试过明确提供 ID?
string userAssignedClientId = "<your managed identity client Id>";
var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = userAssignedClientId });
var client = new CosmosClient(endpoint, new DefaultAzureCredential());
如果这不起作用,您可以尝试连接字符串方法:
using CosmosClient client = new(
accountEndpoint: "endpoint here",
authKeyOrResourceToken: "key here");
您仍然可以使用 Azure.ResourceManager.CosmosDB 包(v1.3.2)来创建数据库和容器。
您需要的“管理器”类(用于 DI):
string subscriptionId = "XXX";
string resourceGroupName = "YYY";
string accountName = "ZZZ";
var resourceManager = new ArmClient(new DefaultAzureCredential(), subscriptionId);
var resourceId = CosmosDBAccountResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, accountName);
var cosmosAccount = resourceManager.GetCosmosDBAccountResource(resourceId);
创建新数据库
string databaseName = "new_db_name";
var databases = cosmosAccount.GetCosmosDBSqlDatabases();
bool databaseExists = await databases.ExistsAsync(databaseName, cancellationToken);
if (!databaseExists)
{
var requestContent = new CosmosDBSqlDatabaseCreateOrUpdateContent(new AzureLocation(), new CosmosDBSqlDatabaseResourceInfo(databaseName));
// you also might want to set scaling options
// requestContent.Options = new CosmosDBCreateUpdateConfig() { Throughput = _options.Throughput };
var response = await databases.CreateOrUpdateAsync(WaitUntil.Completed, databaseName, requestContent, cancellationToken);
// That's how you can access it
var createdDatabase = response.Value;
}
创建一个新容器
string containerName = "new_container_name";
var database = await databases.GetAsync(databaseName, cancellationToken);
var containers = database.GetCosmosDBSqlContainers();
bool containerExists = await containers.ExistsAsync(containerName, cancellationToken);
if (!containerExists)
{
var containerPartitionKey = new CosmosDBContainerPartitionKey();
containerPartitionKey.Paths.Add("/yourContainerPartitionKey");
// you can also configure other options such as indexing policy
var resource = new CosmosDBSqlContainerResourceInfo(containerName)
{
PartitionKey = containerPartitionKey
}
var requestContent = new CosmosDBSqlContainerCreateOrUpdateContent(new AzureLocation(), resource);
var response = await containers.CreateOrUpdateAsync(WaitUntil.Completed, containerName, requestContent, cancellationToken);
// That's how you can access it
var createdContainer = response.Value;
}