Abort / stop amazon aws s3 upload, aws sdk javascript

I am using aws sdk javascript to upload a file in amazon s3.

code:

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('<i id="delete-aws-file'+i+'" class="delete-aws-file icon-remove-sign"  data-filename=' + fileData +'></i><a href="'+data.Location+'" target=_blank >'+filename+'</a><br>');
                        }else{
                            console.log(err);
                        }
                    });
                }
            });

the file is loading successfully, I want to interrupt / stop the file download is used:

 bucket.abort();// not working
 bucket.abort.bind(bucket); //not working.

Thanks for the help.

+4
source share
4 answers

Found a solution:

// replaced bucket.upload() with bucket.putObject()
var params = {Key: file.name, ContentType: file.type, Body: file};
request = bucket.putObject(params);

then to cancel the request:

abort: function(){
            request.abort();
        }
+4
source

You cannot communicate with bucketwhich is your S3 object; you need to call it for part of the download.

change something like this

var upload = bucket.upload(params)
upload.send(....)

so that you can bind at boot, for example

upload.abort.bind(upload);

you can call the timeout method certified in an example

// abort request in 1 second
setTimeout(upload.abort.bind(upload), 1000);
+1

abort() , . , , , , .

(5 * 1024 * 1024)

0

. putObject, , API . s3.upload AWS S3 SDK Javascript . , .

var bucket = new AWS.S3({params: {Bucket: 'bucket-name'}});
var params = {Key: file.name, ContentType: file.type, Body: file};
var bucket.upload(params).send();

setTimeout(bucket.abort, 1000);

What is it. I just tried calling bucket.abort()and it just worked. Not sure why AWS didn't fix this.

0
source

Source: https://habr.com/ru/post/1621388/


All Articles