2016-08-25 149 views
0

我正在从Cordova上拷贝文件以上传到S3(在Android上)。目前我下载文件到缓存目录(我宁愿流,但我还没有能够解决如何做到这一点)Cordova将文件上传到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(…) 

又如

回答

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 
    };