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.
UnhandledPromiseRejectionWarning: TypeError: size.split is not a function or its return value is not iterable
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: `app/users/${userId}/img${i}`,
crop: "scale",
quality: "auto",
})
.end(file.buffer);
return imagesUploaded.push(image.secure_url);
});
console.log("imagesUpdated", imagesUploaded);
// upload images url to the db
}
} catch (err) {
return res.json(err);
}
});
How to fix this?