Fix R2 URL path
This commit is contained in:
parent
3334277ec9
commit
191f176a26
1 changed files with 4 additions and 3 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { S3Client, PutObjectCommand, ListBucketsCommand, ListObjectsV2Command } from "@aws-sdk/client-s3";
|
||||
import { S3Client, PutObjectCommand, ListObjectsV2Command } from "@aws-sdk/client-s3";
|
||||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
|
|
@ -12,6 +12,7 @@ const r2 = new S3Client({
|
|||
});
|
||||
|
||||
const BUCKET = process.env.R2_BUCKET_NAME!;
|
||||
const BASE_URL = `https://${process.env.R2_ACCOUNT_ID}.r2.cloudflarestorage.com`;
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
|
|
@ -35,7 +36,7 @@ export async function POST(req: NextRequest) {
|
|||
return NextResponse.json({
|
||||
uploadUrl: url,
|
||||
key,
|
||||
publicUrl: `${process.env.R2_PUBLIC_URL}/${key}`,
|
||||
publicUrl: `${BASE_URL}/${key}`,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("R2 upload error:", error);
|
||||
|
|
@ -57,7 +58,7 @@ export async function GET(req: NextRequest) {
|
|||
const res = await r2.send(command);
|
||||
const objects = (res.Contents || []).map((obj) => ({
|
||||
key: obj.Key,
|
||||
url: `${process.env.R2_PUBLIC_URL}/${obj.Key}`,
|
||||
url: `${BASE_URL}/${obj.Key}`,
|
||||
size: obj.Size,
|
||||
lastModified: obj.LastModified?.toISOString(),
|
||||
})).sort((a, b) => new Date(b.lastModified!).getTime() - new Date(a.lastModified!).getTime());
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue