Skip to content

Instantly share code, notes, and snippets.

@Geertvdc
Created December 6, 2017 19:41
Show Gist options
  • Select an option

  • Save Geertvdc/0d9efc23d105ee71d6ea70a02908ec68 to your computer and use it in GitHub Desktop.

Select an option

Save Geertvdc/0d9efc23d105ee71d6ea70a02908ec68 to your computer and use it in GitHub Desktop.
Event Grid Azure Function
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using System;
using Newtonsoft.Json;
namespace StorageEventFunction
{
public static class StorageEventFunction
{
[FunctionName("StorageEventFunction")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
string jsonContent = await req.Content.ReadAsStringAsync();
dynamic eventgridinfo = JsonConvert.DeserializeObject(jsonContent);
log.Info($"function trigger with data: {eventgridinfo}");
var eventType = eventgridinfo[0].eventType.ToString();
var imageUrl = eventgridinfo[0].data.url.ToString();
log.Info($"triggering with event {eventType} and url {imageUrl}");
switch (eventType)
{
case "Microsoft.Storage.BlobCreated":
await CreateAction(imageUrl, log);
break;
case "Microsoft.Storage.BlobDeleted":
await DeleteAction(imageUrl, log);
break;
default:
throw new ArgumentException($"unsupported event type: {eventType}");
}
return req.CreateResponse(HttpStatusCode.OK);
}
private static async Task CreateAction(string url, TraceWriter log)
{
// do whatever you would like with the new file in blob storage
}
private static async Task DeleteAction(string url, TraceWriter log)
{
// do whatever you would like on delete action
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment