This sample shows how to take image files as a input via BlobTrigger, processes the file to create thumbnails, and then outputs to another image file blob using BlobOutput binding. Note there is an alternate version in the blobclient
branch showing how to use richer features of the Azure Storage blob SDK instead of simple output binding.
- .NET 6 SDK or higher
- Azure Functions Core Tools
- Add this `local.settings.json`` file to this /files/dotnet folder to simplify local development using azurite (storage emulator).
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
}
}
- Azure Storage Explorer, Azure Storage extension for VSCode, or storage explorer features of Azure Portal
- Azurite
The easiest way to install Azurite is using a Docker container or the support built into Visual Studio. To start container, enter this in a new Terminal:
docker run -d -p 10000:10000 -p 10001:10001 -p 10002:10002 mcr.microsoft.com/azure-storage/azurite
- An Azure subsubcription
- Azure Developer CLI
- Open
dotnet.sln
using Visual Studio 2022 or later. - Press Run/F5 to run in the debugger
- Open Storage Explorer, Storage Accounts -> Emulator -> Blob Containers -> and create a container
images
if it does not already exist - Copy any image file into the
images
container
You will see blob processing in Terminal standard out. The result will be saved in a another image file in the images-thumbnails
blob container.
- Open a new terminal and do the following:
func start
- Open Storage Explorer, Storage Accounts -> Emulator -> Blob Containers -> and create a container
images
if it does not already exist - Copy any image file into the
images
container
You will see blob processing in Terminal standard out. The result will be saved in a another image file in the images-thumbnails
blob container.
The easiest way to deploy this app is using the Azure Dev CLI aka AZD. If you open this repo in GitHub CodeSpaces the AZD tooling is already preinstalled.
To provision and deploy:
azd up
The main logic is contained in the CreateThumbnail.cs. When an image is uploaded to the blob container, the BlobTrigger will fire and call this function passing you the stream of what was uploaded. The body of the function is our user code that handles the conversion of the blob stream to a smaller thumbnail (have fun and put any logic you want here). Then, simply by returning the output stream as a Byte array with return outputBlob.ToArray()
, the Blob output trigger will persist to a new blob in a new container.
Here is the most interesting code:
[Function(nameof(CreateThumbnail))]
[BlobOutput("images-thumbnails/thumbnail.jpg", Connection = "AzureWebJobsStorage")]
public async Task<Byte[]> Run([BlobTrigger("images/{name}", Connection = "AzureWebJobsStorage")] Stream stream, string name)
{
_logger.LogInformation($"Processing blob\n Name: {name} \n Data: {name.Length}");
using (var image = Image.Load(stream))
{
// Generate thumbnail
image.Mutate(async x => x.Resize(new ResizeOptions
{
Size = new Size(thumbnailWidth, thumbnailHeight),
Mode = ResizeMode.Max
}));
var outputBlob = new MemoryStream();
image.Save(outputBlob, new JpegEncoder());
// Save the thumbnail to the output blob
_logger.LogInformation($"Finished processing blob\n Name:{name} and saved to output blob");
return outputBlob.ToArray();
}
}