title: How to upload images directly on cloudinary without storing it into local directory?
body: <p>I am new in ExpressJs and working on creating api for one of a dashboard created in reactjs. There is a form in a dashboard which is collecting some of information from the users like &quot;title&quot;, &quot;description&quot; and &quot;image&quot;. I have created an express server to collect that information and to save it into mongodb. For images What I have done is that, I am uploading image to <a href="https://cloudinary.com/" rel="nofollow noreferrer">Cloudinary</a> and storing uploaded url and public_id into database.</p>
  <p>So after following some of tutorials I have done something like this.</p>
  <p><strong>index.js</strong></p>
  <pre class="lang-js prettyprint-override"><code>const express = require(&quot;express&quot;);
  const bodyParser = require(&quot;body-parser&quot;);
  const cors = require(&quot;cors&quot;);
  
  const db = require(&quot;./db&quot;);
  
  // Api router import goes here
  const sectionTypesRouter = require(&quot;./routes/section-types-router&quot;);
  
  const app = express();
  const apiPort = 3000;
  
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(cors());
  app.use(bodyParser.json());
  
  db.on(&quot;error&quot;, console.error.bind(console, &quot;MongoDB connection error:&quot;));
  
  app.get(&quot;/&quot;, (req, res) =&gt; {
    res.send(&quot;Hello World!&quot;);
  });
  
  app.use(&quot;/api&quot;, sectionTypesRouter);
  
  app.listen(apiPort, () =&gt; console.log(`Server running on port ${apiPort}`));
  </code></pre>
  <p>Than, First I have create a file <strong>multer.js</strong> :</p>
  <pre class="lang-js prettyprint-override"><code>const multer = require(&quot;multer&quot;);
  
  const storage = multer.diskStorage({
    destination: &quot;public/uploads&quot;,
    filename: (req, file, cb) =&gt; {
      cb(null, file.fieldname + &quot;-&quot; + Date.now());
    },
  });
  
  const fileFilter = (req, file, cb) =&gt; {
    if (file.mimetype === &quot;image/jpeg&quot; || file.mimetype === &quot;image/png&quot;) {
      cb(null, true);
    } else {
      //reject file
      cb({ message: &quot;Unsupported file format&quot; }, false);
    }
  };
  
  const upload = multer({
    storage: storage,
    fileFilter: fileFilter,
  });
  
  module.exports = upload;
  </code></pre>
  <p>Below is my api router <strong>section-type-router.js</strong> :</p>
  <pre class="lang-js prettyprint-override"><code>const express = require(&quot;express&quot;);
  const upload = require(&quot;../utils/multer&quot;);
  
  const SectionTypesCtrl = require(&quot;../controllers/section-types-ctrl&quot;);
  
  const router = express.Router();
  
  router.post(
    &quot;/section-type&quot;,
    upload.single(&quot;image&quot;),
    SectionTypesCtrl.createSectionType
  );
  router.get(&quot;/section-types&quot;, SectionTypesCtrl.getSectionTypes);
  
  module.exports = router;
  </code></pre>
  <p>This is the <strong>section-type-ctrl.js</strong> :</p>
  <pre class="lang-js prettyprint-override"><code>const SectionType = require(&quot;../models/section-type-model&quot;);
  const fs = require(&quot;fs&quot;);
  const path = require(&quot;path&quot;);
  const cloudinaryUploader = require(&quot;../utils/cloudinaryUploader&quot;);
  
  const createSectionType = async (req, res) =&gt; {
    const body = req.body;
  
    if (!body) {
      return res.status(400).json({
        success: false,
        error: &quot;Required parameter are missing&quot;,
      });
    }
    cloudinaryUploader
      .uploads(req.file.path, &quot;Section-Types&quot;)
      .then((result) =&gt; {
        const sectionType = new SectionType({
          title: body.title,
          description: body.description,
          image: {
            url: result.url,
            publicId: result.public_id,
          },
        });
        sectionType
          .save()
          .then(() =&gt; {
            return res.status(201).json({
              success: true,
              id: sectionType._id,
              message: &quot;Section type created!&quot;,
            });
          })
          .catch((error) =&gt; {
            return res.status(400).json({
              error,
              message: &quot;Section type not created!&quot;,
            });
          });
      })
      .catch((error) =&gt; {
        res.status(500).send({
          message: &quot;failure&quot;,
          error,
        });
      });
  };
  
  module.exports = {
    createSectionType,
  };
  </code></pre>
  <p>And lastly this is <strong>cloudinaryUpload.js</strong> :</p>
  <pre class="lang-js prettyprint-override"><code>const cloudinary = require(&quot;../config/cloudinary&quot;);
  
  exports.uploads = (file, folder) =&gt; {
    return new Promise((resolve) =&gt; {
      cloudinary.uploader.upload(
        file,
        {
          resource_type: &quot;auto&quot;,
          folder: folder,
        },
        (err, result) =&gt; {
          if (!err) {
            resolve({
              url: result.url,
              public_id: result.public_id,
            });
          } else {
            throw err;
          }
        }
      );
    }).catch((error) =&gt; {
      throw error;
    });
  };
  </code></pre>
  <p>Now, everything is working properly. Images is uploading to the cloudinary and returned url and public_id is storing in database. But the problem is that <strong>image that I have uploaded is also upload on local directory public/uploads/</strong>. This will may create a storage issue while host a site. So Is there any best way to upload image directly to the cloudinary without creating a copy in local directory which also should work on production mode ?</p>