opensource-developer
opensource-developer

Reputation: 3068

Aborting upload to s3 javascript API

I am using Javascript API to upload documents to Amazon S3, and I want to handle cases where the upload to s3 is aborted. May be user closes the browser tab or refresh the page (mainly closing of the browser tab)

Is there a way i can handle the case where i can send a request to my server side code when user aborts s3.upload.

I have the below code

    let s3Upload = s3.upload(params).on('httpUploadProgress', function(evt) {
      jQuery('.export-progress').html("Progress: "+ evt.loaded + "/" + evt.total);
    }).promise();

    s3Upload.then(function(s3data){
    //success processing
    }, function(err){
    // any error
    });

Any help in this would be really great. Many thanks.

Upvotes: 1

Views: 460

Answers (1)

Brad
Brad

Reputation: 163593

Is there a way i can handle the case where i can send a request to my server side code when user aborts s3.upload.

Not really. The Beacon API can be used to fire off a request if the user closes a tab, but what about cases where their battery dies, browser crashes, computer gets rebooted, etc.?

I usually handle this in one of two ways:

  • Configure a notification when objects are added, so you can associate the data.
  • On upload success (at the client), do a post back to your API to notify it that it succeeded.

Note that both methods are subject to failure, so you should have a cron job or something that regularly sweeps up unexpected items in your bucket, if appropriate for your use case.

Upvotes: 1

Related Questions