简体   繁体   English

使用Javascript AWS开发工具包将使用Cordova Media Capture插件捕获的视频上传到S3

[英]Uploading to S3 a video captured using Cordova Media Capture plugin using Javascript AWS SDK

I am facing a peculiar issue while trying to upload a video file (.mp4 and .mov) to S3, captured using cordova-plugin-media-capture@1.4.3 or picked from gallery using cordova-plugin-camera@2.4.1. 尝试将视频文件(.mp4和.mov)上传到S3,使用cordova-plugin-media-capture@1.4.3捕获或使用cordova-plugin-camera@2.4.1从图库中拾取时,我遇到一个特殊的问题。 。 I am using javascript AWS SDK v2.3.11 and calling the .upload function of the SDK. 我正在使用JavaScript AWS SDK v2.3.11并调用SDK的.upload函数。

It only copies 15 Bytes of data onto the S3 regardless of the actual size of the video file and it is non-playable. 无论视频文件的实际大小如何,它仅将15字节的数据复制到S3上,并且无法播放。

Implementation - 实施-

Capture video: 捕捉视频:

navigator.device.capture.captureVideo(
  captureSuccess,
  captureError, 
  {
    limit: 1,
    duration: 30,
    destinationType: 2,
    sourceType: 1,
    mediaType: 1
  }
);
var captureSuccess = function (mediaFiles) {
  var mediaFile = mediaFiles[0];
  var filedata = {
    Key: "videos/" + fileName,
    ContentType: mediaFile.type,
    Body: mediaFile
  };
  var aws = AWS;
  var creds = new aws.Credentials(
                      AccessKeyId,
                      SecretAccessKey,
                      SessionToken
                  );
  aws.config.credentials = creds;
  s3 = new aws.S3();

  s3.upload(
    filedata, 
    {
      Bucket: bucketName
    }, 
    function(err, location){
      if(!err){
        //uploaded successfully
      } else {
        //upload failed
      }
    }
 );
}

When I try to convert the media file to its Base64 data and upload, it does write the complete base64 file to the S3 bucket. 当我尝试将媒体文件转换为其Base64数据并上传时,它确实将完整的base64文件写入S3存储桶。 However, I then need to strip the prefixed filetype and base64 identifiers text then decompile the data to a binary format save it again to S3 (from EB nodeJS service). 但是,然后我需要剥离前缀的文件类型和base64标识符文本,然后将数据反编译为二进制格式,然后再次将其保存到S3(来自EB nodeJS服务)。 Another issue with this approach is that converting a video file to a base64 data and saving in RAM memory of the phone is prone to application crashes due to memory management on both IOS and Android. 这种方法的另一个问题是,由于IOS和Android上的内存管理,将视频文件转换为base64数据并保存在手机的RAM内存中很容易导致应用程序崩溃。 I am unable to use this mechanism to convert a video file of more than 5 secs in Android, and more than 10 secs in 16GB iPhone6. 我无法使用这种机制在Android中转换超过5秒的视频文件,而在16GB iPhone6中转换超过10秒的视频文件。 The application crashes beyond both these scenarios. 在这两种情况下,应用程序都将崩溃。

Changed Implementation with Base64: 更改的Base64实现:

var captureSuccess = function (mediaFiles) {
  var mediaFile = mediaFiles[0];
  var filedata = {
    Key: "videos/" + fileName,
    ContentType: mediaFile.type
  };
  var aws = AWS;
  var creds = new aws.Credentials(
                      AccessKeyId,
                      SecretAccessKey,
                      SessionToken
                  );
  aws.config.credentials = creds;
  s3 = new aws.S3();
  getBase64data(
    mediaFile.fullPath, //tried with mediaFile.localURL as well
    function(data){
      filedata.Body = data;
      s3.upload(
        filedata, 
        {
          Bucket: bucketName
        }, 
        function(err, location){
          if(!err){
            //uploaded successfully
          } else {
            //upload failed
          }
        }
      );  //ending s3.upload
    );  //ending getBase64data
}
function getBase64Data(filePath, cb){
  window.resolveLocalFileSystemURL(
    filePath,
    function(entry){
      entry.file(
        function(file) {
          var reader = new FileReader();
          reader.onloadend = function(event) {
            cb(event.target.result);
          };
          reader.readAsDataURL(file);
        }, 
        function(e){
         //error retrieving file object
        }
      ); //ending entry.file
    },
    function(e){
      //error getting entry object
    }
  ); //ending resolveLocalFileSystemURL
}

The AWS S3 JavaScript SDK allows a couple of different ways to provide the file data to the upload function. AWS S3 JavaScript SDK允许采用两种不同的方式将文件数据提供给upload功能。 According to the documentation the file data can be any of the following: 根据文档,文件数据可以是以下任意一种:

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

You need to extract the data from your captured video to any of those formats before attempting to upload. 您需要先将捕获的视频中的数据提取为任何一种格式,然后再尝试上传。

Using mediaFile.fullPath , you can read the data from the file into a Buffer or create a ReadableStream and then use that to upload the file. 使用mediaFile.fullPath ,您可以将文件中的数据读入Buffer或创建ReadableStream,然后使用该文件上载文件。 To extract the data from the file you can use cordova-plugin-file . 要从文件中提取数据,可以使用cordova-plugin-file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有NodeJ的情况下使用javascript sdk为s3视频创建AWS CloudFront签名的URL - How to create AWS CloudFront signed url for s3 video using javascript sdk without NodeJs 使用AWS SDK for JavaScript访问AWS S3 - Accessing AWS S3 using AWS SDK for JavaScript 使用媒体捕获cordova插件录制视频并上传到远程服务器问题 - Record Video using Media capture cordova plugin and upload to remote server issue 如何显示使用cordova-plugin-media-capture捕获的图像 - How to display an image captured with cordova-plugin-media-capture 使用纯 Javascript(无 SDK)对 AWS S3 进行 REST 调用 - Making REST calls to AWS S3 using Pure Javascript (No SDK) 使用AWS S3 Javascript SDK加载新网页 - Loading New Webpage Using AWS S3 Javascript SDK 使用浏览器JavaScript SDK检查AWS S3上是否存在文件? - Check if File Exists on AWS S3 Using Browser JavaScript SDK? 使用JavaScript SDK的AWS S3:未经身份验证的公共用户的listObjects - AWS S3 using JavaScript SDK: listObjects for unauthenticated, public users 使用外部Java插件在PhoneGap / Cordova中进行音频/视频捕获 - Audio/Video Capture in PhoneGap/Cordova using external Java plugin 在AWS S3 SDK中上传文件 - Uploading File in AWS S3 SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM