Upload File on Amazon S3 Using Amazon SDK

28 / Jun / 2015 by Ashu Baweja 1 comments

Amazon Simple Storage Service (Amazon S3), provides developers and IT teams with secure, durable, highly-scalable object storage. Amazon S3 is easy to use, with a simple web services interface to store and retrieve any amount of data from anywhere on the web.

Requirements:

  • iOS 7 and later
  • Xcode 5 and later
  • Version 2 of the AWS SDK for iOS (Download from http://aws.amazon.com/mobile/sdk/ )
  • AWS Account

Setup :

  1. Add AWSiOSSDKv2.framework from AWS SDK frameworks directory. If you want to use Amazon Cognito Sync, you also need to add the AWSCognitoSync.framework, which is in the frameworks/extras directory.
  2. Include following frameworks from AWS SDK third-party directory
    • Bolts.framework (If your application uses the Facebook SDK, you won’t need this framework, as it’s already included with the Facebook SDK.)
    • GZIP.framework
    • Mantle.framework
    • Reachability.framework
    • TMCache.framework
    • UICKeyChainStore.framework
    • XMLDictionary.framework
  3. Add the following JSON files, located in the service defintions directory, into your project.
    • autoscaling-2011-01-01.json
    • cib-2014-06-30.json
    • css-2014-06-30.json
    • dynamodb-2012-08-10.json
    • ec2-2014-06-15.json
    • elasticloadbalancing-2012-06-01.json
    • email-2010-12-01.json
    • kinesis-2013-12-02.json
    • mobileanalytics-2014-06-30.json
    • monitoring-2010-08-01.json
    • s3-2006-03-01.json
    • sdb-2009-04-15.json
    • sns-2010-03-31.json
    • sqs-2012-11-05.json
    • sts-2011-06-15.json
  4. Add following Libraries
    • libsqlite3.dylib
    • libz.dylib

Create an S3 Bucket

Amazon S3 stores your resources in buckets—cloud storage containers that live in a specific region. Each S3 bucket must have a globally unique name.
Let’s use the AWS Management Console to create an S3 bucket.

  1. Sign in to the Amazon account and create bucket.
  2. Enter a bucket name, select a region, and click create.

Configure Credentials :

  • Import the AWSCore header in the application delegate.
  #import <AWSiOSSDKv2/AWSCore.h>
 
  • Create a default service configuration by adding the following code in application: didFinishLaunchingWithOptions: application delegate method:
AWSStaticCredentialsProvider *credentialsProvider = [AWSStaticCredentialsProvider credentialsWithAccessKey: AWSAccessKey secretKey : AWSSecretKey];

AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionAPSoutheast1 credentialsProvider:credentialsProvider];

[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

Upload File :

  • Import following header into your viewController file:
#import <AWSiOSSDKv2/S3.h>
  • To use the S3 TransferManager, we first need to create a TransferManager client ( AWSS3TransferManager class is our entry point to the high-level S3 API ):
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
  • For uploading a file you need to create a upload request:
NSString *filePath = @"Path of document” ;
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = @"Your Bucket Name";
uploadRequest.key = @"File Name with which you want to save file in S3 bucket";
uploadRequest.body = [NSURL fileURLWithPath:filePath];
  • After creating the request, we can now pass it to the upload method of the TransferManager client
[[transferManager upload:uploadRequest] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask*task) {
	if (task.error) {
		NSLog(@"Error: %@", task.error);
	}
	else {
		// The file uploaded successfully.
	}  

	return nil;
}];

Run your app and check out its working fine. Congratulations! your file has uploaded on Amazon S3 Bucket.

Want to learn about how to download file with progress status from Amazon S3? Keep reading the next part.

Tag -

FOUND THIS USEFUL? SHARE IT

comments (1 “Upload File on Amazon S3 Using Amazon SDK”)

  1. Pingback: File Upload on Amazon S3 server using CURL request : | TO THE NEW Blog

Leave a comment -