5. Store And Retrieve Files securely from Amazon S3

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

5.1 Authenticate User

Add the following import statement in Objective-C:

#import <AWSCore/AWSCore.h>

Add the following code to application:didFinishLaunchingWithOptions method:

Objective-C
Swift

AWSStaticCredentialsProvider *credentialsProvider = [[AWSStaticCredentialsProvider alloc]
                                                       initWithAccessKey : YourS3AccessKey
                                                       secretKey : YourS3SecretKey];  AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc]     initWithRegion:AWSRegionUSWest2 credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;



let credentialsProvider : AWSStaticCredentialsProvider = AWSStaticCredentialsProvider(accessKey:"YourS3AccessKey", secretKey: "YourS3SecretKey")
let defaultServiceConfiguration = AWSServiceConfiguration(region: AWSRegionType.USWest2,   credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = defaultServiceConfiguration

5.2 Transferring Data using Standard AWSS3TransferManager

5.2.1 Upload a File to Amazon S3 using AWSS3TransferManager

Create a AWSS3TransferManagerUploadRequest instance specifying the file to upload and the destination bucket.

Submit the upload request to the S3 service asynchronously with AWSS3TransferManager:

Objective-C
Swift

AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = @"example-bucket";
uploadRequest.key = @"test.txt";
uploadRequest.body = self.uploadingFileURL;

AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
    
//call the AWSS3TransferManager upload method with the uploadRequest , AWSExecutor (responsible for determining how the continuation block will be run) and the completion block (taking the executor and the completion block as parameters to have synchronous secure upload request)
[transferManager upload:uploadRequest continueWithExecutor:[AWSExecutor mainThreadExecutor]  withBlock:^id(AWSTask *task) {
 if (task.error) {  
     NSLog(@"Error uploading");
   }
 } else {
    NSLog(@"Upload completed");
 }
}];


let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.body = uploadingFileURL
uploadRequest.key = @"test.txt"
uploadRequest.bucket = @"example-bucket"
        
let transferManager = AWSS3TransferManager.defaultS3TransferManager()
        
transferManager.upload(uploadRequest, continueWithExecutor: AWSExecutor.mainThreadExecutor(), withBlock:  { (task) -> AnyObject? in
    if let error = task.error {
       NSLog("Error uploading : [\(error)]")
    }
            
    if let exception = task.exception {
        NSLog("Error uploading : [\(exception)]")
    }
            
    if task.result != nil {
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            NSLog("Upload completed")
        })
    }
    return nil
})

5.2.2 Download a File from Amazon S3 using AWSS3TransferManager

Create a destination URL where file will be downloaded. Create the download request. Submit the download request with AWSS3TransferManager.

Objective-C
Swift

NSString *downloadingFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:FileName];
NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];

AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];
downloadRequest.bucket = @"example-bucket";
downloadRequest.key = @"test.txt";
downloadRequest.downloadingFileURL = self.downloadingFileURL;

AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
 
//call the AWSS3TransferManager download method with the downloadRequest , AWSExecutor (responsible for determining how the continuation block will be run) and the completion block(taking the executor and the completion block as parameters to have synchronous secure download request)
 
[transferManager download:downloadRequest continueWithExecutor:[AWSExecutor mainThreadExecutor]  withBlock:^id(AWSTask *task) {
  if (task.error){  
       NSLog(@"Error downloading");
 } else {
    NSLog(@"Download completed");
 }
 return nil;
}];


let downloadRequest = AWSS3TransferManagerDownloadRequest()
downloadRequest.bucket = @"example-bucket"
downloadRequest.key = @"test.txt"
downloadRequest.downloadingFileURL = downloadingFileURL
        
let transferManager = AWSS3TransferManager.defaultS3TransferManager()
transferManager.download(downloadRequest, continueWithExecutor: AWSExecutor.mainThreadExecutor(), withBlock:  { (task) -> AnyObject? in
    if let error = task.error {
        NSLog("Download() failed: [\(error)]")
    }
            
    if let exception = task.exception {
        NSLog("Download() failed: [\(exception)]")
    }
    
    if task.result != nil {
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    NSLog("Download Completed")
        })
    }
    return nil
})

5.3 Transferring Data using Bayun’s S3Wrapper

Now in order to use Bayun’s S3Wrapper instead of the standard AWS S3 SDK classes, these code snippets above change to using “Secure” versions of the corresponding classes, as below:

Add the following import statement in Objective-C:

#import "SecureAWSS3TransferManager.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.

    • AWSS3TransferManager --> SecureAWSS3TransferManager
    • AWSS3TransferManagerErrorType --> SecureAWSS3TransferManagerErrorType
    • AWSS3TransferManagerErrorDomain --> SecureAWSS3TransferManagerErrorDomain
5.3.1 Upload a File to Amazon S3 using SecureAWSS3TransferManager

Submit the upload request to the S3 service asynchronously with SecureAWSS3TransferManager:

Objective-C
Swift


SecureAWSS3TransferManager *transferManager = [SecureAWSS3TransferManager defaultS3TransferManager];

//call the SecureAWSS3TransferManager upload method with the uploadRequest , AWSExecutor (responsible for determining how the continuation block will be run) and the completion block (taking the executor and the completion block as parameters to have synchronous secure upload request)
[transferManager upload:uploadRequest continueWithExecutor:[AWSExecutor mainThreadExecutor]  withBlock:^id(AWSTask *task) {
//[OPTIONAL] The following error handling conditions are optional and client app may apply checks against the SecureAWSS3TransferManagerErrorType as per its requirement
 if (task.error) {
   if (task.error.code == SecureAWSS3TransferManagerErrorUserInactive) {
       NSLog(@"Error uploading : User Inactive");
   } else if (task.error.code == SecureAWSS3TransferManagerErrorAccessDenied) {
       NSLog(@"Error uploading : Access Denied");
   } else {
       NSLog(@"Error uploading");
   }
 } else {
    NSLog(@"Upload completed");
 }
}];



let transferManager = SecureAWSS3TransferManager.defaultS3TransferManager()

transferManager.upload(uploadRequest, continueWithExecutor: AWSExecutor.mainThreadExecutor(), withBlock:  { (task) -> AnyObject? in
    if let error = task.error {
        if error.domain == SecureAWSS3TransferManagerErrorDomain as String {
            if let errorCode = SecureAWSS3TransferManagerErrorType(rawValue: error.code) {
                if(errorCode == .UserInactive) {
                    NSLog("Error uploading : User Inactive")
                } else if (errorCode == .AccessDenied) {
                    NSLog("Error uploading : Access Denied")
                } else {
                    NSLog("Error uploading")
                }
            } else {
                NSLog("Error uploading : [\(error)]")
                }
        } else {
            NSLog("Error uploading : [\(error)]")
        }
    }

    if let exception = task.exception {
        NSLog("Error uploading : [\(exception)]")
    }

    if task.result != nil {
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            NSLog("Upload completed")
        })
    }
    return nil
})

5.3.2 Download a File from Amazon S3 using SecureAWSS3TransferManager

Submit the download request with SecureAWSS3TransferManager :

Objective-C
Swift

SecureAWSS3TransferManager *transferManager = [SecureAWSS3TransferManager defaultS3TransferManager];

//call the SecureAWSS3TransferManager download method with the downloadRequest , AWSExecutor (responsible for determining how the continuation block will be run) and the completion block(taking the executor and the completion block as parameters to have synchronous secure download request)

[transferManager download:downloadRequest continueWithExecutor:[AWSExecutor mainThreadExecutor]  withBlock:^id(AWSTask *task) {
  if (task.error){    
   //[OPTIONAL] The following error handling conditions are optional and client app may apply checks against the SecureAWSS3TransferManagerErrorType as per its requirement
   if (task.error.code == SecureAWSS3TransferManagerErrorUserInactive) {
       NSLog(@"Error downloading : User Inactive");
   } else if (task.error.code == SecureAWSS3TransferManagerErrorAccessDenied) {
       NSLog(@"Error downloading : Access Denied");
   } else {
       NSLog(@"Error downloading");
   }
 } else {
    NSLog(@"Download completed");
 }
 return nil;
}];

let transferManager = SecureAWSS3TransferManager.defaultS3TransferManager()
transferManager.download(downloadRequest, continueWithExecutor: AWSExecutor.mainThreadExecutor(), withBlock:  { (task) -> AnyObject? in
    if let error = task.error {
        if error.domain == SecureAWSS3TransferManagerErrorDomain as String {
            if let errorCode = SecureAWSS3TransferManagerErrorType(rawValue: error.code) {
                if(errorCode == .UserInactive) {
                    NSLog("Error downloading : User Inactive")
                } else if (errorCode == .AccessDenied) {
                    NSLog("Error downloading : Access Denied")
                } else {
                    NSLog("Error downloading")
                }
            } else {
                NSLog("Download() failed: [\(error)]")
            }
        } else {
                NSLog("Download() failed: [\(error)]")
        }
    }

    if let exception = task.exception {
        NSLog("Download() failed: [\(exception)]")
    }

    if task.result != nil {
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    NSLog("Download Completed")
        })
    }
    return nil
})

results matching ""

    No results matching ""