提问者:小点点

使用S3 getObject数据作为视频源


当我使用带有AWS SDK的节点请求视频时,将返回以下内容:

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

共1个答案

匿名用户

想出来了。 愚蠢的错误。 base64方式确实有效,只是忘了向video元素添加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