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 { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
|
|
@ -12,6 +12,7 @@ const r2 = new S3Client({
|
||||||
});
|
});
|
||||||
|
|
||||||
const BUCKET = process.env.R2_BUCKET_NAME!;
|
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) {
|
export async function POST(req: NextRequest) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -35,7 +36,7 @@ export async function POST(req: NextRequest) {
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
uploadUrl: url,
|
uploadUrl: url,
|
||||||
key,
|
key,
|
||||||
publicUrl: `${process.env.R2_PUBLIC_URL}/${key}`,
|
publicUrl: `${BASE_URL}/${key}`,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("R2 upload error:", error);
|
console.error("R2 upload error:", error);
|
||||||
|
|
@ -57,7 +58,7 @@ export async function GET(req: NextRequest) {
|
||||||
const res = await r2.send(command);
|
const res = await r2.send(command);
|
||||||
const objects = (res.Contents || []).map((obj) => ({
|
const objects = (res.Contents || []).map((obj) => ({
|
||||||
key: obj.Key,
|
key: obj.Key,
|
||||||
url: `${process.env.R2_PUBLIC_URL}/${obj.Key}`,
|
url: `${BASE_URL}/${obj.Key}`,
|
||||||
size: obj.Size,
|
size: obj.Size,
|
||||||
lastModified: obj.LastModified?.toISOString(),
|
lastModified: obj.LastModified?.toISOString(),
|
||||||
})).sort((a, b) => new Date(b.lastModified!).getTime() - new Date(a.lastModified!).getTime());
|
})).sort((a, b) => new Date(b.lastModified!).getTime() - new Date(a.lastModified!).getTime());
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue