Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

azure function c# http trigger blob output

Can someone describe me how I can configure a C# azure function which uses an HTTP input trigger and a blob storage output trigger?

Maybe also with an example code snippet and an example function.json. I don't get it to work locally with the azure functions core tools.

like image 233
aumanjoa Avatar asked Jun 24 '19 20:06

aumanjoa


People also ask

What is a Azure function?

Azure Functions is a serverless solution that allows you to write less code, maintain less infrastructure, and save on costs. Instead of worrying about deploying and maintaining servers, the cloud infrastructure provides all the up-to-date resources needed to keep your applications running.

What language is Azure Functions?

With Azure Functions, you can choose between several programming languages depending on which runtime you select. The 1. x runtime supports C# (Full . NET Framework), JavaScript (Node 6), F# (Framework 4.7), and a few languages to experiment with (Python, TypeScript, PHP, Bash, and PowerShell), while 2.

Are Azure Functions the same as lambda?

Azure Functions, compared to AWS Lambda and Google Cloud Functions, is more flexible and complex about how users deploy serverless functions as part of a larger workload. Azure Functions users can deploy code directly on the Azure Functions service or run the software inside Docker containers.


2 Answers

This is a combined HTTP triggered function with a output blob binding:

[FunctionName("HttpTriggeredFunction")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest httpRequest,
    [Blob("blobcontainer", Connection = "StorageConnectionString")] CloudBlobContainer outputContainer,
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    await outputContainer.CreateIfNotExistsAsync();

    var requestBody = await new StreamReader(httpRequest.Body).ReadToEndAsync();
    var blobName = Guid.NewGuid().ToString();

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

    return new OkObjectResult(blobName);
}

It uses the CloudBlobContainer output type to get a reference to the blob container which then enables you to use methods such as .GetBlockBlobReference("blobPath") to get a reference to a blob.

Once you have a reference to a blob, you can use different methods to upload:

  • cloudBlockBlob.UploadFromByteArrayAsync()
  • cloudBlockBlob.UploadFromFileAsync()
  • cloudBlockBlob.UploadTextAsync()
  • cloudBlockBlob.UploadFromStreamAsync()

To get it running locally, you need set some things up. Notice in my example the attribute [Blob("blobcontainer", Connection = "StorageConnectionString")]

  • "blobcontainer" this can be whatever you want and will be the name of the container that will be created in your storage account by this line outputContainer.CreateIfNotExistsAsync(); (if it doesn't exist already).
  • Connection = "StorageConnectionString" this can be a setting in your local.settings.json for the connection string of your storage account. When developing locally I would recommend setting this to "UseDevelopmentStorage=true" so that you can take advantage of the storage emulator. Then when you are ready to deploy onto Azure, you would create a setting in the function app containing the real connection string.

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",

    "StorageConnectionString": "UseDevelopmentStorage=true"
  }
}
like image 79
Chris B Avatar answered Oct 27 '22 06:10

Chris B


to make an http function that saves to Blob Storage use this code:

 #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)
{
   
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    outputBlob.WriteLine(requestBody);
 
    string result =  "{ 'result':  'ok' }";
    dynamic data = JsonConvert.DeserializeObject(result);
      
    return new OkObjectResult(data);
}
 

You need to set the output binding:

enter image description here

You can then run a test posting content on the test window

enter image description here

like image 23
CodeSerendipity Avatar answered Oct 27 '22 04:10

CodeSerendipity