无法成功将视频上传到AWS S3

时间:2020-08-10 16:31:48

标签: node.js react-native amazon-s3

在将视频从React Native发送到后端并使其在S3上正常工作的方式上,我有些迷茫。所有帮助将不胜感激,尤其是在我可能出问题的地方。

最初,我从React Native开始使用Expo Camera录制视频。停止记录后,我们使用fetch发送数据,如下所示:

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

我们通过IOS从Camera组件获取的数据类型为:

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

在后端,我正在使用一种称为multer的中间件。我的后端路由如下所示:

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

我们在req.file中看到的数据是:

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

要上传有效的视频,我可能会做错什么?无论是下载文件还是尝试使用链接并观看视频,我都无法从s3观看视频。

谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果您已经在使用multer,请使用以下命令: https://www.npmjs.com/package/multer-s3