Skip to content

Instantly share code, notes, and snippets.

@milankamilya
Last active March 1, 2021 14:24
Show Gist options
  • Save milankamilya/2d50097f11bb5327b13db6c904265c0d to your computer and use it in GitHub Desktop.
Save milankamilya/2d50097f11bb5327b13db6c904265c0d to your computer and use it in GitHub Desktop.
AWS S3 with Transfer Utility in Objective C
- (void) uploadFileURL: (NSURL *) fileURL key: (NSString *) key content: (NSString *) contentType {
// TODO: TRANSER MANAGER NEED TO BE UPDATED BASED REGISTER KEY
AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility S3TransferUtilityForKey:AWSS3_TRANSFER_MANAGER_CONFIGURATION_LMS];
@weakify(self)
//Create the completion handler for the transfer
AWSS3TransferUtilityUploadCompletionHandlerBlock completionHandler = ^(AWSS3TransferUtilityUploadTask *task, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self)
NULL_CHECKER(self);
if (error) {
// Handle Error
} else {
// Upload Successful
}
});
};
//Create the TransferUtility expression and add the progress block to it.
//This would be needed to report on progress tracking
AWSS3TransferUtilityUploadExpression *expression = [AWSS3TransferUtilityUploadExpression new];
expression.progressBlock = ^(AWSS3TransferUtilityTask *task, NSProgress *progress) {
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self)
NULL_CHECKER(self);
[self handleUploadTaskStatus:task.status fileKey:task.key progress:progress];
});
};
[[transferUtility uploadFile:fileURL
bucket: AWSS3_LMS_BUCKET_NAME
key:key
contentType:contentType
expression:expression
completionHandler:completionHandler] continueWithBlock:^id(AWSTask *task) {
if (task.error) {
// handle error
}
if (task.result) {
// Uploading Started
}
return nil;
}];
[self updateUploadingStatus:uploadRequest];
}
- (void) handleUploadTaskStatus: (AWSS3TransferUtilityTransferStatusType) statusType
fileKey: (NSString *) key
progress: (NSProgress *) progress {
NSLog(@"Upload Task Status %ld, progress %f", (long)statusType, progress.fractionCompleted);
switch (statusType) {
case AWSS3TransferUtilityTransferStatusInProgress:{
if (progress.fractionCompleted > 0) {
if (![key isEqualToString:thumbKey]) {
// Show Progress
}
} else {
// reset progress
}
}
break;
case AWSS3TransferUtilityTransferStatusPaused: {
[self.progressView pausedUpload];
}
break;
case AWSS3TransferUtilityTransferStatusCompleted: {
}
break;
case AWSS3TransferUtilityTransferStatusWaiting: {
}
break;
case AWSS3TransferUtilityTransferStatusError: {
[self.progressView failedUpload];
}
break;
case AWSS3TransferUtilityTransferStatusCancelled: {
[self.progressView canceledUpload];
}
break;
default:
[self.progressView failedUpload];
break;
}
}
- (void) handleUploadError:(NSError *) error {
if ([error.domain isEqualToString:AWSS3TransferUtilityErrorDomain]) {
switch (error.code) {
case AWSS3TransferUtilityErrorUnknown:
NSLog(@"%@", [error debugDescription]);
break;
case AWSS3TransferUtilityErrorRedirection:
NSLog(@"%@", [error debugDescription]);
break;
case AWSS3TransferUtilityErrorClientError:
NSLog(@"%@", [error debugDescription]);
break;
case AWSS3TransferUtilityErrorServerError:
NSLog(@"%@", [error debugDescription]);
break;
case AWSS3TransferUtilityErrorLocalFileNotFound:
NSLog(@"%@", [error debugDescription]);
break;
default:
NSLog(@"%@", [error debugDescription]);
break;
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.progressView failedUpload];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment