简体   繁体   English

Azure function HTTP 触发器 Blob 存储 Z78E6221F6393D14356681DB3ZD8 绑定

[英]Azure function HTTP trigger Blob storage output binding

I try to develop HTTP trigger azure function using C# and .net 3.0.1 (runtime ~3) and Visual Studio 2019. In my function I want to write data into blob and I want to be able set destination file name from request body. I try to develop HTTP trigger azure function using C# and .net 3.0.1 (runtime ~3) and Visual Studio 2019. In my function I want to write data into blob and I want to be able set destination file name from request body. I use the following code:我使用以下代码:

public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            [Blob("reports/{reportname}", FileAccess.Write)] TextWriter report,
            ILogger log)
        {

But when I run function, I got error:但是当我运行 function 时,我得到了错误:

Unable to resolve binding parameter 'reportname'. Binding expressions must map to either a value provided by the trigger or a property of the value the trigger is bound to, or must be a system binding expression (e.g. sys.randguid, sys.utcnow, etc.).

I already read about bindings but I completely don't understand how to create correct binding or how to add binding into function.json file.我已经阅读了有关绑定的信息,但我完全不明白如何创建正确的绑定或如何将绑定添加到 function.json 文件中。

Can anyone please help/explain what do I do wrong?谁能帮助/解释我做错了什么?

Thanks!谢谢!

Firstly, you need to provide a value for {reportname} as the error message says.首先,您需要为{reportname}提供一个值,如错误消息所述。

As you are using a [HttpTrigger] , you can provide the value in a POST request body:当您使用[HttpTrigger]时,您可以在 POST 请求正文中提供值:

{
    "reportName": "test"
}

In the [HttpTrigger] binding, instead of getting a HttpRequest object, you can get the binding to pass you a deserialized object to represent your POST request ie MyRequest :[HttpTrigger]绑定中,您可以获取绑定以向您传递反序列化的 object 来表示您的 POST 请求,即 MyRequest ,而不是获取HttpRequest MyRequest

[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] MyRequest request

When you send a request, the ReportName property will then get mapped in the [Blob] binding to the path: reports/{reportname} .当您发送请求时, ReportName属性将在[Blob]绑定中映射到路径: reports/{reportname} This is the path in blob storage where your file will be stored.这是存储文件的 blob 存储路径。

You can then use the output TextWriter report parameter to add text to your blob:然后,您可以使用 output TextWriter report参数将文本添加到您的 blob:

report.WriteLineAsync("content");

Or you may wish to also send the report content in the POST body:或者您可能还希望在 POST 正文中发送报告内容:

public class MyRequest
{
    public string ReportName { get; set; }

    public string ReportContent { get; set; }
}

report.WriteLineAsync(request.ReportContent);

Full working example:完整的工作示例:

public static class HttpTriggeredFunction
{
    [FunctionName("HttpTriggeredFunction")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "reports")] MyRequest request,
        [Blob("reports/{reportname}", FileAccess.Write)] TextWriter report,
        ILogger log)
    {
        await report.WriteLineAsync(request.ReportContent);

        return new OkResult();
    }
}

public class MyRequest
{
    public string ReportName { get; set; }

    public string ReportContent { get; set; }
}

邮递员请求

Blob 存储

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM