简体   繁体   中英

How to create Azure function with C# using HTTP POST and store incoming data into text file in BLOB storage?

I am new to Azure function. I am trying to create Azure function in portal with Http trigger which gets the data as JSON and POST it as a text file in BLOB storage. I know I am missing something here in the code:

function.json

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "blob",
      "name": "outputBlob",
      "path": "outcontainer/{rand-guid}",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    }
  ]
}

run.csx

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
#r "Microsoft.Azure.WebJobs.Extensions.Storage"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.Azure.WebJobs.Extensions.Storage;

public static async Task<IActionResult> Run(HttpRequest req,
[Blob("blobcontainer", Connection = "AzureWebJobsStorage")] CloudBlobContainer outputContainer,
ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    await outputContainer.CreateIfNotExistsAsync();

    var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    var blobName = Guid.NewGuid().ToString();

    var cloudBlockBlob = outputContainer.GetBlockBlobReference(blobName);
    await cloudBlockBlob.UploadTextAsync(data);

    return new OkObjectResult(blobName);
}

It compiles successfully but getting run time error as below:

No value was provided for parameter 'outputContainer'

Ok, now with a valid and tested sample :) (although somebody else posed a similar sample now as well)

using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;

namespace SampleFunctions
{
    public static class Http2BlobFunction
    {
        [FunctionName("Http2BlobFunction")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
            [Blob("myblobcontainer/{rand-guid}.txt", FileAccess.Write)] CloudBlockBlob blob,
            ILogger log)
        {
            log.LogInformation("Received file upload request");
            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            await blob.UploadTextAsync(requestBody);
            return new OkObjectResult(blob.Name);
        }
    }
}

Maybe you could try with my code, just bind the blob and write the request.

[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[Blob("myblobcontainer/{rand-guid}.txt", FileAccess.Write)]CloudBlockBlob outputblob)
{              
    var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    await outputblob.UploadTextAsync(requestBody);
    return new OkObjectResult(outputblob);
}

I test local, it could write the request body to a txt blob.

在此处输入图片说明

在此处输入图片说明

Hope this could help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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