Here’s a sketch of two common patterns for “photo-gallery on a Cloudflare Pages site” with your images living on Cloudflare—pick whichever feels more natural for you:
Option A – Cloudflare Images (built-in CDN + on-the-fly transforms)
Great if you want instant resizing/format conversions (webp/avif), auto-optimizations, built-in upload UI, and a simple URL-based API.
- Enable Cloudflare Images in your Dashboard → Images → Get Started.
- Upload your photos via Dashboard or CLI:
# via wrangler
wrangler images upload ./path/to/cat.jpg \
--metadata '{"title":"My Cat"}'
- List all images via the REST API (or GraphQL). Example in a Pages Function at
functions/api/images.js:
export async function onRequestGet({ env }) {
const resp = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${env.ACCOUNT_ID}/images/v1`,
{
headers: { "Authorization": `Bearer ${env.CF_API_TOKEN}` }
}
);
const { result } = await resp.json();
// result is an array of { id, filename, variants:[…] }
return new Response(JSON.stringify(result), {
headers: { "Content-Type": "application/json" }
});
}
• Bind CF_API_TOKEN & ACCOUNT_ID via wrangler secret put.
- On your front-end (Next.js, Astro, plain HTML+Vanilla JS…), fetch
/api/images, loop over the array, and render <img src={ "https://imagedelivery.net/<ACCOUNT_HASH>/" + id + "/public" }.
- Use URL params for transforms:
https://imagedelivery.net/<ACCOUNT_HASH>/${id}/public/thumbnail/300x200/q_auto.webp
Pros:
• Zero infra to maintain, auto CDN, built-in transforms, easy to use.
Cons:
• Per-image storage & transformation fees.
Option B – Cloudflare R2 + Pages Functions (S3-style object storage + your own transforms)
Ideal if you already have a Node-based build pipeline or want full control (e.g. custom thumbnails).
- Create an R2 bucket in Dashboard → R2 → Create bucket.
- In your
pages.toml, bind it:
account_id = "<YOUR_ACCOUNT_ID>"
project_name = "photo-gallery"
[[build]]
command = "npm run build"
directory = "dist"
[[functions]]
# no extra config needed here
[bindings]
type = "r2_bucket"
name = "GALLERY_BUCKET"
bucket_name = "my-gallery-bucket"
- Upload your images:
# using AWS SDK or Minio CLI
aws --endpoint-url=https://<ACCOUNT_ID>.r2.cloudflarestorage.com \
s3 cp ./photos/ s3://my-gallery-bucket/ --recursive