How to download a file from S3 to an iPhone application?

I am new to iPhone development. I am developing an iPhone application that needs to open files stored in Amazon S3.

How to upload a file from S3 to my iPhone application? I tried the Amazon SDK, but they don't seem to provide a means to download and save the file. How can I get the file url from S3 and save it in my application?

+4
source share
2 answers

I always use the ASIHttpRequest library to do this, and it's pretty simple, here is a sample code from my site:

NSString *secretAccessKey = @"my-secret-access-key"; NSString *accessKey = @"my-access-key"; NSString *bucket = @"my-bucket"; NSString *path = @"path/to/the/object"; ASIS3ObjectRequest *request = [ASIS3ObjectRequest requestWithBucket:bucket key:path]; [request setSecretAccessKey:secretAccessKey]; [request setAccessKey:accessKey]; [request startSynchronous]; if (![request error]) { NSData *data = [request responseData]; } else { NSLog(@"%@",[[request error] localizedDescription]); } 

It couldn't be easier for you :)

+3
source

If you do not have the luxury of simplicity using ASI and / or are stuck with the AWSiOS SDK, this is not so much:

 /* borrowed from Maurício Linhares answer */ NSString *secretAccessKey = @"my-secret-access-key"; NSString *accessKey = @"my-access-key"; NSString *bucket = @"my-bucket"; NSString *path = @"path/to/the/object"; /********************************************/ AmazonCredentials *credentials = [[AmazonCredentials alloc] initWithAccessKey: accessKey withSecretKey: secretAccessKey]; AmazonS3Client *connection = [[AmazonS3Client alloc] initWithCredentials: credentials]; S3GetObjectRequest *downloadRequest = [[[S3GetObjectRequest alloc] initWithKey:path withBucket: bucket] autorelease]; [downloadRequest setDelegate: self]; /* only needed for delegate (see below) */ S3GetObjectResponse *downloadResponse = [self.awsConnection getObject: downloadRequest]; 

Then you can see downloadResponse.body and downloadResponse.httpStatusCode , preferably in the delegate:

 -(void)request: (S3Request *)request didCompleteWithResponse: (S3Response *) response { NSLog(@"Download finished (%d)",response.httpStatusCode); /* do something with response.body and response.httpStatusCode */ /* if you have multiple requests, you can check request arg */ } 
+3
source

All Articles