尝试将图像上传到 s3,但由于未知原因每次都失败。
private func uploadImageToS3(withS3Path s3path: String, image: UIImage, progress: (Float -> Void)?) -> BFTask{
let path: NSString = NSTemporaryDirectory().stringByAppendingString("profile_image.jpg")
let imageData = UIImageJPEGRepresentation(image, 0.8)
imageData?.writeToFile(path as String, atomically: true)
let url: NSURL = NSURL(fileURLWithPath: path as String)
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.bucket = "my_bucket"
uploadRequest.key = s3path
uploadRequest.contentType = "image/jpg"
uploadRequest.body = url
let transferManager = CognitoCredentialProvider.sharedInstance.s3TransferManager
let completionSource = BFTaskCompletionSource()
transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: {(task:AWSTask!) in
if task.error != nil{
completionSource.setError(task.error!)
return nil
}else{
completionSource.setResult(s3path)
}
return nil
})
return completionSource.task
}
上传开始,但在短时间内失败。日志:
进度 0.126221
进度 0.252442
进度 0.378663
进度 0.504884
AWSiOSSDKv2 [错误] AWSURLSessionManager.m 行:260 | -[AWSURLSessionManager URLSession:task:didCompleteWithError:] | 会话任务失败并出现错误:错误域=NSURLErrorDomain Code=-1017“无法解析响应”UserInfo={NSUnderlyingError=0x7fda2f90e3c0 {Error Domain=kCFErrorDomainCFNetwork Code=-1017“(null)”UserInfo={_kCFStreamErrorCodeKey=-1, _kCFStreamErrorDomainKey=4 }}, NSErrorFailingURLStringKey= https://s3-us-west-2.amazonaws.com/my_bucket/profile_image/cognitoID/profile_image.jpg , NSErrorFailingURLKey= https://s3-us-west-2.amazonaws.com/my_bucket /profile_image/cognitoID/profile_image.jpg , _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1, NSLocalizedDescription=无法解析响应}
传输管理器初始化:
let kclIdentityProvider = KCLCognitoIdentityProvider(regionType: .USEast1, identityPoolId: "poolID", userSession: UserSession.sharedInstance)
let cognitoCredProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityProvider: kclIdentityProvider, unauthRoleArn: nil, authRoleArn: nil)
self.credProvider = cognitoCredProvider
let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: cognitoCredProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
// s3, sns use uswest2
let usWest2 = AWSServiceConfiguration(region: AWSRegionType.USWest2, credentialsProvider: self.credProvider)
AWSS3TransferManager.registerS3TransferManagerWithConfiguration(usWest2, forKey: "USWest2S3")
AWSSNS.registerSNSWithConfiguration(usWest2, forKey: "USWest2SNS")
self.snsClient = AWSSNS(forKey: "USWest2SNS")
self.s3TransferManager = AWSS3TransferManager.S3TransferManagerForKey("USWest2S3")
我能否以某种方式获得有关上传失败原因的更多信息?