Yama
Yama

Reputation: 431

Unable to successfully upload a video to AWS S3

I'm a bit lost on the way a video is being send from React Native to the backend and having it working on S3. All help would be appreciated especially where I might be going wrong.

Initially, from React Native I use the Expo Camera to record a video. Once it has stopped recording, we use fetch to send the data as follows:

const startRecording = async () => {
    setIsRecording(true);
    const video = await camera.recordAsync({
        maxDuration: 15
    });

    const data = new FormData();
    data.append('video', {
        name: 'mobile-video-upload',
        uri: video.uri
    });

    try {
        const res = await fetch('url/users/testing', {
            method: 'post',
            body: data
        });
    } catch (error) {
        console.log('error uploading');
    }
};

the type of Data we get back from Camera component through IOS is:

Object {
 "uri": "...Camera/D3B7B5F5-6A17-4C45-A0BE-897956A9E637.mov",
}

On the backend I'm using a middleware known as multer. My route for the backend looks like this

const multer = require('multer');
const upload = multer({ dest: 'uploads/' });

router.post('/testing', upload.single('video'), async (req, res) => {
let buffer = null;
fs.readFile(req.file.path, (err, data) => {
    if (err) {
        console.log('There was an error reading file: ', err);
    }
    buffer = data;
});

const params = {
    Bucket: bucket_name,
    Key: 'testing124.mov',
    ContentType: req.file.mimetype,
    Body: buffer
};

try {
    let uploadPr = await s3.putObject(params).promise();
    console.log(uploadPr);
} catch (error) {
    console.log('There was an err ', error);
}

The data we see in req.file is:

{
 fieldname: 'video',
 originalname: 'mobile-video-upload',
 encoding: '7bit',
 mimetype: 'video/quicktime',
 destination: 'uploads/',
 filename: 'aed58f2dfbcc8daa7964fb3df7d3b4f4',
 path: 'uploads/aed58f2dfbcc8daa7964fb3df7d3b4f4',
 size: 480422
}

What might I be doing wrong in order to have a valid video uploaded? I'm unable to view the video from s3 whether I download the file or try using the link and viewing the video.

Thank you for all the help.

Upvotes: 0

Views: 468

Answers (1)

Talg123
Talg123

Reputation: 1506

If your already using multer, use this: https://www.npmjs.com/package/multer-s3

Upvotes: 1

Related Questions