MSH
MSH

Reputation: 19

Azure Function with two output bindings (Azure Queue & Blob Storage)

Is it possible to write an azure http trigger function with two output bindings, so that data not only outputs to an azure queue but also sends the same message to an azure blob storage?

Upvotes: 1

Views: 1726

Answers (2)

Rich
Rich

Reputation: 2347

If you're running your function app in an isolated process, Microsoft has a brief discussion and example here:

Isolated process - Multiple output bindings

Upvotes: 0

Markus Meyer
Markus Meyer

Reputation: 3937

Please find a sample for multiple outbound bindings here:

    public async Task<IActionResult> Sample(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "sample")] HttpRequest req,
[Queue("sample", Connection = "StorageAccountString")] CloudQueue sampleQueue,
[Blob("sample", FileAccess.Read, Connection = "StorageAccountString")] CloudBlobContainer sampleContainer,
ExecutionContext context,
ILogger log)
    {
        var requestBody = await req.ReadAsStringAsync().ConfigureAwait(false);

        var queueMessage = new CloudQueueMessage(requestBody);
        await sampleQueue.AddMessageAsync(queueMessage).ConfigureAwait(false);

        CloudBlockBlob blob = sampleContainer.GetBlockBlobReference($"{Guid.NewGuid().ToString()}");
        blob.UploadText(requestBody);

        return new OkResult();
    }

Here you can find multiple Blob outbound bindings:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-output?tabs=in-process%2Cextensionv5&pivots=programming-language-csharp#example

**using System.Collections.Generic;
using System.IO;
using Microsoft.Azure.WebJobs;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

public class ResizeImages
{
    [FunctionName("ResizeImage")]
    public static void Run([BlobTrigger("sample-images/{name}")] Stream image,
        [Blob("sample-images-sm/{name}", FileAccess.Write)] Stream imageSmall,
        [Blob("sample-images-md/{name}", FileAccess.Write)] Stream imageMedium)
    {
        IImageFormat format;

        using (Image<Rgba32> input = Image.Load<Rgba32>(image, out format))
        {
            ResizeImage(input, imageSmall, ImageSize.Small, format);
        }

        image.Position = 0;
        using (Image<Rgba32> input = Image.Load<Rgba32>(image, out format))
        {
            ResizeImage(input, imageMedium, ImageSize.Medium, format);
        }
    }

    public static void ResizeImage(Image<Rgba32> input, Stream output, ImageSize size, IImageFormat format)
    {
        var dimensions = imageDimensionsTable[size];

        input.Mutate(x => x.Resize(dimensions.Item1, dimensions.Item2));
        input.Save(output, format);
    }

    public enum ImageSize { ExtraSmall, Small, Medium }

    private static Dictionary<ImageSize, (int, int)> imageDimensionsTable = new Dictionary<ImageSize, (int, int)>() {
        { ImageSize.ExtraSmall, (320, 200) },
        { ImageSize.Small,      (640, 400) },
        { ImageSize.Medium,     (800, 600) }
    };

}**

Upvotes: 1

Related Questions