Tutorial: Automate resizing uploaded images using Event Grid
Azure Event Grid is an eventing service for the cloud. Event Grid enables you to create subscriptions to events raised by Azure services or third-party resources.
This tutorial is part two of a series of Storage tutorials. It extends the previous Storage tutorial to add serverless automatic thumbnail generation using Azure Event Grid and Azure Functions. Event Grid enables Azure Functions to respond to Azure Blob storage events and generate thumbnails of uploaded images. An event subscription is created against the Blob storage create event. When a blob is added to a specific Blob storage container, a function endpoint is called. Data passed to the function binding from Event Grid is used to access the blob and generate the thumbnail image.
You use the Azure CLI and the Azure portal to add the resizing functionality to an existing image upload app.
In this tutorial, you learn how to:
- Create a general Azure Storage account
- Deploy serverless code using Azure Functions
- Create a Blob storage event subscription in Event Grid
This article has been updated to use the new Azure PowerShell Az module. You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. For Az module installation instructions, see Install Azure PowerShell.
To complete this tutorial:
You must have completed the previous Blob storage tutorial: Upload image data in the cloud with Azure Storage.
If you've not previously registered the Event Grid resource provider in your subscription, make sure it's registered.
az provider register --namespace Microsoft.EventGrid
Use Azure Cloud Shell
Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. Cloud Shell lets you use either
PowerShell to work with Azure services. You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.
To launch Azure Cloud Shell:
|Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell.|
|Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser.|
|Select the Cloud Shell button on the top-right menu bar in the Azure portal.|
To run the code in this article in Azure Cloud Shell:
- Launch Cloud Shell.
- Select the Copy button on a code block to copy the code.
- Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.
- Press Enter to run the code.
If you choose to install and use the CLI locally, this tutorial requires the Azure CLI version 2.0.14 or later. Run
az --version to find the version. If you need to install or upgrade, see Install Azure CLI.
If you are not using Cloud Shell, you must first sign in using
Create an Azure Storage account
Azure Functions requires a general storage account. In addition to the Blob storage account you created in the previous tutorial, create a separate general storage account in the resource group by using the az storage account create command. Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only.
Set a variable to hold the name of the resource group that you created in the previous tutorial.
Set a variable for the name of the new storage account that Azure Functions requires.
functionstorage=<name of the storage account to be used by the function>
Create the storage account for the Azure function.
az storage account create --name $functionstorage --location southeastasia \ --resource-group $resourceGroupName --sku Standard_LRS --kind storage
Create a function app
You must have a function app to host the execution of your function. The function app provides an environment for serverless execution of your function code. Create a function app by using the az functionapp create command.
In the following command, provide your own unique function app name. The function app name is used as the default DNS domain for the function app, and so the name needs to be unique across all apps in Azure.
Specify a name for the function app that's to be created.
functionapp=<name of the function app>
Create the Azure function.
az functionapp create --name $functionapp --storage-account $functionstorage \ --resource-group $resourceGroupName --consumption-plan-location southeastasia
Now you must configure the function app to connect to the Blob storage account you created in the previous tutorial.
Configure the function app
The function needs credentials for the Blob storage account, which are added to the application settings of the function app using the az functionapp config appsettings set command.
blobStorageAccount=<name of the Blob storage account you created in the previous tutorial> storageConnectionString=$(az storage account show-connection-string --resource-group $resourceGroupName \ --name $blobStorageAccount --query connectionString --output tsv) az functionapp config appsettings set --name $functionapp --resource-group $resourceGroupName \ --settings AzureWebJobsStorage=$storageConnectionString THUMBNAIL_CONTAINER_NAME=thumbnails \ THUMBNAIL_WIDTH=100 FUNCTIONS_EXTENSION_VERSION=~2
FUNCTIONS_EXTENSION_VERSION=~2 setting makes the function app run on version 2.x of the Azure Functions runtime.
You can now deploy a function code project to this function app.
Deploy the function code
az functionapp deployment source config --name $functionapp --resource-group $resourceGroupName --branch master --manual-integration --repo-url https://github.com/Azure-Samples/function-image-upload-resize
The image resize function is triggered by HTTP requests sent to it from the Event Grid service. You tell Event Grid that you want to get these notifications at your function's URL by creating an event subscription. For this tutorial you subscribe to blob-created events.
The data passed to the function from the Event Grid notification includes the URL of the blob. That URL is in turn passed to the input binding to obtain the uploaded image from Blob storage. The function generates a thumbnail image and writes the resulting stream to a separate container in Blob storage.
This project uses
EventGridTrigger for the trigger type. Using the Event Grid trigger is recommended over generic HTTP triggers. Event Grid automatically validates Event Grid Function triggers. With generic HTTP triggers, you must implement the validation response.
To learn more about this function, see the function.json and run.csx files.
The function project code is deployed directly from the public sample repository. To learn more about deployment options for Azure Functions, see Continuous deployment for Azure Functions.
Create an event subscription
An event subscription indicates which provider-generated events you want sent to a specific endpoint. In this case, the endpoint is exposed by your function. Use the following steps to create an event subscription that sends notifications to your function in the Azure portal:
In the Azure portal, select All Services on the left menu, and then select Function Apps.
Expand your function app, choose the Thumbnail function, and then select Add Event Grid subscription.
Use the event subscription settings as specified in the table.
Setting Suggested value Description Name imageresizersub Name that identifies your new event subscription. Topic type Storage accounts Choose the Storage account event provider. Subscription Your Azure subscription By default, your current Azure subscription is selected. Resource group myResourceGroup Select Use existing and choose the resource group you have been using in this tutorial. Resource Your Blob storage account Choose the Blob storage account you created. Event types Blob created Uncheck all types other than Blob created. Only event types of
Microsoft.Storage.BlobCreatedare passed to the function.
Subscriber type autogenerated Pre-defined as Web Hook. Subscriber endpoint autogenerated Use the endpoint URL that is generated for you.
Switch to the Filter tab, and do the following actions:
Select Enable subject filtering option.
For Subject begins with, enter the following value : /blobServices/default/containers/images/blobs/.
Select Create to add the event subscription. This creates an event subscription that triggers
Thumbnailfunction when a blob is added to the
imagescontainer. The function resizes the images and adds them to the
Now that the backend services are configured, you test the image resize functionality in the sample web app.
Test the sample app
To test image resizing in the web app, browse to the URL of your published app. The default URL of the web app is
Click the Upload photos region to select and upload a file. You can also drag a photo to this region.
Notice that after the uploaded image disappears, a copy of the uploaded image is displayed in the Generated thumbnails carousel. This image was resized by the function, added to the thumbnails container, and downloaded by the web client.