AWS SDK for iOSでS3にファイルのアップロード/ダウンロード
暫定版。
アップロード
- (void)uploadFile:(NSString*)filePath key:(NSString*)key { AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:@"YOUR-ACCESS-KEY" withSecretKey:@"YOUR-SECRET-KEY"]; S3PutObjectRequest *req = [[S3PutObjectRequestalloc] initWithKey:key inBucket:@"name.of.your.bucket"]; req.filename = filePath; S3Response *res = [s3 putObject:req]; [req release]; [s3 release]; }
ダウンロード
- (void)downloadObjectWithKey:(NSString*)key toFile:(NSString*)filePath { AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:@"YOUR-ACCESS-KEY" withSecretKey:@"YOUR-SECRET-KEY"]; S3GetObjectRequest *req = [[S3GetObjectRequest alloc] initWithKey:key withBucket:@"name.of.your.bucket"]; NSOutputStream *out = [[NSOutputStream alloc] initToFileAtPath:filePath append:NO]; req.outputStream = out; [out open]; @try { S3GetObjectResponse *res = [s3 getObject:req]; if ([res httpStatusCode] == 200) { // ... } else { // ... } } @catch (NSException *exception) { } @finally { [out close]; [out release]; [req release]; [s3 release]; } }