Ios AmazonS3上传两次
我在将图像上传到AmazonS3存储桶时遇到了一个奇怪的问题 我观察到,当我在一段时间(一小时?)没有使用应用程序后上传到我的bucket时,在上传完成之前,进度从0%到100%运行了两次。例如,我在下面的代码块中有一个示例,用于确定上载了多少%:Ios AmazonS3上传两次,ios,swift,xcode,amazon-s3,Ios,Swift,Xcode,Amazon S3,我在将图像上传到AmazonS3存储桶时遇到了一个奇怪的问题 我观察到,当我在一段时间(一小时?)没有使用应用程序后上传到我的bucket时,在上传完成之前,进度从0%到100%运行了两次。例如,我在下面的代码块中有一个示例,用于确定上载了多少%: // Track progress through an AWSNetworkingUploadProgressBlock uploadRequest?.uploadProgress = {[weak self](bytesSent:Int64, t
// 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)
})
}
对于打印输出,我看到下面的打印输出两次,我观察我的进度循环,在5秒内做两次360度旋转(上传时间)
奇怪的是,如果我在之后重新上传任何东西,它会像预期的那样工作,打印输出只会100%运行一次
下面是我如何处理S3的:
// 1. Save file to disk and retreive NSURL before we can store to Amazon S3
let path: String = NSTemporaryDirectory().stringByAppendingString("\(CURRENT_USER_UID)).jpg")
let imageData: NSData = UIImageJPEGRepresentation(img, 0.1)!
imageData.writeToFile(path, atomically: true)
let url: NSURL = NSURL(fileURLWithPath: path)
// 2. Create upload request
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.bucket = "myBucket"
uploadRequest.key = "\(CURRENT_USER_UID)\(Int(NSDate().timeIntervalSince1970)).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 Amazon S3
let transferManager = AWSS3TransferManager.defaultS3TransferManager()
transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task: AWSTask) -> AnyObject? in
if let error = task.error {
if error.domain == AWSS3TransferUtilityErrorDomain {
MRProgressOverlayView.dismissOverlayForView(self.view, animated: true)
self.enableUserInteraction()
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)")
MRProgressOverlayView.dismissOverlayForView(self.view, animated: true)
self.enableUserInteraction()
}
} else {
if task.completed {
let imgLink = "https://s3.amazonaws.com/mybucket/\(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()
}
}
return nil
----更新------
在查看控制台之后,我发现第一次上传没有成功,因为出现了以下错误
2016-08-20 16:42:51.524 Letzzee[20327:176849]AWSiOSSDK v2.4.5[错误]AWSURLSessionManager.m行:211 |-[AWSURLSessionManager URLSession:task:DidCompleteWitherError:]|会话任务失败,出现错误:错误域=NSURErrorDomain代码=-1005“网络连接丢失”
然而,我不明白为什么这种情况会定期发生。我认为这与我的网络连接无关,因为
// 1. Save file to disk and retreive NSURL before we can store to Amazon S3
let path: String = NSTemporaryDirectory().stringByAppendingString("\(CURRENT_USER_UID)).jpg")
let imageData: NSData = UIImageJPEGRepresentation(img, 0.1)!
imageData.writeToFile(path, atomically: true)
let url: NSURL = NSURL(fileURLWithPath: path)
// 2. Create upload request
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.bucket = "myBucket"
uploadRequest.key = "\(CURRENT_USER_UID)\(Int(NSDate().timeIntervalSince1970)).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 Amazon S3
let transferManager = AWSS3TransferManager.defaultS3TransferManager()
transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task: AWSTask) -> AnyObject? in
if let error = task.error {
if error.domain == AWSS3TransferUtilityErrorDomain {
MRProgressOverlayView.dismissOverlayForView(self.view, animated: true)
self.enableUserInteraction()
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)")
MRProgressOverlayView.dismissOverlayForView(self.view, animated: true)
self.enableUserInteraction()
}
} else {
if task.completed {
let imgLink = "https://s3.amazonaws.com/mybucket/\(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()
}
}
return nil