Cordova将文件上传到S3

时间:2016-08-25 04:38:53

标签: cordova amazon-s3

我将相机中的文件复制到Cordova App(在Android上)上传到S3。目前我将该文件下载到缓存目录(我很反过来,但我还没有弄清楚如何做到这一点)

我可以创建一个小的测试文本文件并上传没有问题,所以我知道该部分正在运行。它只是获取图片或视频文件并上传,我遇到了重大问题,

function downloadFileToLocalStorage(fileName, url, callback){

 window.resolveLocalFileSystemURL(cordova.file.cacheDirectory, function(dir) {

var name = dir.nativeURL + fileName;
var fileTransfer = new FileTransfer();
var uri = encodeURI(url);

fileTransfer.download(
  uri, name, function (entry) {
    callback(entry);
  },
  function (error) {console.log(error);},
  false, {
    headers: {
      "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
    }
  }
);

});

}

然后我读了文件并上传

function uploadAsset(fileEntry,callback) {

 fileEntry.file(function(file) {

var reader = new FileReader();
reader.onloadend = function (evt) {
//  var theBody = btoa(evt.target._result) (8mb file);
 var theBody = this.result//  (6mb file);

  var bucket = new AWS.S3({params: {Bucket: 'video-processing'}});
  var opts = {queueSize: 2, partSize: 1024 * 1024 * 10};
  var params = {Key: file.name, ContentType: file.type, Body: theBody, opts};

  bucket.upload(params,opts, function (err, data) {
    if(data)callback(data);
  }).on('httpUploadProgress', function(evt) {
    console.log('Progress:',evt,formatBytes(evt.loaded),formatBytes(evt.total), parseInt(evt.loaded/evt.total * 100) + "%");
  });
};
reader.readAsDataURL(file);
})
}

这似乎有效,但有一些问题:

  • 报告的上传大小evt.total几乎是原始文件大小(4mb)的两倍,我假设是因为我将其读入blob。我应该做别的吗?我试过直接上传读取文件。
  • 它在完成之前超时,它似乎达到大约28-30%然后下降到16%,然后达到大约25%并且它在那里停留一段时间然后超时。

以下是跟踪的一个示例,通常它会在回落到11之前达到大约30-40%,再次出现然后下降。很沮丧!

awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 917504, totalSize: 6171107, lengthComputable: true, loaded: 917504…} 6.171 MB 6.171 MB 14%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 1146880, totalSize: 6171107, lengthComputable: true, loaded: 1146880…} 6.171 MB 6.171 MB 18%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 1359872, totalSize: 6171107, lengthComputable: true, loaded: 1359872…} 6.171 MB 6.171 MB 22%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 1589248, totalSize: 6171107, lengthComputable: true, loaded: 1589248…} 6.171 MB 6.171 MB 25%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 1818624, totalSize: 6171107, lengthComputable: true, loaded: 1818624…} 6.171 MB 6.171 MB 29%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 2048000, totalSize: 6171107, lengthComputable: true, loaded: 2048000…} 6.171 MB 6.171 MB 33%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 2277376, totalSize: 6171107, lengthComputable: true, loaded: 2277376…} 6.171 MB 6.171 MB 36%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 2277376, totalSize: 6171107, lengthComputable: true, loaded: 2277376…} 6.171 MB 6.171 MB 36%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 688128, totalSize: 6171107, lengthComputable: true, loaded: 688128…} 6.171 MB 6.171 MB 11%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 917504, totalSize: 6171107, lengthComputable: true, loaded: 917504…} 6.171 MB 6.171 MB 14%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 1146880, totalSize: 6171107, lengthComputable: true, loaded: 1146880…} 6.171 MB 6.171 MB 18%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 1359872, totalSize: 6171107, lengthComputable: true, loaded: 1359872…} 6.171 MB 6.171 MB 22%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 1359872, totalSize: 6171107, lengthComputable: true, loaded: 1359872…} 6.171 MB 6.171 MB 22%
awsController.js:43 Progress: XMLHttpRequestProgressEvent {isTrusted: true, position: 688128, totalSize: 6171107, lengthComputable: true, loaded: 688128…} 6.171 MB 6.171 MB 11%
awsController.js:38 Error: Timeout(…) 

另一个例子

1 个答案:

答案 0 :(得分:0)

我必须将超时设置为0,将队列大小设置为1来修复它

 var bucket = new AWS.S3({
    apiVersion: '2006-03-01',
    httpOptions: {timeout: 0}
  });

  var params = {
    Bucket: 'bucket-name',
    Key: file.name,
    ContentEncoding: 'base64',
    ContentType: file.type,
    Body: theBody
  };

  var opts = {
    queueSize: 1,
    partSize: 1024 * 1024 * 10
  };