0

我是一名 iOS 开发人员,使用 Amazon S3 上传和下载数据。我成功上传了许多图像文件,但是当我尝试下载所有文件时,我无法将数据或文件保存在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]; 
}

执行此操作时,它不会在目标路径上保存数据。

4

1 回答 1

1

- download:是一个异步方法并立即返回。由于您没有保留对 的实例的强引用S3TransferManager,因此可以在下载完成之前将其释放。您需要对transferManager.

请注意,您使用的是已弃用的 AWS 开发工具包版本。迁移到适用于 iOS 的 AWS 移动开发工具包的版本 2 可能是值得的。使用最新版本的 SDK,您无需担心遇到的内存保留问题。

于 2015-06-18T18:40:03.547 回答