6. Secure Amazon S3 Transfer Utility

Before using SecureTransferUtility methods make sure you have configured the AWSServiceManager defaultServiceConfiguration.

Let us first look at how a typical app is created using AWSS3 SDK AWSS3TransferUtility and then look at the components of the S3 app that get affected using Bayun AWSS3 wrapper class SecureAWSS3TransferUtility.

6.1 Transferring Data using standard AWSS3TransferUtility

6.1.1 Uploading a File

Here are code snippets you can use for uploading a file.

Objective-C
Swift


NSURL *fileURL = // The file to upload.

AWSS3TransferUtilityUploadExpression *expression = [AWSS3TransferUtilityUploadExpression new];
expression.progressBlock = ^(AWSS3TransferUtilityTask *task, NSProgress *progress) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // Do something e.g. Update a progress bar.
    });
};

AWSS3TransferUtilityUploadCompletionHandlerBlock completionHandler = ^(AWSS3TransferUtilityUploadTask *task, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"File upload completed");
        });
 };
 
AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility defaultS3TransferUtility];

[[transferUtility uploadFile:fileURL
                     bucket:@"YourBucketName"
                        key:@"YourObjectKeyName"
                contentType:@"text/plain"
                 expression:expression
           completionHander:completionHandler]continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
    }
    if (task.exception) {
        NSLog(@"Exception: %@", task.exception);
    }
    if (task.result) {
        AWSS3TransferUtilityUploadTask *uploadTask = task.result;
        // Do something with uploadTask.
    }
    return nil;
}];



let progressBlock: AWSS3TransferUtilityProgressBlock =  {(task, progress) in
    dispatch_async(dispatch_get_main_queue(), {
        // Do something e.g. Update a progress bar.
    })
}

let expression = AWSS3TransferUtilityUploadExpression()
expression.progressBlock = progressBlock
        
let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility()
let completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock = { (task, error) -> Void in
    dispatch_async(dispatch_get_main_queue(), {
        if ((error) != nil){
            NSLog("Failed with error")
            NSLog("Error: %@",error!);
        } else{
            NSLog("success")
        }
    })
}
        
transferUtility!.uploadFile(fileURL,
                            bucket: "YourBucketName",
                            key: "YourObjectKeyName",
                    contentType: "text/plain",
                     expression: expression,
               completionHander: completionHandler).continueWithBlock { (task) -> AnyObject! in
                if let error = task.error {
                    NSLog("Error: %@",error.localizedDescription);
                }
                if let exception = task.exception {
                    NSLog("Exception: %@",exception.description);
                }
                if let _ = task.result {
                    NSLog("Upload Starting!")
                    // Do something with uploadTask.
                }
                return nil;
}

6.1.2 Uploading Binary Data

Here are code snippets you can use for uploading binary data.

Objective-C
Swift


NSData *dataToUpload = // The data to upload.

AWSS3TransferUtilityUploadExpression *expression = [AWSS3TransferUtilityUploadExpression new];
expression.progressBlock = ^(AWSS3TransferUtilityTask *task, NSProgress *progress) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // Do something e.g. Update a progress bar.
    });
};

AWSS3TransferUtilityUploadCompletionHandlerBlock completionHandler = ^(AWSS3TransferUtilityUploadTask *task, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // Do something e.g. Alert a user for transfer completion.
        // On failed uploads, `error` contains the error object.
    });
};

AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility defaultS3TransferUtility];

[[transferUtility uploadData:data
                      bucket:@"YourBucketName"
                         key:@"YourObjectKeyName"
                 contentType:@"text/plain"
                  expression:expression
            completionHander:completionHandler] continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
    }
    if (task.exception) {
        NSLog(@"Exception: %@", task.exception);
    }
    if (task.result) {
        AWSS3TransferUtilityUploadTask *uploadTask = task.result;
        // Do something with uploadTask.
    }
    return nil;
}];



let progressBlock: AWSS3TransferUtilityProgressBlock =  {(task, progress) in
    dispatch_async(dispatch_get_main_queue(), {
       // Do something e.g. Update a progress bar.
    })
}
        
let expression = AWSS3TransferUtilityUploadExpression()
expression.progressBlock = progressBlock
   
let transferUtility = SecureAWSS3TransferUtility.defaultS3TransferUtility()
let completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock = { (task, error) -> Void in
    dispatch_async(dispatch_get_main_queue(), {
        // Do something e.g. Alert a user for transfer completion.
        // On failed uploads, `error` contains the error object.
    })
}
        
transferUtility!.uploadData(dataToUpload,
                            bucket: "YourBucketName",
                            key: "YourObjectKeyName",
                            contentType: "text/plain",
                            expression: expression,
                            completionHander: completionHandler).continueWithBlock { (task) -> AnyObject! in
                            
                                if let error = task.error {
                                    NSLog("Error: %@",error.localizedDescription);
                                }
                                if let exception = task.exception {
                                    NSLog("Exception: %@",exception.description);
                                }
                                if let _ = task.result {
                                    NSLog("Upload Starting!")
                                    // Do something with uploadTask.
                                }
                            return nil;
}


6.1.3 Downloading to a File

Here are code snippets you can use for downloading to a file.

Objective-C
Swift

NSURL *fileURL = // The file URL of the download destination.

AWSS3TransferUtilityDownloadExpression *expression = [AWSS3TransferUtilityDownloadExpression new];
expression.progressBlock = ^(AWSS3TransferUtilityTask *task, NSProgress *progress) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // Do something e.g. Update a progress bar.
    });
};

AWSS3TransferUtilityDownloadCompletionHandlerBlock completionHandler = ^(AWSS3TransferUtilityDownloadTask *task, NSURL *location, NSData *data, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // Do something e.g. Alert a user for transfer completion.
        // On successful downloads, `location` contains the S3 object file URL.
        // On failed downloads, `error` contains the error object.
    });
};

AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility defaultS3TransferUtility];
[[transferUtility downloadToURL:fileURL
                         bucket:S3BucketName
                            key:S3DownloadKeyName
                     expression:expression
               completionHander:completionHandler] continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
    }
    if (task.exception) {
        NSLog(@"Exception: %@", task.exception);
    }
    if (task.result) {
        AWSS3TransferUtilityDownloadTask *downloadTask = task.result;
        // Do something with downloadTask.
    }
    return nil;
}];


let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in
    dispatch_async(dispatch_get_main_queue(), {
        // Do something e.g. Update a progress bar.
    })
}
        
let completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock  = { (task, location, data, error) -> Void in
    dispatch_async(dispatch_get_main_queue(), {
        if ((error) != nil) {
            NSLog("Failed with error")
            NSLog("Error: %@",error!);
        } else {
            if((data) != nil) {
                NSLog("download Successful")
            }
        }
    })
}
        
let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility()
transferUtility!.downloadToURL(fileURL,
                                bucket: "bayun-company13",
                                key: "file.pdf",
                                expression: expression,
            completionHander: completionHandler).continueWithBlock { (task) -> AnyObject? in
                if let error = task.error {
                    NSLog("Error: %@",error.localizedDescription);
                }
                if let exception = task.exception {
                    NSLog("Exception: %@",exception.description);
                }
                if let _ = task.result {
                    NSLog("Download Starting!")
                    // Do something with uploadTask.
                }
                return nil;
}

6.1.4 Downloading as Binary Data

Here are code snippets you can use for downloading binary data.

Objective-C
Swift

AWSS3TransferUtilityDownloadExpression *expression = [AWSS3TransferUtilityDownloadExpression new];
expression.progressBlock = ^(AWSS3TransferUtilityTask *task, NSProgress *progress) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // Do something e.g. Update a progress bar.
    });
};

AWSS3TransferUtilityDownloadCompletionHandlerBlock completionHandler = ^(AWSS3TransferUtilityDownloadTask *task, NSURL *location, NSData *data, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // Do something e.g. Alert a user for transfer completion.
        // On successful downloads, `data` contains the S3 object.
        // On failed downloads, `error` contains the error object.
    });
};

AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility defaultS3TransferUtility];
[[transferUtility downloadDataFromBucket:S3BucketName
                                     key:S3DownloadKeyName
                              expression:expression
                        completionHander:completionHandler] continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
    }
    if (task.exception) {
        NSLog(@"Exception: %@", task.exception);
    }
    if (task.result) {
        AWSS3TransferUtilityDownloadTask *downloadTask = task.result;
        // Do something with downloadTask.
    }
    return nil;
}];

let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in
        dispatch_async(dispatch_get_main_queue(), {
        // Do something e.g. Update a progress bar.
        })
}
        
let completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock  = { (task, location, data, error) -> Void in
    dispatch_async(dispatch_get_main_queue(), {
        if ((error) != nil){
            NSLog("Failed with error")
        } else {
            if((data) != nil) {
                NSLog("Download Successful")
            }
         }
    })
}
        
let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility()
transferUtility!.downloadDataFromBucket(S3BucketName,
                                        key: S3DownloadKeyName,
                                        expression: expression,
                                        completionHander: completionHandler).continueWithBlock { (task) -> AnyObject? in
                                        if let error = task.error {
                                            NSLog("Error: %@",error.localizedDescription);
                                        }
                
                                        if let exception = task.exception {
                                             NSLog("Exception: %@",exception.description);
                                        }
                                        
                                        if let _ = task.result {
                                            NSLog("Download Starting!")
                                            // Do something with uploadTask.
                                        }
                                        return nil;
}

6.2 Transferring Data using Bayun's SecureAWSS3TransferUtility

Now in order to use Bayun’s SecureAWSS3TransferUtility instead of the standard AWS S3 AWSS3TransferUtility, these code snippets above change to using “Secure” version, as below:

Add the following import statement in Objective-C:

#import "SecureAWSS3TransferUtility.h"

As you can see from the code snippets below, in general it should be possible to simply query-replace the following type-names appropriately to their secure versions, and in most situations that should be sufficient.

AWSS3TransferUtility --> SecureAWSS3TransferUtility

6.2.1 Configuring the Application Delegate

The Secure Transfer Utility for iOS uses the background transfer feature in iOS to continue data transfers even when your app isn’t running.

Call the following class method in your - application:handleEventsForBackgroundURLSession:completionHandler: application delegate so that iOS can tell the Transfer Utility when the transfer finishes while the app is not suspended.

Objective-C
Swift


- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
  completionHandler:(void (^)())completionHandler {
    /* Store the completion handler.*/
    [SecureAWSS3TransferUtility interceptApplication:application handleEventsForBackgroundURLSession:identifier completionHandler:completionHandler];
}



func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -> Void) {
       SecureAWSS3TransferUtility.interceptApplication(application, handleEventsForBackgroundURLSession:identifier , completionHandler:completionHandler )
}
    

6.2.2 Uploading a file using SecureAWSS3TransferUtility

To upload a file call uploadFile:bucket:key:contentType:expression:completionHander: on SecureAWSS3TransferUtility:

Objective-C
Swift


NSURL *fileURL = // The file to upload.

AWSS3TransferUtilityDownloadExpression *expression = [AWSS3TransferUtilityDownloadExpression new];
expression.progressBlock = ^(AWSS3TransferUtilityTask *task, NSProgress *progress) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // Do something e.g. Update a progress bar.
    });
};

AWSS3TransferUtilityDownloadCompletionHandlerBlock completionHandler = ^(AWSS3TransferUtilityDownloadTask *task, NSURL *location, NSData *data, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // Do something e.g. Alert a user for transfer completion.
        // On failed downloads, `error` contains the error object.
    });
};

SecureAWSS3TransferUtility *transferUtility = [SecureAWSS3TransferUtility defaultS3TransferUtility];

[[transferUtility uploadFile:fileURL
                     bucket:@"YourBucketName"
                        key:@"YourObjectKeyName"
                contentType:@"text/plain"
                 expression:expression
           completionHander:completionHander]continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
    }
    if (task.exception) {
        NSLog(@"Exception: %@", task.exception);
    }
    if (task.result) {
        AWSS3TransferUtilityUploadTask *uploadTask = task.result;
        // Do something with uploadTask.
    }
    return nil;
}];



let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in
    dispatch_async(dispatch_get_main_queue(), {
        // Do something e.g. Update a progress bar.
    })
}
        
let completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock  = { (task, location, data, error) -> Void in
    dispatch_async(dispatch_get_main_queue(), {
        if ((error) != nil) {
            NSLog("Failed with error")
            NSLog("Error: %@",error!);
        } else {
            if((data) != nil) {
                NSLog("download Successful")
            }
        }
    })
}
 
let transferUtility = SecureAWSS3TransferUtility.defaultS3TransferUtility()        
transferUtility!.uploadFile(fileURL,
                            bucket: "YourBucketName",
                            key: "YourObjectKeyName",
                    contentType: "text/plain",
                     expression: expression,
               completionHander: completionHandler).continueWithBlock { (task) -> AnyObject! in
                if let error = task.error {
                    NSLog("Error: %@",error.localizedDescription);
                }
                if let exception = task.exception {
                    NSLog("Exception: %@",exception.description);
                }
                if let _ = task.result {
                    NSLog("Upload Starting!")
                    // Do something with uploadTask.
                }
                return nil;
}

6.2.3 Downloading to a File using SecureAWSS3TransferUtility

Here are code snippets you can use for downloading to a file.

Objective-C
Swift


NSURL *fileURL = // The file URL of the download destination.

SecureAWSS3TransferUtility *transferUtility = [SecureAWSS3TransferUtility defaultS3TransferUtility];
[[transferUtility downloadToURL:fileURL
                         bucket:S3BucketName
                            key:S3DownloadKeyName
                     expression:expression
               completionHander:completionHandler] continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
    }
    if (task.exception) {
        NSLog(@"Exception: %@", task.exception);
    }
    if (task.result) {
        AWSS3TransferUtilityDownloadTask *downloadTask = task.result;
        // Do something with downloadTask.
    }
    return nil;
}];



let transferUtility = SecureAWSS3TransferUtility.defaultS3TransferUtility()
transferUtility!.downloadToURL(fileURL,
                                bucket: "bayun-company13",
                                key: "file.pdf",
                                expression: expression,
            completionHander: completionHandler).continueWithBlock { (task) -> AnyObject? in
                if let error = task.error {
                    NSLog("Error: %@",error.localizedDescription);
                }
                if let exception = task.exception {
                    NSLog("Exception: %@",exception.description);
                }
                if let _ = task.result {
                    NSLog("Download Starting!")
                    // Do something with uploadTask.
                }
                return nil;
}

results matching ""

    No results matching ""