[英]How to read json files from an Azure function on Azure portal?
I've written a HTTP Azure function that stores data sent via Postman into an Azure Blob Storage, Queue and then send the data into a power BI data set.我编写了一个 HTTP Azure 函数,它将通过 Postman 发送的数据存储到 Azure Blob 存储、队列中,然后将数据发送到 Power BI 数据集中。
My problem is that I can only send raw data like this one for example [1]: https://i.stack.imgur.com/AiTmt.png我的问题是我只能发送这样的原始数据,例如 [1]: https ://i.stack.imgur.com/AiTmt.png
What I want to have is the ability to read JSON files ("like in previous example but in a json file")我想要的是读取 JSON 文件的能力(“就像在前面的例子中但是在一个 json 文件中”)
What should I change in my code?我应该在我的代码中更改什么?
Here's my HTTP Azure function:这是我的 HTTP Azure 函数:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log, TextWriter outputBlob, ICollector<string> outputQueueItem)
{
string connString = "https://api.powerbi.com/beta/a5d051de-762f-4405-9401-3201d0670f2c/datasets/5cd0ca8f-ed39-4b14-b57e-684d1c0d1bd4/rows?redirectedFromSignup=1&key=3hr%2FH1zs7%2Fc9QY%2BRspBDhrip1T1dljq2MWG0h2cBoEb%2Frv%2Fi8SvPLay03vRLcqXc4OSaJOLZRoF51a1q7RNhoQ%3D%3D";
string name = req.Query["name"];
string number = req.Query["number"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
number = number ?? data?.number;
outputBlob.Write(name);
outputBlob.Write(number);
HttpClient client = new HttpClient();
HttpContent content = new StringContent("[" + data + "]");
HttpResponseMessage response = await client.PostAsync(connString, content);
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.";
outputQueueItem.Add(name);
outputQueueItem.Add(number);
return new OkObjectResult(responseMessage);
}
PS: "The code is written on Azure portal with two output bindings, Blob storage & Queue" PS:“代码是在 Azure 门户上编写的,有两个输出绑定,Blob 存储和队列”
It seems you need to Post a File to the Function.看来您需要将文件发布到函数。 Use
Request.Form.Files["FILE"]
to get the Posted File.使用
Request.Form.Files["FILE"]
获取发布的文件。
Detailed Explanation on Uploading Files to Azure Function. 上传文件到 Azure Function 的详细说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.