Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel AWSS3 TransferUtility Upload task on button tap

My requirement is to cancel upload when ever user wishes. I'm following TransferUtility document to setup and upload video, show progress, retains state even if app is in background. I couldn't find any way to cancel the uploadTask.

{
let uploadRequest = AWSS3TransferManagerUploadRequest()
let  transferUtility = AWSS3TransferUtility.default()
var completionHandler : AWSS3TransferUtilityUploadCompletionHandlerBlock?
var uploadTask : AWSTask<AWSS3TransferUtilityUploadTask>?
let expression = AWSS3TransferUtilityUploadExpression()

}


func startUpload() {

uploadTask = transferUtility.uploadFile((uploadRequest?.body)!,
                                   bucket: (uploadRequest?.bucket)!,
                                   key: (uploadRequest?.key)!,
                                   contentType: "video/mov",
                                   expression: expression,
                                   completionHander: completionHandler).continue({ (task) -> AnyObject! in if let error = task.error {
                                    print("Error: \(error.localizedDescription)")
                                    }
                                    if let _ = task.result {

                                    }
                                    return nil;
                                   }) as? AWSTask<AWSS3TransferUtilityUploadTask>


}

I want to know which continue block i should use on the task and how to perform something like uploadTask.cancel() just the way we use for TransferManager task.

like image 719
Hemanth Avatar asked Jan 29 '23 13:01

Hemanth


1 Answers

From the AWS documentation you should use AWSS3TransferManagerUploadRequest as point to cancel upload request.

So simply:

uploadRequest.cancel()

If you would like to cancel AWSS3TransferUtility task. You should work with them directly.

Use method enumerateToAssignBlocks to fetch all active AWSS3TransferUtility tasks. Or directly by using method getUploadTasks().

AWSS3TransferUtility.default().getUploadTasks()

After you get all active upload tasks simply use taskIdentifier of each task to find what task it is. And yes you should store taskIdentifier of task that you would like to cancel before.

Example of the cancel method.

static func cancel(taskIdentifier: UInt) {
        _transferUtility.enumerateToAssignBlocks(forUploadTask: { (uploadTask:AWSS3TransferUtilityUploadTask, progress:AutoreleasingUnsafeMutablePointer<(@convention(block) (AWSS3TransferUtilityTask, Progress) -> Void)?>?, error: AutoreleasingUnsafeMutablePointer<(@convention(block) (AWSS3TransferUtilityUploadTask, Error?) -> Void)?>?) in
            if uploadTask.taskIdentifier == taskIdentifier {
                uploadTask.cancel()
            }
        }, downloadTask: nil)
 }
like image 131
Oleg Gordiichuk Avatar answered Feb 04 '23 04:02

Oleg Gordiichuk