调用CreateContainerIfNotExistsAsync创建Azure Cosmos DB容器失败:无服务器账户不支持设置吞吐量
问题解决:无服务器Azure Cosmos DB创建容器报BadRequest(400)错误
错误原因
报错信息明确说明:无服务器账户不支持为容器设置提供吞吐量或自动驾驶功能。无服务器模式的Cosmos DB采用按需计费模型,不需要预配固定吞吐量(如400 RU/s),而你在调用CreateContainerIfNotExistsAsync时传入了throughput: 400参数,这直接触发了请求拒绝。
修复后的代码
移除CreateContainerIfNotExistsAsync方法中的throughput参数即可:
private static async Task AddContainerTestAsync() { try { var keys = new List<string> { "/source", "/key" }; // 分层分区键列表,最多支持三级 List<string> subpartitionKeyPaths = keys; // 创建容器属性对象 ContainerProperties containerProperties = new ContainerProperties( id: "test_container", partitionKeyPaths: subpartitionKeyPaths ); // 创建带分层分区键的容器(无服务器账户无需指定吞吐量) Microsoft.Azure.Cosmos.Container container = await cosmosDb.CreateContainerIfNotExistsAsync(containerProperties); Console.WriteLine($"created: {container.Id}"); } catch (Exception e) { Console.WriteLine($"Error CreateContainer : {e.Message} {e.StackTrace}."); } }
补充说明
- 无服务器模式下,所有容器共享账户的按需资源池,无需为单个容器预配吞吐量。
- 只有预配吞吐量模式的Cosmos DB账户,才需要为容器或数据库指定RU/s数值。
内容的提问来源于stack exchange,提问作者bruce neiman
相关产品推荐
相关产品推荐

