我想将失败的任务列表上传到 AWSS3TransferUtility。下面的代码,我曾经将图像上传到 S3。
func uploadImage(image:UIImage,keyPath:String,metaData:PSMetaDataModel,completionBlock: @escaping AWSS3TransferUtilityUploadCompletionHandlerBlock){
let expression = AWSS3TransferUtilityUploadExpression()
let progress:AWSS3TransferUtilityProgressBlock = {task,progress in
DispatchQueue.main.async(execute: { })
}
expression.progressBlock = progress
expression.setValue(metaData.eventId, forRequestParameter: Key.S3EventId)
expression.setValue(metaData.s3path, forRequestParameter: Key.S3Path)
expression.setValue("\(0)", forRequestParameter: Key.S3TotalCount)
expression.setValue(metaData.imageType, forRequestParameter: Key.S3ImageType)
expression.setValue("\(metaData.position)", forRequestParameter: Key.S3Position)
expression.setValue(metaData.eventType, forRequestParameter: Key.S3EventType)
let completionHandler:AWSS3TransferUtilityUploadCompletionHandlerBlock?
completionHandler = { (task, error) -> Void in
completionBlock(task, error)
if error != nil {
print(metaData.eventId)
print(keyPath)
}
/* DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now()) {
if let dict = task.response?.url?.queryDictionary, let eventId = dict["x-amz-meta-event"] {
PSDataManager.getImageMetaData(eventId: eventId) { (imageData) in
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now()) {
PSDataManager.deleteImageMetaData(imageData: imageData)
}
}
}
}*/
}
print("key:\(keyPath)")
//Needs to change compressionQuality. I have set it for testing
if let imageData = image.jpegData(compressionQuality: 1.0) {
appDelegate.transferUtility.uploadData(imageData,bucket: AMAZON_S3_BUCKET_NAME,key: keyPath,contentType: "image/jpeg",expression: expression,completionHandler: completionHandler).continueWith { (task) -> Any? in
if let error = task.error {
if let uploadTask = task.result {
print(uploadTask)
uploadTask.setProgressBlock(progress)
uploadTask.setCompletionHandler(completionHandler!)
}
completionBlock(AWSS3TransferUtilityUploadTask(),error)
if error.localizedDescription == "The operation couldn’t be completed. (com.amazonaws.AWSCognitoIdentityErrorDomain error 10.)" {
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .APSoutheast2, identityPoolId: "us-east-1:65f1deb7-1003-4264-a1ce-bcd0c43540e9")
credentialsProvider.clearKeychain()
credentialsProvider.getIdentityId()
}
}
if let result = task.result {
print("AWS Result uploding image: \(result)")
}
if task.isCancelled {
print("task has been cancelled")
}
return nil
}
}
我已经检查了这个链接AWSS3TransferUtility: How to retry failed upload task after app restart
在此链接中,他提到我们需要设置重试计数,但这用于在应用程序处于前台或后台时再次重试任务。
我主要关心的是,假设在上传任务时互联网连接断开并假设用户相当应用程序。在这种情况下,当用户进入应用程序时,我想要失败的任务并再次重新上传。