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 { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
const R2 = {
|
// Get config at runtime (not build time)
|
||||||
accountId: process.env.R2_ACCOUNT_ID!,
|
function getR2Config() {
|
||||||
accessKeyId: process.env.R2_ACCESS_KEY_ID!,
|
return {
|
||||||
secretKey: process.env.R2_SECRET_ACCESS_KEY!,
|
accountId: process.env.R2_ACCOUNT_ID,
|
||||||
bucket: process.env.R2_BUCKET_NAME!,
|
accessKeyId: process.env.R2_ACCESS_KEY_ID,
|
||||||
// Public URL from bucket settings - enable "Public Development URL"
|
secretKey: process.env.R2_SECRET_ACCESS_KEY,
|
||||||
|
bucket: process.env.R2_BUCKET_NAME,
|
||||||
publicUrl: process.env.R2_PUBLIC_URL,
|
publicUrl: process.env.R2_PUBLIC_URL,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!R2.accountId || !R2.accessKeyId || !R2.secretKey || !R2.bucket) {
|
|
||||||
throw new Error("Missing R2 environment variables");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = new S3Client({
|
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",
|
region: "auto",
|
||||||
endpoint: `https://${R2.accountId}.r2.cloudflarestorage.com`,
|
endpoint: `https://${R2.accountId}.r2.cloudflarestorage.com`,
|
||||||
credentials: {
|
credentials: { accessKeyId: R2.accessKeyId, secretAccessKey: R2.secretKey },
|
||||||
accessKeyId: R2.accessKeyId,
|
});
|
||||||
secretAccessKey: R2.secretKey,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const baseUrl = R2.publicUrl || `https://pub-${R2.accountId}.r2.dev`;
|
const baseUrl = R2.publicUrl || `https://pub-${R2.accountId}.r2.dev`;
|
||||||
|
|
||||||
export async function GET() {
|
|
||||||
try {
|
try {
|
||||||
const command = new ListObjectsV2Command({ Bucket: R2.bucket });
|
const command = new ListObjectsV2Command({ Bucket: R2.bucket });
|
||||||
const response = await client.send(command);
|
const response = await client.send(command);
|
||||||
|
|
@ -47,6 +47,11 @@ export async function GET() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
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;
|
let body;
|
||||||
try {
|
try {
|
||||||
body = await req.json();
|
body = await req.json();
|
||||||
|
|
@ -59,6 +64,14 @@ export async function POST(req: NextRequest) {
|
||||||
return NextResponse.json({ error: "Missing filename or contentType" }, { status: 400 });
|
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 {
|
try {
|
||||||
const ext = filename.split(".").pop() || "jpg";
|
const ext = filename.split(".").pop() || "jpg";
|
||||||
const key = `memories/${childId || "default"}/${Date.now()}-${Math.random().toString(36).slice(2, 8)}.${ext}`;
|
const key = `memories/${childId || "default"}/${Date.now()}-${Math.random().toString(36).slice(2, 8)}.${ext}`;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue