我正在尝试将文件上传到AWS S3存储桶。如果上传因任何原因失败,我希望能够重试几次。
我有一个自定义的NSOperation,其主方法创建一个AWSS3TransferManagerUploadRequest()实例。在AWSS3TransferManager的帮助下,我开始上传。
如果失败了,我应该在task.completionBlock中调用main()吗?或者我应该使用我的NSOperation的completionBlock,并尝试从那里重新启动操作? 这里是一些清晰的代码
class PhotoUploadOperation: NSOperation {
var photoURL: NSURL!
var fileName: String!
var festival: PFObject!
var sleepTime: NSTimeInterval = 2.0
init(photoURL: NSURL, withFileName fileName:String, forFestival festival: PFObject) {
super.init()
self.photoURL = photoURL
self.fileName = fileName
self.festival = festival
}
override func main() {
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.bucket = "festivals123";
uploadRequest.key = fileName
uploadRequest.contentType = "image/jpeg"
uploadRequest.body = photoURL
uploadRequest.ACL = .PublicRead
AWSS3TransferManager.registerS3TransferManagerWithConfiguration(DataManager.shared.awsConfiguration, forKey: "ID1")
let transferManager = AWSS3TransferManager.S3TransferManagerForKey("ID1")
let task = transferManager.upload(uploadRequest)
task.continueWithBlock { (task: AWSTask!) -> AnyObject! in
if (task.faulted) {
print("now we need to restart upload for \(self.fileName)")
NSThread.sleepForTimeInterval(self.sleepTime)
if (self.sleepTime <= 2 ) {
self.sleepTime = self.sleepTime*2
}
self.main()
}
return task
}
}
}