4

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.

Hemanth
  • 123
  • 11

2 Answers2

5

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)
 }
Oleg Gordiichuk
  • 13,891
  • 5
  • 52
  • 91
  • Thank you for the response. I tried to fetch all tasks active now, but i couldn't run a loop and cancel all tasks. I want to know the line of code that cancels the AWSTask. – Hemanth Sep 21 '17 at 06:14
  • You should cancel each task separately – Oleg Gordiichuk Sep 21 '17 at 07:48
  • Since i'm saving task instance as 'uploadTask' can i cancel like uploadTask.result.cancel() ?? But that's not happening. – Hemanth Sep 21 '17 at 09:59
  • @OlegGordiichuk I have the same issue with AWSS3TransferUtilityUploadTask. I can not cancel Upload Task so can you please help me out. THANKS. – Solulab Inc. Jun 07 '19 at 12:47
  • @OlegGordiichuk Can you please let me know If I have multiple multipart files uploading simultaniously how can I get the taskIdentifier to cancel particular file upload? – Princess Aug 06 '19 at 14:42
3

I use theese two helper methods when I want to cancel one specific upload request or all upload requests:

static func cancelAllUploads() {
    AWSS3TransferUtility.default().enumerateToAssignBlocks(forUploadTask: { (uploadTask, progress, error) in
        uploadTask.cancel()
    }, downloadTask: nil)
}

static func cancelAllUploads(withTaskId taskId: UInt) {
    AWSS3TransferUtility.default().enumerateToAssignBlocks(forUploadTask: { (uploadTask, progress, error) in
        if uploadTask.taskIdentifier == taskId {
            uploadTask.cancel()
        }
    }, downloadTask: nil)
}
marcelosalloum
  • 3,172
  • 3
  • 29
  • 57