简体   繁体   English

如何从 Azure 门户上的 Azure 函数读取 json 文件?

[英]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.

相关问题 如何编写Azure函数以从Web门户接收数据? - How to code An Azure function to receive data from a web portal? 如何从 .NET Core API 调用 Azure 门户中的函数应用程序 - How to call function App in Azure portal from .NET Core API 使 azure function 在发布到 azure 门户而不是应用程序设置后从 Azure 密钥保管库读取连接字符串 - Make azure function to read the connection string from Azure key vault after published into azure portal not from application settings 将Azure功能从门户导入Visual Studio - Import Azure Function from portal into Visual Studio 如何从 Azure 存储队列的消息文本字段中读取 JSON 字符串(使用 Azure 函数)? - How do I read a JSON string from the Message Text field in an Azure Storage queue (using an Azure function)? 如何从Azure门户获取活动日志 - How to get Activity logs from Azure Portal 如何在没有队列触发器的情况下从 Azure function 中的 Azure 队列读取 - How to read from an Azure queue in an Azure function, without a Queue Trigger 如何使用 Azure Function 从网站下载文件 - How to download files from a website with Azure Function 如何读取 Azure Function 应用程序中的配置 json 文件 - How to read a config json file inside a Azure Function app 如何在 Azure Function 的构造函数中从 local.settings.json 文件中读取变量? - How to read the variables from local.settings.json file in the constructor in Azure Function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM