UPDATE: If you'd like a more robust solution, checkout cfw-easy-utils by @Cerulean. It has a simple helper to serve static assets from any cloud provider.
Have you ever wanted to share images directly from your own domain instead of a 3rd party service?
Here's a very quick and easy (and a bit naive) way to do it using Cloudflare Workers and Digital Ocean Spaces. Sorry I don't have time for a lot of detail here. Maybe later, I'll expand on this post a bit and include a video of the process.
Create a Digital Ocean Space
When you create it, I'd suggest disabling the Content Delivery Network (CDN) that Digital Ocean offers as it's just Cloudflare's CDN and your own Cloudflare account/worker will do this for you.
You'll end up with a URL like this: https://name-of-your-space-here.sfo3.digitaloceanspaces.com
Upload some images or files to you Space. Be sure to mark them as "Public" if appropriate.
Create a Cloudflare Worker
Again, this is a very simplistic worker, but ... it works.
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
if (request.method === "GET") {
const url = new URL(request.url);
const pathName = url.pathname.replace("/shared", "").replace(/^\//, "");
const search = url.search;
const response = await fetch(
`https://name-of-your-space-here.sfo3.digitaloceanspaces.com/${pathName}${search}`,
);
if (response && response.ok) {
return response;
}
return new Response("Not Found", { status: 404 });
} else {
return new Response("Method Not Allowed", { status: 405 });
}
}
Assign a Route to Your Worker
In the Cloudflare dashboard, assign a specific route to your worker. For my domain, it's like this:
*justinnoel.dev/shared/*
Now, you can share links to your images like this https://justinnoel.dev/shared/test.png
Or my site's favicon like this: