Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S3 Multipart Upload: how can I cancel one?

I need to cancel an in-progress download that was initiated with

fileTransferUtility = new TransferUtility(/*...*/);
var uploadRequest = new TransferUtilityUploadRequest() /* config parameters... */
fileTransferUtility.BeginUpload(uploadRequest, new AsyncCallback(uploadComplete), file);

I have searched SO and documentation, but I can't find a way...

Rationale: the user can pick a file to upload, and may choose a very large file, say 1GB. I need to be able to cancel this.

In the worst case, I could just try to kill the thread entirely, or shut down the upload in an unclean way, but how???

Thanks!

like image 947
Palantir Avatar asked May 10 '13 15:05

Palantir


2 Answers

I received an official answer from Amazon on this. Here is their reply:

var fileTransferUtility = new TransferUtility(/* */);
var uploadRequest = new TransferUtilityUploadRequest();

Thread thread = new Thread(() => fileTransferUtility.Upload(uploadRequest));
Thread.Sleep(5000); // If not done in 5 seconds abort
if(thread.IsAlive)
    thread.Abort();

Instead of using BeginUpload/EndUpload calls, you need to use a Upload call wrapped in a Thread, and heep the reference to this thread.

If the user needs to cancel, call Abort() on the thread, which will cancel the upload. Of course, you need to clean partially uploaded files (they bill for them!).

As I suspected: very simple and intuitive, but not so easy to find :)

like image 190
Palantir Avatar answered Sep 21 '22 15:09

Palantir


Try something like :

s3Client.AbortMultipartUpload(new AbortMultipartUploadRequest()
  .WithBucketName(bucketName)
  .WithKey(key)
  .WithUploadId(Response.UploadId));
}

see http://docs.aws.amazon.com/sdkfornet/latest/apidocs/html/M_Amazon_S3_AmazonS3_AbortMultipartUpload.htm

like image 24
ToXinE Avatar answered Sep 23 '22 15:09

ToXinE