使用S3 getObject数据作为视频源



当我使用带有AWS SDK的Node请求视频时,返回的是:

{
AcceptRanges: 'bytes',
LastModified: 2020-08-04T17:15:34.000Z,
ContentLength: 6480325,
ETag: '"c52e2d08feef01b4ce3ff2a4c6adb65b"',
ContentType: 'video/mp4',
Metadata: {},
Body: <Buffer 00 00 00 20 66 74 79 70 69 73 6f 6d 00 00 02 00 69 73 6f 6d 69 73 6f 32 61 76 63 31 6d 70 34 31 00 01 14 3e 6d 6f 6f 76 00 00 00 6c 6d 76 68 64 00 00 ... 6480275 more bytes>
}

缓冲区是一个ArrayBuffer。我想这就是我需要用作视频源的东西,但我真的不知道。

我试过将它转换为普通的Bufferbase64,但我对这两种都没有太多经验,它们都不起作用。我能够使用base64将视频的第一帧显示为图像,但不能显示为视频。

有办法做到这一点吗?

请求代码:

s3.getObject(
{
Bucket: "my-bucket-name",
Key: req.body.file
},
(err, data) => {
console.log(data)
}
);

想明白了。愚蠢的错误。base64方式确实有效,我只是忘记将controls属性添加到视频元素中。

以下是感兴趣的人的代码:

s3.getObject(
{
Bucket: "my-bucket-name",
Key: req.body.file
},
(err, data) => {
const base = new Buffer.from(data.Body.buffer).toString("base64");
if (err)
return res
.status(400)
.json({ msg: "Unable to fetch video", error: err });
else
return res.json({
msg: "Video fetched",
source: base
});
}
);

然后在前端格式化并设置您想要的视频源:

// Response from an Axios request
"data:video/mp4;base64, " + res.data.source

最新更新