【问题标题】:Angularjs to download a private video file and play from s3 bucketAngularjs 下载私有视频文件并从 s3 存储桶播放
【发布时间】:2017-08-31 00:32:40
【问题描述】:

我正在尝试从 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
    }
  }
);

我可以获取数据但不知道如何显示,数据是一个对象

【问题讨论】:

    标签: angularjs aws-sdk-js


    【解决方案1】:

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

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

    Body — (Buffer, Typed Array, Blob, String, ReadableStream) 对象数据。

    【讨论】:

      【解决方案2】:

      得到我的答案,我需要 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');
        }
      

      【讨论】:

        猜你喜欢
        • 2017-10-19
        • 1970-01-01
        • 1970-01-01
        • 2015-11-02
        • 1970-01-01
        • 2015-05-20
        • 2020-12-29
        • 2017-02-16
        • 2013-10-15
        相关资源
        最近更新 更多