'multer file upload - how to get a value from multer in route?

I'm uploading a file using multer with Express.

I'd like access a value from multer's storage object inside the route.
How can I do that?

Multer configuration (right now I only know how to log the key):

const aws = require("aws-sdk");
const multer = require("multer");
const multerS3 = require("multer-s3");

function configureUpload () {
    
  const s3 = new aws.S3({...my credentials...});

  const upload = multer({
    storage: multerS3({
      s3: s3,
      bucket: process.env.S3_BUCKET_NAME,
      metadata: (req, file, cb) => cb(null, { fieldName: file.fieldname }),
      key: (req, file, cb) => {
        const key = `${new Date().toISOString()}-${file.originalname}`;
        return cb(console.log("KEY: ", key), key); // The string I need to access in route
      },
    }),
  });

  return upload;
}

The route:

const express = require("express");
const Person = require("../../../db/models/person");
const configureUpload = require("../../../configureUpload ");

const router = express.Router();

// Saving to MongoDB with mongoose

router.post("/", configureUpload ().any(), async (req, res) => {
  Person.create({
    ...req.body,
    files: [] // I want to add the string in multer.storage.key to this array
  })
  .then((person) => {
    ...
   })
   .catch((err) => {
    ...
   });

});

module.exports = router;


Solution 1:[1]

you can simply add req.key = keyValue then you can access in next route using req.key name

or you can also access req.file or req.files object in route

In express everything is a middleware so you can easily pass and access in next middleware

Solution 2:[2]

This an exapmle of what Tarique Akhtar Ansari already said. Adding your key to the req object so that you can access the key's value in your controller/route like so:

    const aws = require("aws-sdk");
     const multer = require("multer");
     const multerS3 = require("multer-s3");
    
    function configureUpload () {
        
      const s3 = new aws.S3({...my credentials...});
    
      const upload = multer({
        storage: multerS3({
          s3: s3,
          bucket: process.env.S3_BUCKET_NAME,
          metadata: (req, file, cb) => {cb(null, { fieldName: file.fieldname })},
          key: (req, file, cb) => {
            
            const key = `${new Date().toISOString()}-${file.originalname}`;
            req.key = key; // added the key to req object

            // return cb(console.log("KEY: ", key), key); // The string I need to access in route
          },
        }),
      });
    
      return upload;
    }

Accessing the value of the key inside your controller or route

    const express = require("express");
    const Person = require("../../../db/models/person");
    const configureUpload = require("../../../configureUpload ");
    
    const router = express.Router();
    
    // Saving to MongoDB with mongoose
    
    router.post("/", configureUpload ().any(), async (req, res) => {

     console.log('here is the value your key', req.key); // it's that simple.

      Person.create({
        ...req.body,
        files: [] // I want to add the string in multer.storage.key to this array
      })
      .then((person) => {
        ...
       })
       .catch((err) => {
        ...
       });
    
    });
    
    module.exports = router;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tarique Akhtar Ansari
Solution 2