【问题标题】:Create a Cosmos Db and Container in C#在 C# 中创建 Cosmos Db 和容器
【发布时间】:2023-01-30 22:20:10
【问题描述】:

我正在尝试使用 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 错误,我也无法让它工作,即使有贡献者权限,但我知道这是一个错误,因为你应该使用资源管理器。

任何建议,将不胜感激。

【问题讨论】:

    标签: c# azure azure-cosmosdb


    【解决方案1】:

    我没有使用过无密码访问,但您是否尝试过明确提供 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");
    

    【讨论】:

    • 感谢您的评论,虽然使用数据库密钥与 Cosmos DB 客户端一起使用,但根据故障排除页面,DefaultAzureCredentials 不支持它“通过 Azure 资源管理器、Azure 门户、Azure CLI 或 Azure PowerShell 执行操作。如果您正在使用 Azure Functions Azure Cosmos DB 触发器,确保触发器的 CreateLeaseContainerIfNotExists 属性未设置为 true。使用 Azure AD 身份会阻止任何非数据操作,例如创建租用容器。 learn.microsoft.com/en-us/azure/cosmos-db/nosql/…
    猜你喜欢
    • 2021-12-01
    • 2021-02-13
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    相关资源
    最近更新 更多