Achindra Bhatnagar
Published © MIT

Azure Function Apps; Running Custom Code on Blob Insertion

Writing SQL style triggers or stitching action to events in Azure has become easier.

IntermediateProtip4 hours4,331
Azure Function Apps; Running Custom Code on Blob Insertion

Things used in this project

Story

Read more

Code

Code snippet

C#
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;

public static void Run(Stream inputBlob, Stream thumbnailBlob, TraceWriter log)
{
    string SubscriptionKey = "CognitiveServiceKey";

    using (var httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", SubscriptionKey);
        using (HttpContent content = new StreamContent(inputBlob))
        {
            content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/octet-stream");
            var uri = "https://api.projectoxford.ai/vision/v1.0/generateThumbnail?width=300&height=300&smartCropping=true";
            var response = httpClient.PostAsync(uri, content).Result;
            var responseBytes = response.Content.ReadAsByteArrayAsync().Result;

            //write to output thumb
            thumbnailBlob.Write(responseBytes, 0, responseBytes.Length);
        }
    }
}

Credits

Achindra Bhatnagar

Achindra Bhatnagar

20 projects • 161 followers
Windows Kernel Hacker, IoT Hobbyist, Enthusiast, Developer and Dreamer

Comments