Search code examples
node.jsamazon-s3bucketmulter-s3

I am not able to upload large files using multer-s3. It is not giving me any error as well.


I am not able to upload large files using multer-s3. It is not giving me any error as well. It just doesn't upload the file, doesn't even enters the callback and gets timeout. Any way to handle uploading large files to s3 Bucket?

I am using it like this:

var uploadSingle = upload.single('uploadFile');

router.post('/uploadVideo',function(req,res,next){  
    uploadSingle(req,res,function(err){
                // doesn't come here if the file is large
            if(err){
                //Error Response , Error while uploading Module PDF;
            }
            else{
                //handling file upload
               // success response
            }
    });
}

Solution

  • I had the same issue and after researching this page I found that I need to add contentLength as one of the parameters. Its value is for the length in bytes.

    const s3 = new AWS.S3({
        accessKeyId: process.env.S3_ACCESS_KEY_ID,
        secretAccessKey: process.env.S3_SECRET_ACCESS_KEY
    });
    
    var upload = multer({
      storage: multerS3({
        s3: s3,
        bucket: 'myBucket',
        contentType: multerS3.AUTO_CONTENT_TYPE,
        contentLength: 500000000,
        metadata: function (req, file, cb) {
          cb(null, {fieldName: file.fieldname});
        },
        key: function (req, file, cb) {
          cb(null, file.originalname);
        }
      })
    });
    
    router.post('/uploadToS3', upload.array('photos', 30), function(req, res, next) {
      res.send({"message": 'Successfully uploaded ' + req.files.length + ' files!'});
    })