Introduction
Azure Functions with an ASP.NET Core Web API, along with a real-world use case, can be a powerful way to demonstrate the capabilities of serverless computing in Azure. In this example, we'll create a serverless Azure Function that processes image uploads to a cloud storage service like Azure Blob Storage. When a user uploads an image through the ASP.NET Core Web API, the Azure Function will be triggered to perform some image processing and then save the processed image back to storage.
Here's a step-by-step guide.
1. Create an ASP.NET Core Web API
Start by creating an ASP.NET Core Web API project.
dotnet new webapi -n ImageProcessingApi
2. Create Azure Functions Project
Next, create an Azure Functions project within the same solution.
dotnet new func -n ImageProcessingFunctions
3. Install Required Packages
In the ImageProcessingApi project, you will need to install Azure.Storage.Blobs package for interacting with Azure Blob Storage.
dotnet add package Azure.Storage.Blobs
4. Implement Image Upload API
In your ImageProcessingApi project, create an API controller that handles image uploads. This controller should accept image uploads, store them in Azure Blob Storage, and trigger an Azure Function for processing. Here's a simplified example.
Author: Sardar Mudassar Ali Khan
[ApiController]
[Route("api/images")]
public class ImageController: ControllerBase
{
private readonly BlobServiceClient _blobServiceClient;
public ImageController(IConfiguration configuration)
{
_blobServiceClient = new BlobServiceClient(configuration.GetConnectionString("AzureStorage"));
}
[HttpPost("upload")]
public async Task<IActionResult> UploadImage(IFormFile file)
{
if (file == null || file.Length == 0)
return BadRequest("No file uploaded.");
var containerClient = _blobServiceClient.GetBlobContainerClient("images");
await containerClient.CreateIfNotExistsAsync();
var blobClient = containerClient.GetBlobClient(file.FileName);
await blobClient.UploadAsync(file.OpenReadStream(), true);
// Trigger the Azure Function to process the image here
return Ok("Image uploaded successfully.");
}
}
5. Create the Azure Function
In the ImageProcessingFunctions project, create an Azure Function that will be triggered when an image is uploaded to Blob Storage. Here's an example of an Azure Function that resizes an image.
using System.IO;
using System.Drawing;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
Author: Sardar Mudassar Ali Khan
public static class ImageProcessingFunction
{
[FunctionName("ProcessImage")]
public static void Run(
[BlobTrigger("images/{name}", Connection = "AzureWebJobsStorage")] Stream imageStream,
[Blob("processed/{name}", FileAccess.Write)] Stream imageOutput,
string name,
ILogger log)
{
log.LogInformation($"Processing image: {name}");
// Perform image processing (e.g., resizing)
using (var image = new Bitmap(imageStream))
{
using (var resizedImage = ResizeImage(image, 800, 600))
{
resizedImage.Save(imageOutput, ImageFormat.Jpeg);
}
}
log.LogInformation($"Image processed: {name}");
}
private static Bitmap ResizeImage(Bitmap image, int width, int height)
{
var newWidth = width;
var newHeight = height;
var ratio = Math.Min((float)newWidth / image.Width, (float)newHeight / image.Height);
newWidth = (int)(image.Width * ratio);
newHeight = (int)(image.Height * ratio);
var newImage = new Bitmap(newWidth, newHeight);
using (var graphics = Graphics.FromImage(newImage))
{
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}
}

Join the conversation! Your thoughts help the community grow.