shiichan

Manage your images end to end from a Worker with the Images binding!

Hey there, it's Shiichan! Today I found a lovely little update that lets you handle images end to end straight from Workers.

Cloudflare Changelog developers.cloudflare.com

What was announced?

In Cloudflare's Changelog, the Images binding picked up the ability to manage the images themselves. Just by calling env.IMAGES.hosted from your Worker code, you can upload, list, retrieve, update, and delete images stored in Cloudflare Images.

The story so far

Until now, touching your stored images from a Worker meant preparing an API token and hand-building the HTTP requests yourself. Keeping and managing images was a bit of a chore.

What changes

From now on, no API token management and no hand-built HTTP requests. You can call env.IMAGES.hosted directly, so everything from receiving an image to serving it fits neatly inside your Worker. Save an image you got from a form, or return a stored one as-is — the flow you wanted to write stays short.

Dive Deep

The env.IMAGES.hosted namespace gives you these six operations:

  • .upload(image, options) — Upload a new image to your account
  • .list(options) — List images with pagination
  • .image(imageId).details() — Get image metadata
  • .image(imageId).bytes() — Stream the original image bytes
  • .image(imageId).update(options) — Update metadata or access controls
  • .image(imageId).delete() — Delete an image

For example, to upload an image from a request body and return its metadata:

const image = await env.IMAGES.hosted.upload(request.body, {
  filename: "upload.jpg",
  metadata: { source: "worker" },
});

return Response.json(image);

And retrieving and serving the original bytes of a stored image is just this:

const bytes = await env.IMAGES.hosted.image("IMAGE_ID").bytes();
return new Response(bytes);

For more details, everything is written up in the Images binding docs.

Wrap-up

  • You can now upload, list, retrieve, update, and delete images from a Worker via env.IMAGES.hosted
  • No more API tokens or hand-built HTTP requests, so your code stays tidy
  • Six operations are ready to go: upload / list / details / bytes / update / delete

If you want to combine Cloudflare Images with Workers to handle images, this is a happy little update for you!