[英]How do I download a file from S3 to an iPhone application?
我是iPhone开发的新手。 我正在开发一个需要打开存储在亚马逊S3服务上的文件的iPhone应用程序。
如何从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.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.