Last active
November 1, 2022 03:07
-
-
Save Nomad-Go/3bb8fea716408f0efbbfeef8c6dea9f7 to your computer and use it in GitHub Desktop.
Combine S3 Uploads
This file contains hidden or 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
| public func upload(uploadableObjects objects: [S3Uploadable]) -> AnyPublisher<[S3Uploadable], Error> { | |
| var futures: [Future<S3Uploadable, Error>] = [] | |
| for object in objects { | |
| let future = self.upload(objectToUpload: object) | |
| futures.append(future) | |
| } | |
| return Publishers.MergeMany(futures).collect().eraseToAnyPublisher() | |
| } | |
| public func upload(objectToUpload object: S3Uploadable) -> Future<S3Uploadable, Error> { | |
| let future: Future<S3Uploadable, Error> = Future<S3Uploadable, Error> { promise in | |
| guard let transferUtility = AWSS3TransferUtility.s3TransferUtility(forKey: self.utilityKey.key) else { | |
| promise(.failure(S3TransferError.couldNotRetrieveTransferUtility)) | |
| return | |
| } | |
| let expression = AWSS3TransferUtilityUploadExpression() | |
| expression.progressBlock = { (_ task: AWSS3TransferUtilityTask, _ progress: Progress) in | |
| object.progress.send(progress.fractionCompleted) | |
| } | |
| let completionHandle: AWSS3TransferUtilityUploadCompletionHandlerBlock = { (task, p_error) -> Void in | |
| guard let error = p_error else { | |
| promise(.success(object)) | |
| return | |
| } | |
| promise(.failure(error)) | |
| } | |
| let key: String = object.objectCloudKey | |
| let contentType: String = object.contentType.rawValue | |
| switch object.uploadableObjectLocation { | |
| case .DATA_OBJECT(let data): | |
| _ = transferUtility.uploadData(data, key: key, contentType: contentType, expression: expression, completionHandler: completionHandle).continueWith { (task) -> Any? in | |
| if let result = task.result { | |
| object.add(task: result) | |
| } | |
| guard let error = task.error else { | |
| return nil | |
| } | |
| promise(.failure(error)) | |
| return nil | |
| } as? AWSTask<AWSS3TransferUtilityTask> | |
| case .LOCAL_FILE(let url): | |
| _ = transferUtility.uploadFile(url, key: key, contentType: contentType, expression: expression, completionHandler: completionHandle).continueWith(block: { (task) -> Any? in | |
| if let result = task.result { | |
| object.add(task: result) | |
| } | |
| guard let error = task.error else { | |
| return nil | |
| } | |
| promise(.failure(error)) | |
| return nil | |
| }) as? AWSTask<AWSS3TransferUtilityTask> | |
| } | |
| } | |
| return future | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment