Search code examples
reactjsmongodbexpressamazon-s3multer

How do I upload multiple files to AWS S3 at once using mongoDB, react, and express?


I am already able to upload video files to AWS S3, but I want to also have the user upload a thumbnail img along with the video they select to upload. Is there a way to upload multiple files at once using multer and mongoDB?

enter image description here

enter image description here

enter image description here

enter image description here


Solution

  • Of course you can! first, you must use upload.array('file', 2) in your api(the 2 here is the number of files to upload), and in your react code you need an input that accepts multiple files, like this: <input type='file' multiple='multiple' />

    One important step is to make the post as multipart/form-data and send the array of files to the server, maybe in your handleChange function you can do this: this.setState({[key]: e.target.files }); to send all files at once. One last thing, maybe you want to store just the url to the file in your mongodb, if so, then change the type to string, file: {type: String} and thumbnail: {type: String}. Hope i clarified the things a bit!

    some good references for you:

    Uploading multiple files with Multer

    https://www.npmjs.com/package/multer