0
votes

I am playing around with Azure functions and Image manipulation, then outputting to a separate blob container.

I managed to find this "official" example on github https://github.com/Azure/azure-webjobs-sdk-templates/blob/dev/Functions.Templates/Templates/ImageResizer-CSharp/run.csx

using ImageResizer;

public static void Run(
    Stream image,                           // input blob, large size
    Stream imageSmall, Stream imageMedium)  // output blobs
{
    var imageBuilder = ImageResizer.ImageBuilder.Current;
    var size = imageDimensionsTable[ImageSize.Small];

    imageBuilder.Build(
        image, imageSmall, 
        new ResizeSettings(size.Item1, size.Item2, FitMode.Max, null), false);

    image.Position = 0;
    size = imageDimensionsTable[ImageSize.Medium];

    imageBuilder.Build(
        image, imageMedium,
        new ResizeSettings(size.Item1, size.Item2, FitMode.Max, null), false);
}

public enum ImageSize
{
    ExtraSmall, Small, Medium
}

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

This is using a run.csx file, however I am coding in Visual Studio 2017 Preview using a "Azure Functions" project template, which uses .cs files. So everything is pretty much the same in my code, but I have my function signature as

[FunctionName("watermark-func")]
public static void Run(
    [BlobTrigger("images/{name}", Connection = "myStorage_STORAGE")]Stream image,
    [Blob("watermarked-images/s-{name}", Connection = "myStorage_STORAGE")]Stream imageSmall,
    [Blob("watermarked-images/m-{name}", Connection = "myStorage_STORAGE")]Stream imageMedium,
    TraceWriter log)

This code works like a charm, it will take the incoming image, then resize to a medium and small, then save to my out container. The issue I have though is that the Content Type of these newly created images is set to application/octet-stream?

Is there a way I can specify the correct type of image/jpeg? I have looked into using a CloudBlockBlob and setting the Properties.ContentType = "image/jpeg" when Stream imageSmall is CloudBlockBlob imageSmall, however I get an error

Cannot bind blob to CloudBlockBlob using access write.

1

1 Answers

4
votes

When binding to a CloudBlockBlob you need to specify the correct access in the attribute constructor, which needs to be ReadWrite in this case.

Here's an example of what your code would look like

[Blob("watermarked-images/s-{name}", FileAccess.ReadWrite,  Connection = "myStorage_STORAGE")]CloudBlockBlob imageSmall