使用Amazon S3 SDK备份和恢复iPhone数据

时间:2011-05-29 00:42:06

标签: iphone amazon-s3 backup restore

我有一个Amazon S3帐户并已下载AWSiOSSDK框架/ SDK包。这个例子运行正常。我可以“列出存储桶”并上传示例数据。

我想使用S3备份用户数据,然后提供下载相同备份的机会,以便在需要时恢复数据。

我认为识别这样做的方法/类很容易,但我很挣扎。我查看了文档,但它是压倒性的(对于这样一个“直接”的要求)。我也找不到任何在线教程。我试图删除相关代码&来自亚马逊的例子中的框架,但在数小时试图让任何工作开始后停止了。

是否有人指向我展示如何在XCode中使用Amazon SDK的教程(或提供示例代码)?这是我第一次尝试在iPhone上进行在线备份,所以请原谅这是否过于简单。

感谢您的帮助。

2 个答案:

答案 0 :(得分:5)

下载:

s3Client = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY withSecretKey:SECRET_KEY];              
S3GetObjectRequest *request = [[S3GetObjectRequest alloc] initWithKey:@"YOURKEY" withBucket:@"YOURBUCKET"];
S3GetObjectResponse *response = [s3Client getObject:request];

response.body是带有下载的NSData

要上传,S3PutObjectRequest的工作方式大致相同。您可以指定文件名或NSData。

s3Client = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY withSecretKey:SECRET_KEY];              
S3PutObjectRequest *request = [[S3PutObjectRequest alloc] initWithKey:@"YOURKEY" withBucket:@"YOURBUCKET"];
request.data = ...
S3PutObjectResponse *response = [s3Client putObject:request];

答案 1 :(得分:0)

谢谢大卫。非常感谢。但有几个问题。我复制了AWSiOSSDK.framework,但我认为我必须将它导入标题?当我添加#import AWSiOSSDK / S3 / AmazonS3Client.h时,似乎可以阻止任何NSLog消息。很奇怪。不用说我的上传不起作用,但我似乎无法调试。这是代码:

#import UIKit/UIKit.h
#import AWSiOSSDK/S3/AmazonS3Client.h

#define ACCESS_KEY                   @""
#define SECRET_KEY                   @""

@interface S3_Try2ViewController : UIViewController {

AmazonS3Client *s3Client;

S3PutObjectRequest *putObjectRequest;
S3PutObjectResponse *putObjectResponse;

S3GetObjectRequest *getObjectRequest;
S3GetObjectResponse *getObjectResponse;

}

-(IBAction) S3Upload;
-(IBAction) S3Download;


 @end

NSLog(@"Start upload");

NSString *bucketName = @"bucketName";
NSString *keyName    = @"asyncTestFile";
NSString *filename   = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"txt"];

NSLog(@"filename %@",filename);

s3Client = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY withSecretKey:SECRET_KEY];
putObjectRequest = [[S3PutObjectRequest alloc] initWithKey:keyName inBucket:bucketName];

NSLog(@"alloc finished");

putObjectRequest.filename = filename;

NSLog(@"Uploading");

putObjectResponse = [s3Client putObject:putObjectRequest];

NSLog(@"Finished?");