Amazon Web Service S3超时

时间:2016-09-07 06:58:35

标签: ios swift amazon-web-services amazon-s3

我正在使用S3 bucket从我的swift项目上传图片,但是,我的上传时不时会暂停并在控制台中向我发出以下警告。但是,我不确定为什么,但上传似乎自动重启,第二次几乎总是成功。之后的任何上传通常都很好。 (只有当我暂时没有与服务器交互时

AWSiOSSDK v2.4.7 [Error] AWSURLSessionManager.m line:212 | -[AWSURLSessionManager URLSession:task:didCompleteWithError:] | Session task failed with error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x13eba2b50 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://s3.amazonaws.com/myBucketName/filename.jpg, NSErrorFailingURLKey=https://s3.amazonaws.com/myBucketName/filename.jpg, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}

以下是我向S3发送文件的方式

    // 2. Create upload request
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.bucket = "myBucket"
uploadRequest.key = "FileName.jpg"
uploadRequest.ACL = AWSS3ObjectCannedACL.PublicRead
uploadRequest.contentType = "image/jpg"
uploadRequest.body = url


// Track progress through an AWSNetworkingUploadProgressBlock
uploadRequest?.uploadProgress = {[weak self](bytesSent:Int64, totalBytesSent:Int64, totalBytesExpectedToSend:Int64) in
    dispatch_sync(dispatch_get_main_queue(), { () -> Void in

        let progress = Float(totalBytesSent) / Float(totalBytesExpectedToSend)

        print(" totalBytesSent \(totalBytesSent) / totalBytesExpectedToSend \(totalBytesExpectedToSend) progress = \(progress * 100 ) %")

        circularProgressView.setProgress(progress, animated: true)

    })
}

// 3. Upload to Amazone S3
let transferManager = AWSS3TransferManager.defaultS3TransferManager()
transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task: AWSTask) -> AnyObject? in

    MRProgressOverlayView.dismissOverlayForView(self.view, animated: true)
    self.enableUserInteraction()

    if let error = task.error {

        if error.domain == AWSS3TransferUtilityErrorDomain {

            switch task.error?.code {
            case AWSS3TransferManagerErrorType.Cancelled.rawValue?:
                break
            case AWSS3TransferManagerErrorType.Paused.rawValue?:
                break
            default:
                self.showErrorAlert("Error uploading image #1", message: "\(error)")
            }
        } else {
            self.showErrorAlert("Error uploading image #2", message: "\(error)")
        }

    } else {

        if task.completed {
            let imgLink = "https://s3.amazonaws.com/letzzeeamazones3bucket2/\(uploadRequest.key!)"
            self.postToFirebase(imgLink, userCoordinate: userCoordinate)

        } else {
            self.showErrorAlert("Error uploading image #3", message: "Please try again later")
            MRProgressOverlayView.dismissOverlayForView(self.view, animated: true)
            self.enableUserInteraction()
        }
    }

1 个答案:

答案 0 :(得分:2)

每当亚马逊AWS Mobile SDK在控制台中显示不明确的警告时,请打开 verbose 日志记录进行调试:

(斯威夫特4)AWSDDLog.sharedInstance.logLevel = .verbose

(斯威夫特3) AWSLogger.default().logLevel = .verbose
(斯威夫特2)AWSLogger.defaultLogger().logLevel = .Verbose

...以显示有关正在使用的S3存储桶,区域,端点等的更多信息。