1

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

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

在此处输入图像描述

4

2 回答 2

0

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

根据AWS 开发工具包文档,您可能希望看到以下内容Body

正文 —(缓冲区、类型化数组、Blob、字符串、ReadableStream)对象数据。

于 2017-04-14T19:46:09.987 回答
0

得到我的答案,我需要 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-04-17T11:41:46.413 回答