5

I'm trying to upload images to AWS S3 using multer-s3. Everything works fine (i.e. uploading video, images and files) but it's incomplete. I have no idea how to track the progress or percentage of the upload.

My code for multer is treated as a middleware like this

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

var s3 = new AWS.S3();

const s3Storage = multerS3({
    s3     : s3,
    bucket : 'app-bucket',
    acl    : 'public-read',
    key    : function (req, file, callback) {
        callback(null, file.originalname);
    }
});

module.exports.s3Upload  = multer({ storage: s3Storage });

Then I will attached the middleware to my route like this:

router.route('/image/upload').get(uploadController.getUploadImageController)
                    .post(middleware.s3Upload.single('myImage'),
                    uploadController.postUploadPhotoToAlbumController );

Then on my controller, is a simple post request that will save the path to database:

module.exports.postUploadPhotoToAlbumController = (req, res) => {
    let query = Images.findById({ _id: req.params.id });

    query.exec((err, images) => {   
        if(err){
            return res.status(500).send({success: false, error: err, message: 'Something went wrong.'});
        } if(!images){
            return res.status(200).send({success: false, message: 'That image does not exist to your album.'});
        }

        images.image = !!req.file ? AwsS3PublicURL.setAwsPublicUrlSingle(req) : null; 

        images.save(err => {
            if(err){
                return res.status(500).send({success:false, error: err, message: 'Something went wrong.'});
            }
            req.flash('message', 'Your image was successfully uploaded.');
            res.redirect('/album/photos');
        }); 
    });
}

The AwsS3PublicURL.setAwsPublicUrlSingle is a a path to my AmazonS3Bucket set to public.

My problem is I don't know how to properly track the progress or percentage of progress of my upload and display on frontend or in console. Thank you in advance if anyone knows the answer.

0 Answers0