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

发布于 2024-11-11 02:37:21 字数 347 浏览 3 评论 0原文

我有一个 Amazon S3 账户并已下载 AWSiOSSDK 框架/SDK 包。该示例运行良好。我可以“列出存储桶”并上传示例数据。

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

我认为确定执行此操作的方法/类很容易,但我正在努力。我已经浏览了文档,但它是压倒性的(对于这样的“直接”要求)。我也找不到任何在线教程。我尝试删除相关代码&亚马逊示例中的框架,但在尝试了几个小时后停止了。

有谁向我指出一个教程(或提供示例代码),展示如何在 XCode 中使用 Amazon SDK?这是我第一次在 iPhone 上尝试在线备份,所以如果这看起来很简单,请原谅。

感谢您的帮助。

I have an Amazon S3 account and have downloaded the AWSiOSSDK framework/SDK pack. The example works fine. I can "List Buckets" and upload sample data.

I want to use S3 to backup user data and then provide the opportunity to download the same backup to restore the data as and when needed.

I thought it would be easy to identify the methods/classes to do this, but I am struggling. I have looked through the documentation but it is overwhelming (for such a "straight forward" requirement). I cannot find any online tutorials either. I tried stripping out the relevant code & frameworks from Amazon's example, but stopped after hours of trying to get anything to work.

Does anyone point me to a tutorial (or provide sample code) showing how to use the Amazon SDK in XCode? This is the first time I have tried online backup on iPhone, so please excuse if this seems simplistic.

Appreciate your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖兔^ω^ 2024-11-18 02:37:21

下载:

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

To download:

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

response.body is an NSData with the the download

To upload, S3PutObjectRequest works much the same way. You can specify either a filename or 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];
丢了幸福的猪 2024-11-18 02:37:21

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

header

#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

main

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?");

Thanks David. Really appreciated. A couple of issues though. I copied in AWSiOSSDK.framework, but I presume I have to import it into the header? When I add #import AWSiOSSDK/S3/AmazonS3Client.h it seems to prevent any NSLog messages. Very odd. Needless to say my upload is not working, but I cannot seem to debug. Here is the code:

header

#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

main

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?");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文