All writing
Cloud

Building a Private CDN & Storage Pipeline with Cloudflare R2 and Next.js

How to bypass predatory S3 egress fees and deliver global gigabyte media with zero-egress Cloudflare R2 and presigned direct client uploads.

Sep 8, 20265 min read980 views62 likes
Building a Private CDN & Storage Pipeline with Cloudflare R2 and Next.js

Why Cloudflare R2 is the Future of Object Storage

Traditional cloud providers charge hefty egress fees every time your users stream video or load high-res photography. Cloudflare R2 changes the game with $0 egress fees and global Anycast edge routing.

Direct Presigned S3-Compatible Uploads

Instead of routing multi-megabyte payloads through your serverless API routes, issue short-lived presigned URLs:

import { PutObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';

export async function generateUploadUrl(key, contentType) {
  const command = new PutObjectCommand({
    Bucket: process.env.CLOUDFLARE_R2_BUCKET_NAME,
    Key: key,
    ContentType: contentType,
  });

  return await getSignedUrl(s3Client, command, { expiresIn: 300 });
}

This delivers ultra-fast uploads straight from the browser to Cloudflare's nearest edge data center.

#Cloudflare#R2#Storage#Next.js
Keep the curiosity goingMore from the notebook