我從 Azure Function & Cosmo DB 開始。我在 Azure 門戶中創建了一個函式應用程式,然后按照指南開始使用 VS Code:
npm install -g azure-functions-core-tools@4 --unsafe-perm true
Then New Project
Then New function, selected the HTTP trigger template
運行 F5 并部署時,它可以作業。
然后,我在門戶中創建了一個“Azure Cosmos DB API for MongoDB”資料庫。在呼叫我的方法時,我按照此發布了一個檔案:
https://docs.microsoft.com/en-us/azure/azure-functions/functions-add-output-binding-cosmos-db-vs-code?tabs=in-process&pivots=programming-language-csharp
所以我目前的結果是:一個函式:
namespace TakeANumber
{
public static class TestFunc
{
[FunctionName("TestFunc")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[CosmosDB(databaseName: "cosmodb-take-a-number", collectionName: "take-a-number", ConnectionStringSetting = "cosmoDbConnectionString")] IAsyncCollector<dynamic> documentsOut,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
if (!string.IsNullOrEmpty(name))
{
// Add a JSON document to the output container.
await documentsOut.AddAsync(new
{
// create a random ID
id = System.Guid.NewGuid().ToString(),
name = name
});
}
return new OkObjectResult(responseMessage);
}
}
}
具有 cosmoDbConnectionString 設定的 local.settings.json 檔案,其中包含 mongo db 連接字串。
當我運行該函式時,我得到了這個:
[2022-04-21T17:40:34.078Z] Executed 'TestFunc' (Failed, Id=b69a625c-9055-48bd-a5fb-d3c3b3a6fb9b, Duration=4ms)
[2022-04-21T17:40:34.079Z] System.Private.CoreLib: Exception while executing function: TestFunc. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'documentsOut'. Microsoft.Azure.DocumentDB.Core: Value cannot be null. (Parameter 'authKeyOrResourceToken | secureAuthKey').
我的猜測是它需要一個帶有另一種訪問令牌的 Core SQL 資料庫。我的問題:是否可以從 Azure 函式連接到 Azure CosmoDB for MongoDB?
uj5u.com熱心網友回復:
如果使用開箱即用的系結,則只能使用 Cosmos DB 的 SQL API。
您可以完全使用 MongoDB API,但您必須安裝 MongoDB 客戶端 SDK 并以編程方式處理您的資料(就像您使用任何其他面向代碼的方法一樣)。
由于您的示例代碼正在接收資料并將資料寫入 Cosmos DB,因此您可以通過 MongoDB 的 node/c#/python/etc 驅動程式進行寫入(我相信他們仍然稱它們為驅動程式),這有效地為您提供了一個db.collection.insert( {} ) 或更多的東西復雜的。
有關 Cosmos DB 系結的更多資訊,請點擊此處。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/461508.html
標籤:mongodb 天蓝色 天蓝色函数 天蓝色宇宙数据库 天蓝色 cosmosdb-mongoapi
上一篇:Azurekusto無法創建圖表
下一篇:如何為AHB配置許可證
