3

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?

1
  • 1
    hey did you get it fixed? Commented Oct 10, 2020 at 18:58

1 Answer 1

2

I had the same problem and after quite some research i found this solution what will also work for you, i guess.

router.post('/upload', multerUploads, async (req, res) => {
try {
    //console.log(req.file)
    // if (!req.files || Object.keys(req.files).length === 0) {
    //     return res.status(400).send('No files were uploaded.');
    // };

    // const file = req.files.file;
    // if (file.size > 1024*1024) {
    //     return res.status(400).json({ msg: "Size is too large" });
    // }

    // if (file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/png') {
    //     return res.status(400).json({ msg: "File format is incorrect." });
    // }

    //console.log(req.file)
    
    if(req.file) {
        let cld_upload_stream = cloudinary.uploader.upload_stream({folder: "test"}, function (error, result) {     
            console.log(error, result);  
            res.json({ public_id: result.public_id, url: result.secure_url });         
        });

        streamifier.createReadStream(req.file.buffer).pipe(cld_upload_stream);
    }

} catch (err) {
    res.status(500).json({ msg: err.message })
}
});

For reference i found this helpful post from the cloudinary team cloudinary support topic node upload. Cheers

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.