[英]How do I download a file from S3 to an iPhone application?
I am new to iPhone development. 我是iPhone开发的新手。 I am developing an iPhone application which needs to open files stored on Amazon's S3 service. 我正在开发一个需要打开存储在亚马逊S3服务上的文件的iPhone应用程序。
How do I download a file from S3 to my iPhone application? 如何从S3下载文件到我的iPhone应用程序? I have tried Amazon's SDK, but they don't seem to provide a means of downloading and saving a file. 我尝试过亚马逊的SDK,但它们似乎没有提供下载和保存文件的方法。 How do I go about obtaining a file's URL from S3 and saving it in my application? 如何从S3获取文件的URL并将其保存在我的应用程序中?
I always use the ASIHttpRequest library to do this and it's quite simple, here's a sample code from their website: 我总是使用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]);
}
You can't get easier than this :) 你不能比这更容易:)
If you don't have the luxury of simplicity using ASI, and/or are stuck with AWSiOS SDK, it's not a lot different: 如果您没有使用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];
Then you can look at downloadResponse.body
and downloadResponse.httpStatusCode
, preferable in a delegate: 然后你可以看一下downloadResponse.body
和downloadResponse.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 */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.