{"id":21863,"date":"2015-06-28T18:04:56","date_gmt":"2015-06-28T12:34:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=21863"},"modified":"2015-08-03T11:33:55","modified_gmt":"2015-08-03T06:03:55","slug":"upload-file-on-amazon-s3-using-amazon-sdk","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/upload-file-on-amazon-s3-using-amazon-sdk\/","title":{"rendered":"Upload File on Amazon S3 Using Amazon SDK"},"content":{"rendered":"<p>Amazon Simple Storage Service (Amazon S3), provides <a title=\"AWS Architects and Consultants Team\" href=\"http:\/\/www.tothenew.com\/devops-automation-consulting\">developers and IT teams<\/a> 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.<\/p>\n<p><strong>Requirements:<\/strong><\/p>\n<ul>\n<li>iOS 7 and later<\/li>\n<li>Xcode 5 and later<\/li>\n<li>Version 2 of the AWS SDK for iOS (Download from http:\/\/aws.amazon.com\/mobile\/sdk\/ )<\/li>\n<li>AWS Account<\/li>\n<\/ul>\n<p><strong>Setup :<\/strong><\/p>\n<ol>\n<li>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.<\/li>\n<li>Include following frameworks from AWS SDK third-party directory\n<ul>\n<li>Bolts.framework (If your application uses the Facebook SDK, you won&#8217;t need this framework, as it&#8217;s already included with the Facebook SDK.)<\/li>\n<li>GZIP.framework<\/li>\n<li>Mantle.framework<\/li>\n<li>Reachability.framework<\/li>\n<li>TMCache.framework<\/li>\n<li>UICKeyChainStore.framework<\/li>\n<li>XMLDictionary.framework<\/li>\n<\/ul>\n<\/li>\n<li>Add the following JSON files, located in the service defintions directory, into your project.\n<ul>\n<li>autoscaling-2011-01-01.json<\/li>\n<li>cib-2014-06-30.json<\/li>\n<li>css-2014-06-30.json<\/li>\n<li>dynamodb-2012-08-10.json<\/li>\n<li>ec2-2014-06-15.json<\/li>\n<li>elasticloadbalancing-2012-06-01.json<\/li>\n<li>email-2010-12-01.json<\/li>\n<li>kinesis-2013-12-02.json<\/li>\n<li>mobileanalytics-2014-06-30.json<\/li>\n<li>monitoring-2010-08-01.json<\/li>\n<li>s3-2006-03-01.json<\/li>\n<li>sdb-2009-04-15.json<\/li>\n<li>sns-2010-03-31.json<\/li>\n<li>sqs-2012-11-05.json<\/li>\n<li>sts-2011-06-15.json<\/li>\n<\/ul>\n<\/li>\n<li>Add following Libraries\n<ul>\n<li>libsqlite3.dylib<\/li>\n<li>libz.dylib<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>Create an S3 Bucket<\/strong><\/p>\n<p>Amazon S3 stores your resources in buckets\u2014cloud storage containers that live in a specific region. Each S3 bucket must have a globally unique name.<br \/>\nLet&#8217;s use the AWS Management Console to create an S3 bucket.<\/p>\n<ol>\n<li>Sign in to the Amazon account and create bucket.<\/li>\n<li>Enter a bucket name, select a region, and click create.<\/li>\n<\/ol>\n<p><strong>Configure Credentials :<\/strong><\/p>\n<ul>\n<li>Import the AWSCore header in the application delegate.<\/li>\n<\/ul>\n<p>[code language=&#8221;objc&#8221;]<br \/>\n  #import &lt;AWSiOSSDKv2\/AWSCore.h&gt;<br \/>\n [\/code]<\/p>\n<ul>\n<li>Create a default service configuration by adding the following code in <strong>application: didFinishLaunchingWithOptions: application delegate method:<\/strong><\/li>\n<\/ul>\n<p>[code language=&#8221;objc&#8221;]<br \/>\nAWSStaticCredentialsProvider *credentialsProvider = [AWSStaticCredentialsProvider credentialsWithAccessKey: AWSAccessKey secretKey : AWSSecretKey];<\/p>\n<p>AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionAPSoutheast1 credentialsProvider:credentialsProvider];<\/p>\n<p>[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;<br \/>\n[\/code]<\/p>\n<p><strong>Upload File :<\/strong><\/p>\n<ul>\n<li>Import following header into your viewController file:<\/li>\n<\/ul>\n<p>[code language=&#8221;objc&#8221;]<br \/>\n#import &lt;AWSiOSSDKv2\/S3.h&gt;<br \/>\n[\/code]<\/p>\n<ul>\n<li>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 ):<\/li>\n<\/ul>\n<p>[code language=&#8221;objc&#8221;]<br \/>\nAWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];<br \/>\n[\/code]<\/p>\n<ul>\n<li>For uploading a file you need to create a upload request:<\/li>\n<\/ul>\n<p>[code language=&#8221;objc&#8221;]<br \/>\nNSString *filePath = @&quot;Path of document\u201d ;<br \/>\nAWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];<br \/>\nuploadRequest.bucket = @&quot;Your Bucket Name&quot;;<br \/>\nuploadRequest.key = @&quot;File Name with which you want to save file in S3 bucket&quot;;<br \/>\nuploadRequest.body = [NSURL fileURLWithPath:filePath];<br \/>\n[\/code]<\/p>\n<ul>\n<li>After creating the request, we can now pass it to the upload method of the TransferManager client<\/li>\n<\/ul>\n<p>[code language=&#8221;objc&#8221;]<br \/>\n[[transferManager upload:uploadRequest] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask*task) {<br \/>\n\tif (task.error) {<br \/>\n\t\tNSLog(@&quot;Error: %@&quot;, task.error);<br \/>\n\t}<br \/>\n\telse {<br \/>\n\t\t\/\/ The file uploaded successfully.<br \/>\n\t}  <\/p>\n<p>\treturn nil;<br \/>\n}];<br \/>\n[\/code]<\/p>\n<p>Run your app and check out its working fine. Congratulations! your file has uploaded on Amazon S3 Bucket.<\/p>\n<p>Want to learn about how to download file with progress status from Amazon S3? Keep reading the <a title=\"next part\" href=\"http:\/\/www.tothenew.com\/blog\/aws-s3-file-download-with-progress-status-using-amazon-sdk\/\" target=\"_blank\">next part<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":172,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[1400,1772],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21863"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/172"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=21863"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21863\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=21863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=21863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=21863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}