How to store and retrieve files and data from AWS using Transfer-manager

How to store and retrieve files and data from AWS using Transfer-manager

Both the Amazon S3 TransferManager and TransferUtitliy classes make it easy for you to upload and download files from Amazon S3 while optimizing for performance and reliability. Both hide the complexity of making asynchronous file transfers behind simple APIs

OBJECTIVE-C

#import <AWSS3/AWSS3.h>

AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];

 

Download

NSString *downloadingFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"myImage.jpg"];
NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];

AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];

downloadRequest.bucket = @"myBucket";
downloadRequest.key = @"myImage.jpg";
downloadRequest.downloadingFileURL = downloadingFileURL;



[[transferManager download:downloadRequest ] continueWithExecutor:[AWSExecutor mainThreadExecutor]
withBlock:^id(AWSTask *task) {
if (task.error){
if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
switch (task.error.code) {
case AWSS3TransferManagerErrorCancelled:
case AWSS3TransferManagerErrorPaused:
break;

default:
NSLog(@"Error: %@", task.error);
break;
}

} else {
NSLog(@"Error: %@", task.error);
}
}

if (task.result) {
AWSS3TransferManagerDownloadOutput *downloadOutput = task.result;
}
return nil;
}];

 

Upload

 

NSURL *uploadingFileURL = [NSURL fileURLWithPath: @"your/file/path/myTestFile.txt"];

AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];

uploadRequest.bucket = @"myBucket";
uploadRequest.key = @"myTestFile.txt";
uploadRequest.body = uploadingFileURL;



[[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor]
withBlock:^id(AWSTask *task) {
if (task.error) {
if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
switch (task.error.code) {
case AWSS3TransferManagerErrorCancelled:
case AWSS3TransferManagerErrorPaused:
break;

default:
NSLog(@"Error: %@", task.error);
break;
}
} else {
// Unknown error.
NSLog(@"Error: %@", task.error);
}
}

if (task.result) {
AWSS3TransferManagerUploadOutput *uploadOutput = task.result;
// The file uploaded successfully.
}
return nil;
}];

 

Displaying a Downloaded Image in an UIImageView

self.imageView.image = [UIImage imageWithContentsOfFile:downloadingFilePath];

 

=========================================
SWIFT

import AWSS3

let transferManager = AWSS3TransferManager.default()

Download

 

let downloadingFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("myImage.jpg")

let downloadRequest = AWSS3TransferManagerDownloadRequest()

downloadRequest.bucket = "myBucket"
downloadRequest.key = "myImage.jpg"
downloadRequest.downloadingFileURL = downloadingFileURL

transferManager.download(downloadRequest).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask) -> Any? in

if let error = task.error as? NSError {
if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
switch code {
case .cancelled, .paused:
break
default:
print("Error downloading: \(downloadRequest.key) Error: \(error)")
}
} else {
print("Error downloading: \(downloadRequest.key) Error: \(error)")
}
return nil
}
print("Download complete for: \(downloadRequest.key)")
let downloadOutput = task.result
return nil
})

 

Upload

let uploadingFileURL = URL(fileURLWithPath: "your/file/path/myTestFile.txt")

let uploadRequest = AWSS3TransferManagerUploadRequest()

uploadRequest.bucket = "myBucket"
uploadRequest.key = "myTestFile.txt"
uploadRequest.body = uploadingFileURL

transferManager.upload(uploadRequest).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask) -> Any? in

if let error = task.error as? NSError {
if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
switch code {
case .cancelled, .paused:
break
default:
print("Error uploading: \(uploadRequest.key) Error: \(error)")
}
} else {
print("Error uploading: \(uploadRequest.key) Error: \(error)")
}
return nil
}

let uploadOutput = task.result
print("Upload complete for: \(uploadRequest.key)")
return nil
})

Displaying a Downloaded Image in an UIImageView

self.imageView.image = UIImage(contentsOfFile: downloadingFileURL.path)

 


Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top