我正在复制相机上的文件,然后上传到S3上的Cordova应用程序(在安卓上)。目前,我将文件下载到缓存目录(我宁愿流,但我还没有想出如何做到这一点)
我可以创建一个小的测试文本文件,并上传没有问题,所以我知道这部分是可行的。它只是得到一个图像或视频文件,上传它,这是我有很大的问题,
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);
})
}这似乎是可行的,但也有几个问题:
这是一个跟踪的例子,通常在下降到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(…) 另一个例子
发布于 2016-08-25 23:36:49
我必须将超时设置为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
};https://stackoverflow.com/questions/39136785
复制相似问题