Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Amazon S3 Bucket iOS下载多个项目_Ios_Amazon Web Services_Amazon S3 - Fatal编程技术网

从Amazon S3 Bucket iOS下载多个项目

从Amazon S3 Bucket iOS下载多个项目,ios,amazon-web-services,amazon-s3,Ios,Amazon Web Services,Amazon S3,我最近在我的应用程序(耶,cocoapods!)中实现了新的AWS 2.0 iOS SDK,并使用来自Amazon的示例代码成功地配置了访问和下载。我可以成功地下载单个项目而不会出现问题,但我需要能够下载基于当前tableview动态生成的多个文件。似乎没有一种方法可以设置批量下载,所以我只是尝试循环遍历一组对象并触发每个对象的下载。它可以工作,但是如果列表中包含的项目多于几个,它就会开始随机缺火。例如,如果我动态创建的列表中有14个项目,将下载12个项目,而其他2个项目甚至不会尝试。请求消失了

我最近在我的应用程序(耶,cocoapods!)中实现了新的AWS 2.0 iOS SDK,并使用来自Amazon的示例代码成功地配置了访问和下载。我可以成功地下载单个项目而不会出现问题,但我需要能够下载基于当前tableview动态生成的多个文件。似乎没有一种方法可以设置批量下载,所以我只是尝试循环遍历一组对象并触发每个对象的下载。它可以工作,但是如果列表中包含的项目多于几个,它就会开始随机缺火。例如,如果我动态创建的列表中有14个项目,将下载12个项目,而其他2个项目甚至不会尝试。请求消失了。在我的测试中,我添加了一个睡眠(1)计时器,然后所有14个都被触发并下载,所以我猜我正在压倒下载请求,除非我放慢速度,否则它们会被删除。放慢速度并不理想。。。也许还有别的办法?代码如下:

 - (IBAction)downloadAllPics:(UIBarButtonItem *)sender {
if (debug==1) {
    NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));
}

CoreDataHelper *cdh =
[(AppDelegate *)[[UIApplication sharedApplication] delegate] cdh];

// for loop iterates through all of the items in the tableview
for (Item *item in self.frc.fetchedObjects) {

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *downloadingFilePath1 = [NSString stringWithFormat:@"%@/%@@2x.jpg",docDir, item.imageName];
    NSURL *downloadingFileURL1 = [NSURL fileURLWithPath:downloadingFilePath1];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    if ([fileManager fileExistsAtPath:downloadingFilePath1]) {
        fileAlreadyExists = TRUE;
        if (![fileManager removeItemAtPath:downloadingFilePath1
                                     error:&error]) {
            NSLog(@"Error: %@", error);
        }
    }
    __weak typeof(self) weakSelf = self;

    self.downloadRequest1 = [AWSS3TransferManagerDownloadRequest new];
    self.downloadRequest1.bucket = S3BucketName;
    //  self.downloadRequest1.key = S3KeyDownloadName1;
    self.downloadRequest1.key = [NSString stringWithFormat:@"images/%@@2x.jpg", item.imageName];
    self.downloadRequest1.downloadingFileURL = downloadingFileURL1;
    self.downloadRequest1.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite){
        // update progress
        dispatch_sync(dispatch_get_main_queue(), ^{
            weakSelf.file1AlreadyDownloaded = totalBytesWritten;
            weakSelf.file1Size = totalBytesExpectedToWrite;
        });

    };

     // this launches the actual S3 transfer manager - it is successfully launched with each pass of loop
    [self downloadFiles];
}

[cdh backgroundSaveContext];

 }
启动downloadFiles方法的:

 - (void) downloadFiles {
//if I add this sleep, all 14 download. If I don't usually 11-13 download.
sleep(1);
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];

__block int downloadCount = 0;

[[transferManager download:self.downloadRequest1] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {
     if (task.error != nil){
        if(task.error.code != AWSS3TransferManagerErrorCancelled && task.error.code != AWSS3TransferManagerErrorPaused){
            NSLog(@"%s Errorx: [%@]",__PRETTY_FUNCTION__, task.error);
        }
    } else {
        self.downloadRequest1 = nil;

    }
    return nil;
}];        
 }
一定有办法从AmazonS3存储桶下载动态文件列表,对吗?也许有一个传输管理器,允许一个文件数组,而不是单独执行它们

非常感谢您的帮助。
Zack听起来像是请求超时间隔设置问题

首先,当您配置
AWSServiceConfiguration*配置=…
时,请尝试配置
timeoutitervalforrequest
属性。另外,
maxRetryCount
也是如此<如果下载每个操作失败,code>maxRetryCount将尝试下载

AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:DefaultServiceRegionType
                                                                     credentialsProvider:credentialsProvider];
[configuration setMaxRetryCount:2]; // 10 is the max
[configuration setTimeoutIntervalForRequest:120]; // 120 seconds
其次,对于多个下载项目,尝试将每个
AWSTask
收集到一个数组中,并在组操作结束时获得结果。ex)


某些请求似乎消失的原因是您将
AWSS3TransferManagerDownloadRequest
定义为属性<代码>self.downloadRequest1=nil在后台线程上执行,当执行
[transferManager下载:self.downloadRequest1]
时,
self.downloadRequest1
可能为
nil


您应该删除该属性,只需将
AWSS3TransferManagerDownloadRequest
的一个实例作为
-downloadFiles:

的参数传递,如果您尝试做同样的事情,希望看到有人回答此问题。。。
// task collector
NSMutableSet *uniqueTasks = [NSMutableSet new];

// Loop
for (0 -> numOfDownloads) {
     AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];
     [downloadRequest setBucket:S3BucketNameForProductImage];
     [downloadRequest setKey:filename];
     [downloadRequest setDownloadingFileURL:sourceURL];
     [showroomGroupDownloadRequests addObject:downloadRequest];

    AWSTask *task = [[AWSS3TransferManager defaultS3TransferManager] download:downloadRequest];
    [task continueWithBlock:^id(AWSTask *task) {
    // handle each individual operation
    if (task.error == nil) {

    }
    else if (task.error) {

    }
    // add to the tasks
    [uniqueTasks addObject:task];

    return nil;
}

[[AWSTask taskForCompletionOfAllTasks:tasks] continueWithBlock:^id(AWSTask *task) {
    if (task.error == nil) {
        // all downloads succeess
    }
    else if (task.error != nil) {
        // failure happen one of download
    }

   return nil;
}];