gpt4 book ai didi

ios - 从 Amazon S3 Bucket iOS 下载多个项目

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:47 25 4
gpt4 key购买 nike

我最近在我的应用程序(耶,cocoapods!)中实现了新的 AWS 2.0 iOS SDK,并使用来自 Amazon 的示例代码设法正确配置访问和下载。我可以毫无问题地成功下载单个项目,但我需要能够下载基于当前 tableview 动态生成的多个文件。似乎没有办法设置批量下载,所以我只是想遍历一组对象并触发每个对象的下载。它有效,但如果列表包含多个项目,它就会开始随机失火。例如,如果我动态创建的列表中有 14 个项目,则将下载 12 个,甚至不会尝试下载其他 2 个。请求就消失了。在我的测试中,我添加了一个 sleep(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;
}];
}

一定有一种方法可以从 Amazon S3 存储桶中下载动态文件列表,对吗?也许有一个传输管理器允许一系列文件而不是单独处理它们?

感谢任何帮助。扎克

最佳答案

听起来像是请求超时间隔设置问题。

首先,当您配置 AWSServiceConfiguration *configuration = ... 时,尝试配置 timeoutIntervalForRequest 属性。此外,maxRetryCount 也是如此。 maxRetryCount 每次下载失败都会尝试下载。

AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:DefaultServiceRegionType
credentialsProvider:credentialsProvider];
[configuration setMaxRetryCount:2]; // 10 is the max
[configuration setTimeoutIntervalForRequest:120]; // 120 seconds

其次,对于多个项目的下载,尝试将每个 AWSTask 收集到一个数组中,并在组操作结束时获取结果。例如)

// 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;
}];

关于ios - 从 Amazon S3 Bucket iOS 下载多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24995537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com