DEV Community

Cover image for Image Upload to Cloudinary with Nodejs and Dotenv

Image Upload to Cloudinary with Nodejs and Dotenv

NJOKU SAMSON EBERE on February 07, 2020

Cloudinary helps developers across the world manage images with minimal efforts. In this tutorial, we will be looking at how to upload images from ...
Collapse
 
adybecky profile image
_beckyady

Hi... what if i want to have this part of the code in a separate file

// upload image here
cloudinary.uploader.upload(data.image)
.then((result) => {
response.status(200).send({
message: "success",
result,
});
}).catch((error) => {
response.status(500).send({
message: "failure",
error,
});
});

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Hey Becky, that is absolutely fine and well recommended especially for big projects.

I did just that in this article

I hope you find the answer there. If you have more questions, I am still available ๐Ÿ˜Š

Collapse
 
dimer191996 profile image
Dimer Bwimba
module.exports.createPost = async (req, res) => {
  let fileName;
  if (req.file) {
    console.log(req.file);
    try {
      if (
        req.file.mimetype != "image/jpg" &&
        req.file.mimetype != "image/png" &&
        req.file.mimetype != "image/jpeg"
      )
        throw new Error("invalid file");
      if (req.file.size > 500000) throw new Error("max size");
    } catch (error) {
      // const errors = uploadErrors(err);
      return res.status(401).send(error);
    }
    console.log(file);
    cloudinary.uploader
      .upload(req.file)
      .then((result) => {
        response.status(200).send({
          message: "success",
          result,
        });
      })
      .catch((error) => {
        response.status(500).send({
          message: "failure",
          error,
        });
      });
Enter fullscreen mode Exit fullscreen mode

this is my image

{
  fieldname: 'file',
  originalname: '0a628080d7386a847d18308ace6d15a6.jpg',  encoding: '7bit',
  mimetype: 'image/jpeg',
  buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 48 00 48 00 00 ff db 00 43 00 06 04 05 06 05 04 06 06 05 06 07 07 06 08 0a 10 0a 0a 09 09 0a 14 0e 0f 
0c ... 14239 more bytes>,
  size: 14289
}
Enter fullscreen mode Exit fullscreen mode

error:

(node:2016) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Object

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Hey Dimer, you are sending in an object containing the file details instead of just sending in the path to the image in you local machine.

Check out this dev.to/ebereplenty/cloudinary-and-....

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

The path is supposed to read: "c:/nam/folder/.../file_name.extension" and cloudinary will take it from there

Collapse
 
colourjim profile image
ijele_chimaobi

Please is there a way I can upload a blob, please help me

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Hey Chimaobi,

Sorry for the late reply

I think you are referring to the frontend of the application. Right?

Collapse
 
colourjim profile image
ijele_chimaobi

I was able to do it on the frontend of the application
I was just asking it were possible for me to do it on the server (nodejs)

Thread Thread
 
ebereplenty profile image
NJOKU SAMSON EBERE

That's alright.

It may be possible but I haven't given it a try before

Thread Thread
 
colourjim profile image
ijele_chimaobi

Okay...
Thanks for time and consign.

Collapse
 
akshay1027 profile image
Akshay.R.R

How to set a particular width and height for the image or reduce image size while uploading? Can you please help me out!

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

I haven't done that before. I think it is doable when delivering the image to a user on the FE. Check this out: cloudinary.com/documentation/image...

Collapse
 
deepak245 profile image
Deepak245

Hi ,

i am facing issue with when parsing data

Collapse
 
tcee42 profile image
Thomas

Thank you Samson, your article helped a lot. Waiting for this "Persisting and Retrieving images using cloudinary and Postgresql Through Nodejs".

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Hey Thomas, the article is here. I had you in mind when preparing it. Check it out here

Collapse
 
tcee42 profile image
Thomas

Thank you so much Ebere. I'll look at right away.

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

I will definitely work on that especially because you ask. Thank you for reading!

Collapse
 
ndutared profile image
Abby Nduta

Thanks a million. I was totally stuck. Very clear, step by step explanations

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

You are welcome. Comments like this makes me write more. Thanks for reading

Collapse
 
mgrachev profile image
Grachev Mikhail

In addition to using environment variables I can recommend the tool github.com/dotenv-linter/dotenv-li... - itโ€™s a lightning-fast linter forย .env files. Written in Rust.

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Thank you for that suggestion. I will check it out.

Collapse
 
zerodesu profile image
Ahmad Gani • Edited

thanks for the great tutorial! i have a question, how to manage the image on cloudinary after success uploading the file? i'm confused with usecase in frontend-side