如何从S3下载文件到iPhone应用程序?

我是iPhone开发新手。 我正在开发一个iPhone应用程序,需要打开存储在亚马逊的S3服务上的文件。

如何从S3下载文件到我的iPhone应用程序? 我已经尝试了亚马逊的SDK,但他们似乎没有提供下载和保存文件的方式。 我如何从S3获取文件的URL并将其保存在我的应用程序中?

我总是使用ASIHttpRequest库来实现这一点,这很简单,下面是他们网站的示例代码:

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]); } 

你不能比这更容易:)

如果你没有使用ASI进行简单的奢侈,并且/或者被AWSiOS SDK困住了,那么它就没有什么不同了:

 /* borrowed from Maurício Linhares's 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]; 

那么你可以看看downloadResponse.bodydownloadResponse.httpStatusCode ,最好在一个委托中:

 -(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 */ }