I am an iOS developer using Amazon S3 to upload and download the data. I successfully uploaded many image files, but when I am trying to download those all files, I am not able to save data or file on targeted path of S3GetObjectRequest
. I am requesting object through the S3TransferManager
.
Below is my code
- (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];
}
When executing this, it is not saving data on the target path.
- download:
is an asynchronous method and returns immediately. Since you are not retaining a strong reference to an instance ofS3TransferManager
, it can be released before the download completes. You need to keep a strong reference totransferManager
.Please note that you are using a deprecated version of the AWS SDK. It may be worthwhile to migrate to the version 2 of the AWS Mobile SDK for iOS. With the latest version of the SDK, you do not need to worry about the memory retention issue you are encountering.