亚马逊S3上传两次

时间:2016-08-20 07:05:34

标签: ios swift xcode amazon-s3

将图像上传到Amazon S3存储桶时遇到了一个奇怪的问题。

我观察到的是,当我在一段时间内没有使用该应用程序(一小时?)后上传到我的存储桶时,在上传完成之前,进度从0到100%TWICE。例如,我有一个以下代码块来确定已上载了多少%:

// 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度旋转(上传时间)。

 totalBytesSent 32677 / totalBytesExpectedToSend 107360 progress = 30.4368 %
 totalBytesSent 65354 / totalBytesExpectedToSend 107360 progress = 60.8737 %
 totalBytesSent 98031 / totalBytesExpectedToSend 107360 progress = 91.3105 %
 totalBytesSent 107360 / totalBytesExpectedToSend 107360 progress = 100.0 %
 totalBytesSent 107360 / totalBytesExpectedToSend 107360 progress = 100.0 %

奇怪的是,如果我之后重新上传任何内容,它会按预期工作,打印出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

---- UPDATE ------ 看完控制台后,我发现第一次上传没有成功,因为出现以下错误

2016-08-20 16:42:51.524 Letzzee [20327:176849] AWSiOSSDK v2.4.5 [错误] AWSURLSessionManager.m行:211 | - [AWSURLSessionManager URLSession:task:didCompleteWithError:] |会话任务失败,错误:错误域= NSURLErrorDomain代码= -1005“网络连接丢失。”

然而,我不知道为什么会定期发生这种情况。我不认为这与我的互联网连接有关,因为

  1. 我曾尝试使用WIFI / 3G手机和带WIFI的模拟器。我不。如果是互联网问题,它应该一直发生或不定期发生。

  2. 上传总是转到100%并显示该消息。然后它自动重新上传,第二次几乎总是成功。

0 个答案:

没有答案