简体   繁体   English

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

[英]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.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 */
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从iPhone App将文件上传到Amazon S3? - How to upload a file to Amazon S3 from iPhone App? 如何在iPhone / iPad SDK中从Internet下载或打开.docx,.pdf,.zip和视频文件 - How do I download or open .docx,.pdf,.zip and video file from internet in iphone/ipad SDK 如何从iPhone上的应用程序下载sqlite数据库? - How do I download an sqlite database from an app on my iPhone? 如何下载Leopard的iPhone版SDK? - How do I download the SDK for iPhone for Leopard? 如何将CSV文件导入我的iPhone应用程序? - How do I import a CSV file into my iPhone application? 如何让我的程序从iPhone / iPad发送客户的日志文件数据? - How do I have my program send customer's log file data from their iPhone/iPad? 从Titanium中的Amazon S3存储桶下载文件 - Download File from Amazon S3 Bucket in Titanium 如何从CodeIgniter公开ChoneropertyList for IPhone应用程序 - How Do I expose CFPropertyList for IPhone application from CodeIgniter 使用iPhone应用程序从URL下载并播放mp3文件 - Download and play mp3 file from url with iphone application 如何在iPhone模拟器中将图像下载到文档文件中,并通过UIImageView中的路径再次显示它? - how could i download an image into document file in iphone simulator and display it again by it's path in UIImageView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM