From 6a6a0e91dacd4e1416df4100596da377d3b8fa11 Mon Sep 17 00:00:00 2001 From: Mannu Date: Sun, 10 May 2026 15:30:15 +0530 Subject: [PATCH] Fix: check R2 config at runtime not build time --- src/app/api/upload/route.ts | 57 +++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/src/app/api/upload/route.ts b/src/app/api/upload/route.ts index 65e5b45..0746b7e 100644 --- a/src/app/api/upload/route.ts +++ b/src/app/api/upload/route.ts @@ -2,31 +2,31 @@ import { S3Client, PutObjectCommand, ListObjectsV2Command } from "@aws-sdk/clien import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; import { NextRequest, NextResponse } from "next/server"; -const R2 = { - accountId: process.env.R2_ACCOUNT_ID!, - accessKeyId: process.env.R2_ACCESS_KEY_ID!, - secretKey: process.env.R2_SECRET_ACCESS_KEY!, - bucket: process.env.R2_BUCKET_NAME!, - // Public URL from bucket settings - enable "Public Development URL" - publicUrl: process.env.R2_PUBLIC_URL, -}; - -if (!R2.accountId || !R2.accessKeyId || !R2.secretKey || !R2.bucket) { - throw new Error("Missing R2 environment variables"); +// Get config at runtime (not build time) +function getR2Config() { + return { + accountId: process.env.R2_ACCOUNT_ID, + accessKeyId: process.env.R2_ACCESS_KEY_ID, + secretKey: process.env.R2_SECRET_ACCESS_KEY, + bucket: process.env.R2_BUCKET_NAME, + publicUrl: process.env.R2_PUBLIC_URL, + }; } -const client = new S3Client({ - region: "auto", - endpoint: `https://${R2.accountId}.r2.cloudflarestorage.com`, - credentials: { - accessKeyId: R2.accessKeyId, - secretAccessKey: R2.secretKey, - }, -}); - -const baseUrl = R2.publicUrl || `https://pub-${R2.accountId}.r2.dev`; - export async function GET() { + const R2 = getR2Config(); + if (!R2.accountId || !R2.accessKeyId || !R2.secretKey || !R2.bucket) { + return NextResponse.json({ error: "R2 not configured" }, { status: 500 }); + } + + const client = new S3Client({ + region: "auto", + endpoint: `https://${R2.accountId}.r2.cloudflarestorage.com`, + credentials: { accessKeyId: R2.accessKeyId, secretAccessKey: R2.secretKey }, + }); + + const baseUrl = R2.publicUrl || `https://pub-${R2.accountId}.r2.dev`; + try { const command = new ListObjectsV2Command({ Bucket: R2.bucket }); const response = await client.send(command); @@ -47,6 +47,11 @@ export async function GET() { } export async function POST(req: NextRequest) { + const R2 = getR2Config(); + if (!R2.accountId || !R2.accessKeyId || !R2.secretKey || !R2.bucket) { + return NextResponse.json({ error: "R2 not configured" }, { status: 500 }); + } + let body; try { body = await req.json(); @@ -59,6 +64,14 @@ export async function POST(req: NextRequest) { return NextResponse.json({ error: "Missing filename or contentType" }, { status: 400 }); } + const client = new S3Client({ + region: "auto", + endpoint: `https://${R2.accountId}.r2.cloudflarestorage.com`, + credentials: { accessKeyId: R2.accessKeyId, secretAccessKey: R2.secretKey }, + }); + + const baseUrl = R2.publicUrl || `https://pub-${R2.accountId}.r2.dev`; + try { const ext = filename.split(".").pop() || "jpg"; const key = `memories/${childId || "default"}/${Date.now()}-${Math.random().toString(36).slice(2, 8)}.${ext}`;