我是使用Amazon S3上传和下载数据的iOS开发人员。我成功上传了许多图片文件,但是当我尝试下载所有文件时,我无法在S3GetObjectRequest
的目标路径上保存数据或文件。我通过S3TransferManager
请求对象。
以下是我的代码
- (void)listObjects:(id)sender {
self.collection = [[NSMutableArray alloc] init];
s3Client = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
@try {
S3ListObjectsRequest *req = [[S3ListObjectsRequest alloc] initWithName:AMAZON_BUCKET_NAME];
req.prefix = @"arvinds/8/";
//req.prefix = [NSString stringWithFormat:@"%@", self.appUser.userEmail];
S3ListObjectsResponse *resp = [s3Client listObjects:req];
NSMutableArray* objectSummaries = resp.listObjectsResult.objectSummaries;
for (int x = 0; x < [objectSummaries count]; x++) {
NSLog(@"objectSummaries: %@",[objectSummaries objectAtIndex:x]);
S3ObjectSummary *s3Object = [objectSummaries objectAtIndex:x];
NSString *downloadingFilePath = [[NSTemporaryDirectory() stringByAppendingPathComponent:@"download"] stringByAppendingPathComponent:s3Object.key];
NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];
NSLog(@"downloadingFilePath--- %@",downloadingFilePath);
if ([[NSFileManager defaultManager] fileExistsAtPath:downloadingFilePath]) {
[self.collection addObject:downloadingFileURL];
} else {
[self.collection addObject:downloadingFileURL];
S3GetObjectRequest *getObj = [[S3GetObjectRequest new] initWithKey:s3Object.key withBucket:AMAZON_BUCKET_NAME];
getObj.targetFilePath = downloadingFilePath;
getObj.delegate = self;
[self download:getObj];// Start downloding image and write in default folder
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.GalleryCollectionView reloadData];
//[self downloadAllRequestObject];
});
}
@catch (NSException *exception) {
NSLog(@"Cannot list S3 %@",exception);
}
}
// Above code is for access all the objects stored on Amazon S3 server.
// Below code is for S3TransferManager request
- (void)download:(S3GetObjectRequest *)downloadRequest {
S3TransferManager *transferManager = [S3TransferManager new];
transferManager.s3 = s3Client;
transferManager.delegate = self;
[transferManager download:downloadRequest];
}
执行此操作时,不会在目标路径上保存数据。
答案 0 :(得分:1)
- download:
是一个异步方法并立即返回。由于您没有保留对S3TransferManager
实例的强引用,因此可以在下载完成之前发布它。您需要对transferManager
保持强烈的引用。
请注意,您使用的是不推荐使用的AWS SDK版本。迁移到适用于iOS的AWS Mobile SDK版本2可能是值得的。使用最新版本的SDK,您无需担心遇到的内存保留问题。