简体   繁体   中英

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()
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM