我在AWSS3中的并行上传存在问题
我正在使用
AWSTask(forCompletionOfAllTasks:tasks).continueWithBlock({task -> AWSTask in
在S3上并行上传文件。它适用于少量文件,但是当我并行发送超过50个文件时,我有很多超时错误,我不明白为什么。
我尝试了几次并行加载150个文件,并在第60个文件上传成功后超时。有大约50个超时错误,需要大约1或2分钟,之后,上传继续,最后所有文件都成功上传,但这些超时减慢了很多上传...
我看到日志中的错误,但我不知道如何在代码中捕获它,上传请求(AWSS3TransferManagerUploadRequest)没有返回错误,所以我无法显示错误通知用户有什么东西去了错误。
有谁知道如何解决问题?如果没有超时解决方案,您是否知道如何捕获错误至少在屏幕上显示错误?
我已经尝试更改timeoutIntervalForRequest / timeoutIntervalForResource,但它不会改变任何内容。
错误是(五十次或更多):
20 AWSiOSSDK v2.4.8 [Error] AWSURLSessionManager.m line:212 | -[AWSURLSessionManager URLSession:task:didCompleteWithError:] | Session task failed with error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSErrorFailingURLStringKey=https://s3.amazonaws.com/***/user-72358/project-***/***.jpg?partNumber=1&uploadId=***, _kCFStreamErrorCodeKey=-2102, NSErrorFailingURLKey=https://s3.amazonaws.com/***/user-***/project-***/***.jpg?partNumber=1&uploadId=***, NSLocalizedDescription=The request timed out., _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x1dc69d80 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102}}}
这是我的代码:
func uploadAllFileRequests(imageArray:[String], completion:(filedataReturned:FileData?, uploadOfFileSucceeded: Bool, error: NSError?)-> Void,progress:(totalSent: Int64, totalExpect: Int64)-> Void) -> Void
{
var tasks = [AWSTask]()
let keyCred = s3CredentialsInfo?.key
AWSS3TransferManager.registerS3TransferManagerWithConfiguration(serviceConfiguration, forKey: keyCred)
let transferManager:AWSS3TransferManager = AWSS3TransferManager.S3TransferManagerForKey(keyCred)
for imageFilePath:String in imageArray {
...
let keyOnS3 = "\(keyCred!)/\(filename)"
// url image to upload to s3
let url:NSURL = NSURL(fileURLWithPath: filedata.getFullpath())
// next we set up the S3 upload request manager
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest?.bucket = s3CredentialsInfo?.bucket
uploadRequest?.key = keyOnS3
uploadRequest?.contentType = type
uploadRequest?.body = url
// Track Upload Progress through AWSNetworkingUploadProgressBlock
uploadRequest?.uploadProgress = {(bytesSent:Int64, totalBytesSent:Int64, totalBytesExpectedToSend:Int64) in
dispatch_sync(dispatch_get_main_queue(), { () -> Void in
progress(totalSent: currentTotal, totalExpect: totalSize)
})
}
tasks.append(transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock:{task -> AWSTask in
if(task.result != nil){
// upload success
print("-> upload successfull \(keyOnS3) on S3")
completion(filedataReturned:filedata, uploadOfFileSucceeded: true, error: nil)
}else{
print("->task error upload on S3 : \(task.error)")
}
return task
}))
}
AWSTask(forCompletionOfAllTasks:tasks).continueWithBlock({task -> AWSTask in
print("-> parallel task is \(task)")
if(!task.cancelled && !task.faulted){
// upload success
print("-> upload successfull on S3")
}else{
print("->error of parallel upload on S3")
completion(filedataReturned:nil, uploadOfFileSucceeded: false, error: task.error)
}
return task
})
}
我正在使用AWSCore和AWSS3 cocoapods版本2.4.8
提前感谢您的帮助
史蒂芬妮
答案 0 :(得分:0)
一旦网络速度慢,就会出现超时错误,为此,您只需要在配置中创建超时即可。
以您的情况
let serviceConfiguration = AWSServiceConfiguration(region: convertedRegion, credentialsProvider: credentialsProvider)
configuration?.timeoutIntervalForRequest = 60 // in seconds