Sunday 28 June 2020

How to upload buffer image to cloudinary?

Hi,I want to upload an array of images to cloudinary. I've managed to parse the files with multer, that return them as buffer. After reading the documentation and github issues, it seems that converting the buffer to data uri is a performance killer. So I used the cloudinary' stream uploader, but it doesn't work.Here is my code:// multer config const storage = multer.memoryStorage(); const limits = { fileSize: 1000 * 1000 * 4 }; // limit to 4mb const upload = multer({ storage, limits }); cloudinary.config({ cloud_name: process.env.CLOUDINARY_CLOUD_NAME, api_key: process.env.CLOUDINARY_API_KEY, api_secret: process.env.CLOUDINARY_API_SECRET, }); // upload image router.post("/upload-images", upload.array("image"), ({files}, res) => { try { if (files) { const imagesUploaded = []; files.forEach(async (file, i) => { const image = await cloudinary.uploader.upload_stream(file, { resource_type: "image", public_id: `users/${req.userid}/img${i}`, crop: "scale", quality: "auto", }); return imagesUploaded.push(image.secure_url); }); console.log("imagesUpdated", imagesUploaded); // upload images url to the db } } catch (err) { return res.json(err); } }); Thanks!

Submitted June 28, 2020 at 08:25PM by MonsieurLeland

No comments:

Post a Comment