Vishal Raj
Vishal Raj

Reputation: 1775

How to abort/stop an Amazon AWS s3 upload in progress

I am using the javascript version of the aws sdk to upload a file to an amazon s3 bucket.

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

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:

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

Thanks for help.

Upvotes: 6

Views: 12301

Answers (4)

AJB
AJB

Reputation: 7590

Through dumb luck I've stumbled upon a way to do this for multipart uploads.

The accepted answer forces you to use the putObject method, which does not chunk uploads and sends them using the multipart upload API.

The following solution uses the s3.upload method of the AWS S3 SDK for Javascript in the Browser. And it seems to work just fine, even though the example from the official documentation doesn't work.

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

That's it. I just tried calling bucket.abort() and it just worked. Not sure why AWS hasn't documented this.

Upvotes: 2

Bhavesh Radadiya
Bhavesh Radadiya

Reputation: 21

Calling abort() in the browser environment will not abort any requests that are already in flight. If a multipart upload was created, any parts not yet uploaded will not be sent, and the multipart upload will be cleaned up.

Default value for part size is (5 * 1024 * 1024)

Upvotes: 2

Vishal Raj
Vishal Raj

Reputation: 1775

Found the solution :

// 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:

abort: function(){
            request.abort();
        }

Upvotes: 5

Frederic Henri
Frederic Henri

Reputation: 53813

You cannot bind from the bucket which is your S3 object, it must be called for the upload part.

change for something like this

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

so you can bind on upload like

upload.abort.bind(upload);

you can call within an timeout method as crowned in the example

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

Upvotes: 4

Related Questions