Question:
I am using the javascript version of the aws sdk to upload a file to an amazon s3 bucket.
code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
AWS.config.update({ accessKeyId : 'access-key', secretAccessKey : 'secret-key' }); AWS.config.region = 'region'; var bucket = new AWS.S3({params: {Bucket: 'bucket-name'}}); //var fileChooser = document.getElementById('file'); var files = event.target.files; $.each(files, function(i, file){ //console.log(file.name); if (file) { var params = {Key: file.name, ContentType: file.type, Body: file}; bucket.upload(params).on('httpUploadProgress', function(evt) { console.log("Uploaded :: " + parseInt((evt.loaded * 100) / evt.total)+'%'); if("Uploaded :: " + parseInt((evt.loaded * 100) / evt.total)+'%' == 'Uploaded :: 20%'){ console.log("abort upload"); bucket.abort.bind(bucket); } }).send(function(err, data) { if(err != "null"){ console.log(data); //alert("Upload Success \nETag:"+ data.ETag + "\nLocation:"+ data.Location); var filename = data.Location.substr(data.Location.lastIndexOf("/")+1, data.Location.length-1); console.log(filename); fileData = filename; filename = filename.replace("%20"," "); $('.aws-file-content').append(''+filename+' '); }else{ console.log(err); } }); } }); |
While the file is uploading parts of the file successfully and is still in progress, I want to abort/stop the file upload.
I tried:
1 2 3 |
bucket.abort();// not working bucket.abort.bind(bucket); //not working. |
Thanks for help.
Answer:
Found the solution :
1 2 3 4 |
// replaced bucket.upload() with bucket.putObject() var params = {Key: file.name, ContentType: file.type, Body: file}; request = bucket.putObject(params); |
then for abort the request:
1 2 3 4 |
abort: function(){ request.abort(); } |