Building a Serverless Image Processing Service with Function Compute and Event Bus
This article explains how to build a serverless image‑processing pipeline that automatically compresses and watermarks uploaded pictures by deploying Go functions, configuring event‑bus rules, enabling OBS notifications, and linking them together, highlighting cost, scalability, and maintenance advantages over traditional architectures.
In the digital era, image processing is essential for many applications such as e‑commerce platforms (automatic optimization, compression, watermarking), social media (filtering and resizing), and content management systems (format conversion and quality control).
Traditional service architectures for large‑scale image handling often suffer from high cost, poor scalability, and complex maintenance. Serverless architecture offers a flexible, efficient, and cost‑controlled alternative.
Serverless Overview
Serverless automatically manages underlying compute resources, allowing developers to focus on business logic. Developers upload code, and the platform runs and scales it automatically. Function as a Service (FaaS) is the core component, executing code snippets on event triggers. An event bus routes events from multiple sources, eliminating the need for custom listeners and making the system more modular and scalable.
The article first reviews the traditional micro‑service image‑processing flow (illustrated by a diagram) and then presents a simplified Serverless flow, emphasizing lower cost, better scalability, and easier maintenance.
Image Processing Steps
The complete image‑processing workflow includes:
Deploy Function (Step 1): Deploy a function that performs image compression and watermarking.
Configure Rules (Step 4): Define event‑bus rules to listen for specific events.
Enable Notification (Step 7): Turn on OBS bucket event notifications.
Upload Image (Step 10): Users upload images to the OBS bucket.
Event Notification (Step 11): OBS sends a notification to the event bus.
Trigger Rule (Step 12): The event bus filters events and forwards them to the function.
Function Processing (Step 14): The function processes the image.
Store Image (Step 15): The function uploads the processed image back to a designated OBS bucket (source and target buckets must differ to avoid loops).
The deployment process can be divided into four main steps: Deploy Function, Configure Event Rules, Enable Notification, and Upload Image for testing.
Step 1: Configure Function Compute
The function receives events from the event bus and executes image compression and watermarking. Below is a Go example.
Write Code
Use the language supported by Function Compute (Go in this case) to define how images are processed.
... // download image from OBS
bucket = record.S3.Bucket.Name
region = record.AwsRegion
fileName := record.S3.Object.Key
inPath := fileName
fileNameAll := path.Base(fileName)
fileSuffix := path.Ext(fileName)
filePrefix := fileNameAll[0 : len(fileNameAll)-len(fileSuffix)]
outPath := filePrefix + dstFileSuffix
s3Client := utils.NewClientByParam(bucket, ak, sk, region, endpoint)
err := s3Client.DownloadFile(nil, fileName, inPath)
if err != nil {
return nil, cloudevents.NewHTTPResult(http.StatusNotFound, "s3 download failed")
}
...Image compression using the imaging library:
... // Open a image.
src, err := imaging.Open(inPath)
if err != nil {
return err
}
// Resize srcImage to width = 800px preserving the aspect ratio.
dstImage800 := imaging.Resize(src, 800, 0, imaging.Lanczos)
// Save the resulting image as JPEG.
err = imaging.Save(dstImage800, outPath)
if err != nil {
return err
}
...Adding a watermark:
... // original image bounds
origin_size := origin.Bounds()
// create new canvas
canvas := image.NewNRGBA(origin_size)
// draw original image
draw.Draw(canvas, origin_size, origin, image.ZP, draw.Src)
// draw watermark at offset (30,30)
draw.Draw(canvas, watermark.Bounds().Add(image.Pt(30, 30)), watermark, image.ZP, draw.Over)
// save new image
create_image, _ := os.Create(outPath)
jpeg.Encode(create_image, canvas, &jpeg.Options{95})
defer create_image.Close()
...Full source code can be downloaded from the provided link.
Deploy Function
Navigate to the Function Compute console via the 360 Zhihui Cloud product page: Product → Serverless Development → Function Compute (FC). Create a function using a custom runtime, upload a local ZIP package, select “No cloud build”, and choose the Go 1.20 runtime.
After deployment, obtain the function’s API endpoint to configure the event bus.
Step 2: Configure Event Rules
When an image is uploaded to OBS, an event is sent to the event bus. Create a rule on the default bus that matches object‑creation events from OBS where the bucket name is event-bucket .
Set the rule’s target to an API endpoint (POST) that invokes the previously deployed function. Associate the function target with the rule.
Step 3: Enable Notification
Turn on the “Event Notification” switch for the OBS bucket so that all events are forwarded to the event bus.
Step 4: Upload Image for Testing
Upload an image to the event-bucket via the OBS console (or SDK/API). After upload, the function processes the image, and the processed file appears in the bucket with an _output suffix, reduced from ~3.3 MB to ~226 KB and containing the watermark.
Logs in Function Compute confirm successful execution.
Conclusion
Serverless architecture effectively addresses many challenges of traditional image‑processing pipelines, offering cost efficiency, scalability, and reduced operational burden. By combining an event bus with Function Compute, developers can quickly implement asynchronous image compression and watermarking, accelerating time‑to‑market and improving system responsiveness.
360 Smart Cloud
Official service account of 360 Smart Cloud, dedicated to building a high-quality, secure, highly available, convenient, and stable one‑stop cloud service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.