Fix: check R2 config at runtime not build time
This commit is contained in:
parent
c58f64552d
commit
6a6a0e91da
1 changed files with 35 additions and 22 deletions
|
|
@ -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}`;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue