ar099968
ar099968

Reputation: 7537

ASW-SDK S3 Upload Abort

I want abort an upload, but calling upload.abort() raise an error Upload aborted.. How abort the upload silently (without error)?

import { PutObjectCommandInput, S3Client } from '@aws-sdk/client-s3';
import { Progress, Upload } from "@aws-sdk/lib-storage";

const uploadParams: PutObjectCommandInput = {
  Bucket: 'my-bucket',
  Key: 'my-file',
  Body: file,
};

const upload: Upload = new Upload({
  client: s3Client,
  params: uploadParams,
});

// abort after 3 seconds
setTimeout(() => upload.abort(), 3000);

// start upload
upload.done();

I have also tried to catch the promise

upload.abort().then().catch(() => {
  // ...
})

And also tried to try catch all

try {
  upload.abort().then().catch(() => {
    // ...
  })
} catch () {
  // ...
}

Upvotes: 1

Views: 942

Answers (1)

Addison Shaw
Addison Shaw

Reputation: 26

That error is actually being thrown from the upload.done Promise.

You can check the code here and here

For your case, you would want something like the following:

const upload: Upload = new Upload({
  client: s3Client,
  params: uploadParams,
});

// abort after 3 seconds
setTimeout(() => upload.abort(), 3000);

try {
  // start upload
  upload.done();
} catch (err) {
    if (err.name === 'AbortError') {
      // Handle gracefully
    }
}

Upvotes: 1

Related Questions