在 AWSS3 上并行上传文件时出错'The request timed out'



我在AWSS3中有一个并行上传的问题

我使用

AWSTask(forCompletionOfAllTasks:tasks).continueWithBlock({task -> AWSTask in

在S3上并行上传文件。它可以很好地处理少量文件,但是当我并行发送超过50个文件时,我有很多超时错误,我不明白为什么。

我尝试了几次并行加载150个文件,在成功上传第60个文件后我超时了。大约有50个超时错误,大约需要1到2分钟,之后,上传继续,最后所有文件都上传成功,但这些超时使上传速度变慢了很多…

我看到日志中的错误,但我不知道如何在代码中捕获它,上传请求(AWSS3TransferManagerUploadRequest)没有返回错误,所以我不能显示错误来通知用户出了问题。

有谁知道如何解决这个问题吗?如果超时没有解决方案,你知道如何捕获错误,至少在屏幕上显示一个错误吗?

我已经尝试改变timeoutIntervalForRequest/timeoutIntervalForResource,但它没有改变任何东西。

错误是(50次以上):

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版本

提前感谢您的帮助

斯蒂芬妮

当你的网络很慢时,超时错误就会出现,为此你只需要在配置中增加超时。

在你的情况下

let serviceConfiguration = AWSServiceConfiguration(region: convertedRegion, credentialsProvider: credentialsProvider)
configuration?.timeoutIntervalForRequest = 60 // in seconds

相关内容

  • 没有找到相关文章

最新更新