GithubHelp home page GithubHelp logo

azure-samples / function-image-upload-resize Goto Github PK

View Code? Open in Web Editor NEW
65.0 23.0 154.0 28 KB

Sample function in Azure Functions that demonstrates how to upload and resize images in Azure Storage.

License: MIT License

C# 100.00%

function-image-upload-resize's Introduction

page_type languages products description urlFragment
sample
csharp
azure
This sample demonstrates how to respond to an EventGridEvent published by a storage account to resize an image and upload a thumbnail as described in the article Automate resizing uploaded images using Event Grid.
function-image-upload-resize

Image Upload Resize

This sample demonstrates how to respond to an EventGridEvent published by a storage account to resize an image and upload a thumbnail as described in the article Automate resizing uploaded images using Event Grid.

Local Setup

Before running this sample locally, you need to add your connection string to the AzureWebJobsStorage value in a file named local.settings.json file. This file is excluded from the git repository, so an example file named local.settings.example.json is provided.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "<STORAGE_ACCOUNT_CONNECTION_STRING>",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "THUMBNAIL_CONTAINER_NAME": "thumbnails",
    "THUMBNAIL_WIDTH":  "100",
    "datatype": "binary"
  }
}

To use this file, do the following steps:

  1. Replace <STORAGE_ACCOUNT_CONNECTION_STRING> with your storage account connection string
  2. Rename the file from local.settings.example.json to local.settings.json

Version Support

The master branch of this repository contains the Functions version 2.x implementation, while the v1 branch has the Functions 1.x implementation.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

function-image-upload-resize's People

Contributors

chaitanyanaykodi-msft avatar craigshoemaker avatar ggailey777 avatar mhopkins-msft avatar microsoftopensource avatar msftgits avatar prozac777 avatar richardjortega avatar v-rajagt-zz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

function-image-upload-resize's Issues

Upgrade SixLabors.ImageSharp version

This project is still running SixLabors.ImageSharp version 1.0.0-beta0005. Should it be upgraded. I tried myself but ran into compiler errors ๐Ÿ˜”.

Can not create blob storage binding in c# using visual studio 2017

Trying to remake the this example in c# using visual studio but having issues making the azure function trigger be triggered by the event grid and bind to the blob storage.

Current Code:

using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs;
using Microsoft.WindowsAzure.Storage.Blob;

namespace FunctionApp
{
    public static class CreateIndex
    {
        [FunctionName("CreateIndex")]
        [StorageAccount("backup_STORAGE")]
        public static void Run(
            [EventGridTrigger()] EventGridEvent myEvent, 
            [Blob("{data.url}")] CloudBlockBlob inputBlob, 
            TraceWriter log)
        {
            log.Info(myEvent.ToString());
            log.Info(inputBlob.ToString());
        }
    }
}

Generated function.json:

{
  "generatedBy": "Microsoft.NET.Sdk.Functions.Generator-1.0.6",
  "configurationSource": "attributes",
  "bindings": [
    {
      "type": "eventGridTrigger",
      "name": "myEvent"
    }
  ],
  "disabled": false,
  "scriptFile": "../bin/FunctionApp.dll",
  "entryPoint": "FunctionApp.CreateIndex.Run"
}

The binding is working for the event grid trigger but not the Blob input.

CR for function.json

  • Rename EventGridTrigger from e to whatever is in the run.csx method signature.

Divisor should be double instead of integer

With the current implementation of Thumbnail.cs in the master branch, the division yielding var divisor returns an integer when in fact, a double precision number should be used. When running the code as is, aspect ratios for images are incorrect after processing. E.g. when divisor should be 1.6, a value of 1 will be returned. Same goes for divisor that should be 0.4, a value of 0 will be returned which in the end results in a "Divide by 0" exception to be thrown.

I've solved this in a forked version to explicitly cast one of the factors in the division to a double, like this:

var divisor = image.Width / (double)thumbnailWidth;

master branch doesn't fit the tutorial

The tutorial here and the same one in Storage doc seems for v1 branch as we can see csx mentioined, but the GitHub link points to master v2 branch.

Problem is there are inconsistencies between code of v1 and master.

  1. connection is not set in master branch so blob binding detects input from Storage of Function app instead of blob Storage account.

  2. Similarly, the Storage account used to save output becomes that for Function app instead of blob Storage account.

  3. "THUMBNAIL_CONTAINER_NAME": "thumbnails", "THUMBNAIL_WIDTH": "100", are not mentioned in tutorial.

Output to thumbnails container not working.

Following the tutorial here:
https://docs.microsoft.com/en-us/azure/event-grid/resize-images-on-storage-blob-upload-event?tabs=dotnet

Installation went well and when upload an image to the image container the Thumbnail function seems to run successfully.
However there are not images showing up in the thumbnail container.

Current Code:
local.settings.json
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=egthumbnail;AccountKey=xxxxxxxxx;", "FUNCTIONS_WORKER_RUNTIME": "dotnet", "THUMBNAIL_CONTAINER_NAME": "thumbnails", "THUMBNAIL_WIDTH": "100", "datatype": "binary" } }

function.json
{ "generatedBy": "Microsoft.NET.Sdk.Functions-3.0.2", "configurationSource": "attributes", "bindings": [ { "type": "eventGridTrigger", "name": "eventGridEvent" } ], "disabled": false, "scriptFile": "../bin/ImageFunctions.dll", "entryPoint": "ImageFunctions.Thumbnail.Run" }

compilation error

I was following this scenario https://docs.microsoft.com/en-us/azure/event-grid/resize-images-on-storage-blob-upload-event?toc=%2Fazure%2Fstorage%2Fblobs%2Ftoc.json&tabs=net#deploy-the-function-code

here is the error

Executing 'Functions.imageresizerfunc' (Reason='EventGrid trigger fired at 2018-10-10T23:36:18.3983661+00:00', Id=f56c4c83-3624-4673-99df-d24f6a1be981)
Function compilation error
run.csx(4,1): error CS0006: Metadata file 'ImageResizer' could not be found
run.csx(10,7): error CS0246: The type or namespace name 'ImageResizer' could not be found (are you missing a using directive or an assembly reference?)

Images with an upper case extension throw errors

The GetEncoder() function correctly checks for upper case extensions:

var isSupported = Regex.IsMatch(extension, "gif|png|jpe?g", RegexOptions.IgnoreCase);

However, the switch statement following does not so if the uploaded image is test.PNG then errors are reported:

No encoder support for: test.PNG

CR for run.csx

  • Add a space between #r and using statements
  • Pull out hardcoded string to App Settings (i.e. thumbs)
  • Load any App Settings into static variables
  • rename the EventGridEvent to somethings more than e

Rename storage setting name to 'AzureWebJobsStorage'

"connection": "STORAGE_CONNECTION_STRING"

When you create a function using CLI, a connection string app setting gets added automatically, which is named AzureWebJobsStorage and is connected to the storage account supplied in the az functionapp create command. @richardjortega or @Prozac777, can we just use this same app setting for the existing storage account in the function binding so we don't have to explicitly add another app setting? This will make the tutorial simpler (and I don't think there any need for 2 storage accounts).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.