Last active
March 28, 2018 08:34
-
-
Save mbalex99/e1c69727884d370622a3 to your computer and use it in GitHub Desktop.
RxSwift with AWSS3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return create { (observer: AnyObserver<UploadSignal>) -> Disposable in | |
let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility() | |
let key = String(format: "uploads/%@.png", arguments: [NSUUID().UUIDString]) | |
let fullUrl = String(format: "https://s3.amazonaws.com/edenmessenger/%@", arguments: [key]) | |
var uploadTask : AWSS3TransferUtilityUploadTask? | |
var completionUploadTask : AWSS3TransferUtilityUploadTask? | |
transferUtility?.uploadData(data, bucket: Constants.awsS3BucketName, key: key, contentType: "image/png", expression: nil, completionHander: { (uploadTask, err) -> Void in | |
completionUploadTask = uploadTask | |
if let error = err { | |
observer.onError(error) | |
}else { | |
observer.onNext(UploadSignal(uploadedUrl: fullUrl, uploadProgress: 1)) | |
observer.onCompleted() | |
} | |
}).continueWithBlock({ (task) -> AnyObject! in | |
if let error = task.error { | |
observer.onError(error) | |
} | |
if let exception = task.exception { | |
print("AWS Upload Exception :", exception.description) | |
} | |
if let thisUploadTask = task.result as? AWSS3TransferUtilityUploadTask { | |
uploadTask = thisUploadTask | |
} | |
return task | |
}) | |
return AnonymousDisposable{ [weak uploadTask, completionUploadTask] in | |
uploadTask?.cancel() | |
completionUploadTask?.cancel() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment