Angularjs 下载私人视频文件并从 s3 存储桶中播放

Angularjs to download a private video file and play from s3 bucket

我正在尝试从 s3 存储桶下载视频文件并在我的 angularjs 应用程序中显示它。我正在尝试使用 AWS Node.js 来执行此操作,但无法执行此操作。请帮助我

示例代码

var AWS = require('aws-sdk');
AWS.config.update(
  {
    accessKeyId: ".. your key ..",
    secretAccessKey: ".. your secret key ..",
  }
);
var s3 = new AWS.S3();
s3.getObject(
  { Bucket: "my-bucket", Key: "path/to/videofile" },
  function (error, data) {
    if (error != null) {
      alert("Failed to retrieve an object: " + error);
    } else {
      alert("Loaded " + data.ContentLength + " bytes");
      // do something with data.Body
    }
  }
);

我能够获取数据但不知道如何显示它,数据是一个对象

您要查找的数据将在您收到的响应对象的 Body 中。

根据 AWS SDK documentation,这是您可能希望在 Body 中看到的内容:

Body — (Buffer, Typed Array, Blob, String, ReadableStream) Object data.

得到我的答案,我需要 Body 元素然后将其转换为 base64 编码数据。下面的代码

    var d = s3.getObject(params,function (error, data) {
      if (error != null) {
        console.log("Failed to retrieve an object: " + error);
      } else {
        console.log(data);
        $scope.learningVideo = "data:video/mp4;base64," + encode(data.Body);
      }
   })

以及编码方法

function encode(data)
  {
    var str = data.reduce(function(a,b){ return a+String.fromCharCode(b) },'');
    return btoa(str).replace(/.{76}(?=.)/g,'$&\n');
  }