2026-03-25 22:20:37 +08:00

25 lines
711 B
TypeScript

import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
const s3 = new S3Client({
region: "auto",
endpoint: `https://${process.env.R2_ACCOUNT_ID}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: process.env.R2_ACCESS_KEY!,
secretAccessKey: process.env.R2_SECRET_KEY!,
},
});
export async function getPresignedUploadUrl(key: string) {
const command = new PutObjectCommand({
Bucket: process.env.R2_BUCKET!,
Key: key,
ContentType: "image/webp",
});
return getSignedUrl(s3, command, { expiresIn: 3600 });
}
export function getPublicUrl(key: string) {
return `${process.env.R2_PUBLIC_URL}/${key}`;
}