Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop/Cancel Uploading on AWS S3

I am using AWSLocal content upload method to upload file. I need to cancel uploading from another screen .

Here is the uploading function :

private func uploadLocalContent(localContent: AWSLocalContent) {
    localContent.uploadWithPinOnCompletion(false, progressBlock: {[weak self](content: AWSLocalContent?, progress: NSProgress?) -> Void in
        guard let strongSelf = self else { return }
        dispatch_async(dispatch_get_main_queue()) {
            // Update the upload UI if it is a new upload and the table is not yet updated
            if(strongSelf.tableView.numberOfRowsInSection(0) == 0 || strongSelf.tableView.numberOfRowsInSection(0) < strongSelf.manager.uploadingContents.count) {
                strongSelf.updateUploadUI()
            } else {

                for uploadContent in strongSelf.manager.uploadingContents {
                    if uploadContent.key == content?.key {
                        let index = strongSelf.manager.uploadingContents.indexOf(uploadContent)!
                        let indexPath = NSIndexPath(forRow: index, inSection: 0)
                        strongSelf.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
                    }
                }
            }
        }
        }, completionHandler: {[weak self](content: AWSContent?, error: NSError?) -> Void in
            guard let strongSelf = self else { return }
            strongSelf.updateUploadUI()
            if let error = error {
                print("Failed to upload an object. \(error)")
                strongSelf.showSimpleAlertWithTitle("Error", message: "Failed to upload an object.", cancelButtonTitle: "OK")
            } else {
                strongSelf.refreshContents()
            }
        })
    updateUploadUI()
}
like image 933
chirag bhalara Avatar asked Jan 11 '17 10:01

chirag bhalara


1 Answers

Unfortunately there's no way to cancel an upload when called from AWSLocalContent as there's no access to the AWTask.

Look into AWSS3TransferManager (http://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/AWSS3TransferManager.html#//api/name/upload:) that creates a AWTask for each action, it can be upload or download, that can be canceled/paused/resumed. Plus you get convenience methods from AWSS3TransferManager which can cancel/pause/resume all tasks at once.

You'll have to create the uploading AWTask, keep it somewhere accessible from your other screen and then you'll be able to cancel it.

like image 80
Marcos Griselli Avatar answered Sep 23 '22 15:09

Marcos Griselli