How to upload images to amazon s3 bucket on nextjs13 using app router?
I tried using formidable and multer but both soltions are not working as nextjs changes their api routes structure:
import { NextRequest, NextResponse } from "next/server";
import { v4 as uuid } from "uuid";
import multer from "multer";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { IncomingForm } from "formidable";
const s3Client = new S3Client({
region: process.env.REGION as string,
credentials: {
accessKeyId: process.env.ACCESS_KEY as string,
secretAccessKey: process.env.SECRET_KEY as string,
},
});
async function uploadImageToS3(
file: Buffer,
fileName: string
): Promise<string> {
const params = {
Bucket: process.env.BUCKET_NAME as string,
Key: `${Date.now()}-${fileName}`,
Body: file,
ContentType: "image/jpeg", // Change the content type accordingly
};
const command = new PutObjectCommand(params);
await s3Client.send(command);
return fileName;
}
export async function POST(request: NextRequest, response: NextResponse) {
try {
const formData = await request.formData();
// How to pass the buffer and store the image on s3 bucket ???
} catch (error) {
console.error("Error uploading image:", error);
NextResponse.json({ message: "Error uploading image" });
}
}