Image Thumbnail Worker Implementation
Let's take a look at a sample implementation. Simulate a storage-triggered function that reads a file, "processes" it, and writes an output (no external libs; pretend we resize by truncating bytes).
Takeaway: serverless is excellent for event-driven jobs like file processing.
xxxxxxxxxx32
print(handler({"bucket": "uploads", "key": "cat.jpg"}, {"request_id": "local"}))import osfrom datetime import datetimedef handler(event, context): """ event = {"bucket": "uploads", "key": "cat.jpg"} context = {"request_id": "req-1", "deadline_ms": 0} """ src = os.path.join("data", event["bucket"], event["key"]) dst = os.path.join("data", "thumbnails", event["key"] + ".thumb") os.makedirs(os.path.dirname(dst), exist_ok=True) with open(src, "rb") as f: data = f.read() # "Process": write the first 1024 bytes to simulate a small thumbnail with open(dst, "wb") as f: f.write(data[:1024]) return { "status": "ok", "src_bytes": len(data), "dst": dst, "time": datetime.utcnow().isoformat() + "Z" }if __name__ == "__main__": os.makedirs("data/uploads", exist_ok=True) # Write a fake binary fileOUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment



