Is there a way to stream back upload progress when using multer, multer-s3, and express?

263 views Asked by At

My current implementation for letting a user upload a file to s3 through my server looks like this.

import express from 'express';
import "dotenv/config";
import { S3Client } from '@aws-sdk/client-s3';
import multer from 'multer';
import multerS3 from 'multer-s3';

const router = express.Router();

const s3 = new S3Client({
    secretAccessKey: process.env.SECRET_KEY,
    accessKeyId: process.env.ACCESS_KEY,
    region: process.env.REGION
});

const upload = multer({
    storage: multerS3({
      s3: s3,
      bucket: process.env.BUCKET,
      metadata: function (req, file, cb) {
        cb(null, {fieldName: file.fieldname});
      },
      key: function (req, file, cb) {
        cb(null, `${req.user.id}/${Date.now()}-${file.originalname}`)
      }
    })
});

router.post('/upload', upload.single('file'), function(req, res, next) {
    res.send({
        message: "Uploaded!",
        urls: { name: `${req.user.id}/${Date.now()}-${req.file.originalname}`, type: req.file.mimetype, size: req.file.size }
    });
});

export default router;

I am trying to stream the progress of the upload back to the user, but I could not find any method in multer or multer-s3 that would allow me to do this. My server is written using express. Am I missing something fundamental?

0

There are 0 answers