使用multer,express和multer-s3为NodeJS跟踪图像上传的进度

时间:2017-07-19 04:27:36

标签: node.js express multer multer-s3

我正在尝试使用multer-s3将图像上传到AWS S3。一切正常(即上传视频,图像和文件),但它不完整。我不知道如何跟踪上传的进度或百分比。

我的multer代码被视为像这样的中间件

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 });

然后我会将中间件附加到我的路线上:

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

然后在我的控制器上,是一个简单的发布请求,它将保存数据库的路径:

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');
        }); 
    });
}
  

AwsS3PublicURL.setAwsPublicUrlSingle是我的一条路径   AmazonS3Bucket将公开发布。

我的问题是我不知道如何正确跟踪上传的进度或进度百分比,并在前端或控制台中显示。如果有人知道答案,请提前感谢您。

0 个答案:

没有答案