Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions lib/r2/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,40 @@ import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { randomUUID } from "crypto";
import { getR2PublicBaseUrl } from "@/lib/utils/urls";

const R2_ENDPOINT = `https://${process.env.R2_ACCOUNT_ID}.r2.cloudflarestorage.com`;
declare global {
var _r2Client: S3Client | undefined;
}

function getR2Client(): S3Client {
if (globalThis._r2Client) return globalThis._r2Client;

const accountId = process.env.R2_ACCOUNT_ID;
const accessKeyId = process.env.R2_ACCESS_KEY_ID;
const secretAccessKey = process.env.R2_SECRET_ACCESS_KEY;

function getR2Client() {
return new S3Client({
if (!accountId || !accessKeyId || !secretAccessKey) {
throw new Error(
"Missing R2 credentials in environment variables (R2_ACCOUNT_ID, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY).",
);
}
if (!process.env.R2_BUCKET_NAME) {
throw new Error("Missing R2_BUCKET_NAME in environment variables.");
}

const client = new S3Client({
region: "auto",
endpoint: R2_ENDPOINT,
endpoint: `https://${accountId}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: process.env.R2_ACCESS_KEY_ID!,
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY!,
accessKeyId,
secretAccessKey,
},
});

if (process.env.NODE_ENV !== "production") {
globalThis._r2Client = client;
}

return client;
}

interface PresignUploadResult {
Expand Down
Loading