1

Need to upload my images into local storage and s3

My code:

    const fileStorage = multer.diskStorage({
      destination: function(req, file, cb) {
        cb(null, "./public/uploads");
      },
      filename: function(req, file, cb) {
        cb(null, file.originalname);
      }
    });
    
    /** AWS catalog */
    
    aws.config.update({
      secretAccessKey: process.env.SECRET_KEY,
      accessKeyId: process.env.ACCESS_KEY,
      region: "us-east-1"
    });
    
    const s3 = new aws.S3();
    const awsStorage = multerS3({
      s3: s3,
      bucket: process.env.BUCKET_NAME,
      key: function(req, file, cb) {
        console.log(file);
        cb(null, file.originalname);
      }
    });
    
    var upload = multer({ storage: awsStorage}).array('userPhoto',10);

    router.post('/postimages',function(req,res)
{
    upload(req,res,function(err) {
    
    });
});

In this case, I can upload to either local storage or S3. I am unable to upload to both places. Please help to solve this issue.

Sesuraj P
  • 111
  • 1
  • 1
  • 6

1 Answers1

0

I would suggest you try multer-s3 which will upload the file directly to s3 without saving to your local. if you want to save files to your Local you can do it as well using the same.

I have uploaded files to s3 upto 18Gb without any issues.

Here is the helpful link - https://www.npmjs.com/package/multer-s3

Vinayak Iyer
  • 53
  • 1
  • 1
  • 10