I want to save image in cloudinary and save the path of image in my db. I have sent the image with other data to the api using the FormData object and it worked perfectly fine. The data have reached the backend and the console log shows me that the image and other data are ready to be saved in the cloudinary and the database. That’s where i’m stuck since this morning. I can’t find a solution to save directly the image to cloudinary whithout saving it in my local repo. Since my image is in the FormData, I was thinking about saving it directly into cloudinary and saving the path of the image in the database. Why do i have to save the image to the local repo since i already have it in the FormData ? I have to use other packages like multer or formidable… and i don’t want. So, do you have a solution for me ?
import { NextResponse } from "next/server";
import dbConnect, { pool } from "@/utils/dbConnect";
import { v2 as cloudinary } from "cloudinary";
export const dynamic = "force-dynamic";
export async function POST(req) {
try {
dbConnect()
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
});
const formData = await req.formData();
if (formData.get("image") !== null) {
//console.log(formData.get("image").name)
cloudinary.uploader.upload(formData.get("image").name)
.then(result => console.log("resultat :" + result))
.catch(error => console.error("erreur" + error.message))
}
/*const newAbout = await pool.query('INSERT INTO blog (title, text, picture) VALUES ($1, $2, $3) RETURNING *',
[title, description, thumbImage])
if (newAbout.rows[0]) {
return NextResponse.json({
success: true,
message: "Data saved successfully",
data: newAbout.rows[0],
});
} else {
return NextResponse.json({
success: false,
message: "Something goes wrong !Please try again",
});
}*/
} catch (e) {
console.log(e);
return NextResponse.json({
success: false,
message: "Something goes wrong !Please try again",
});
}
}