ios - S3TransferManager 无法在 ios 中的 S3GetObjectRequest targetedFilePath 上保存文件/数据

标签 ios amazon-web-services amazon-s3 s3transfermanager

我是一名 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]; 
}

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

最佳答案

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

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

关于ios - S3TransferManager 无法在 ios 中的 S3GetObjectRequest targetedFilePath 上保存文件/数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30909554/

相关文章:

node.js - AWS SAM 本地和环境参数

java - AWS 中的可用区选择

amazon-web-services - Amazon Free Tier中的存储桶数

objective-c - MKMapView 的暗淡部分

python - 收到 celery 任务但未执行

amazon-web-services - 让cloudformation等待,直到在s3中创建一个对象

python-3.x - PySpark 使用临时 AWS token 进行 s3 身份验证的问题

objective-c - h.file 中的属性定义

iOS 扩展 : Is it necessary to increment its bundle version (CFBundleVersion)?

android - 关于selendroid和appium的几个问题