2014-10-27 91 views
-1

我将15张照片上传到AWS S3(v2),并且我想显示每张照片的进度。使用适用于iOS的AWS S3 SDK v2上传图像:跟踪进度

首先,我为每张照片创建了一个AWSS3TransferManagerUploadRequest。

AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new]; 
// load uploadRequest atts... 
uploadRequest.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) { 
    int progress = (int)(totalBytesSent * 100.0/totalBytesExpectedToSend); 
    DDLogInfo(@"%d", progress); 
} 

然后创建了一个的NSArray BFTask的

BFTask *task = [self.s3transferManager upload:uploadRequest]; 
[tasks addObject:task]; 

最后:

[[BFTask taskForCompletionOfAllTasks:tasks] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) { 

    if (task.error != nil) {   
     DDLogError(@"Error: [%@]", task.error); 
    } else { 
     DDLogInfo(@"Complete!"); 
    }   
    return nil;   
}]; 

我的问题是,随着 “上传进度” 相关联的块执行用于第一4倍的照片和那么其余的只是上传,但没有跟踪进度。

有什么想法?

谢谢!

+0

这似乎是一个错误:HTTPS ://github.com/aws/aws-sdk-ios/issues/91#issuecomment-69859140 – dms90 2015-01-14 17:14:31

回答

1

我不确定这会对您有所帮助,但我附上自己的代码,了解如何使用AWS SDK v2显示上传到AWS S3的进度。在我的情况下,我显示了所有文件的总进度。我注意到你似乎没有在主线程上更新你的进度,这可能是你的代码中的错误。希望这可以帮助。

for (NSURL *myurl in self.myenum){ 
     AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new]; 
     uploadRequest.bucket = @"yourbucketname"; 
     uploadRequest.body = myurl;  
     self.fileSize = @([[[NSFileManager defaultManager] attributesOfItemAtPath:myurl.path error:nil][NSFileSize] unsignedLongLongValue]); 

     self.expectedToUpload = @(self.expectedToUpload.unsignedLongLongValue + fileSize.unsignedLongLongValue); 
     __weak typeof(self) weakSelf = self; 

     uploadRequest.contentLength = self.fileSize; 
     uploadRequest.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend){ 
     dispatch_sync(dispatch_get_main_queue(), ^{ 
       @synchronized(weakSelf){ 
         // NSLog(@"%@ => %llu %llu",myurl,totalBytesSent, totalBytesExpectedToSend); 
         // NSLog(@"Progress => %f",(weakSelf.fileAlreadyUpload.unsignedLongLongValue*1.0/weakSelf.expectedToUpload.unsignedLongLongValue)*100); 
        weakSelf.fileAlreadyUpload = @(weakSelf.fileAlreadyUpload.unsignedLongLongValue + bytesSent); 
        weakSelf.myprogressbarController.progressbarView.progress = [NSNumber numberWithUnsignedLongLong: (weakSelf.fileAlreadyUpload.unsignedLongLongValue/weakSelf.expectedToUpload.unsignedLongLongValue)*100]; 
       } 
       self.myprogressbarController.progressbarView.progress = [NSNumber numberWithFloat: myprogress]; 
       [self.myprogressbarController.progressbarView setNeedsDisplay:YES]: 
      }); 

我使用uploadcontroller上传的文件:

-(void) uploadController{ 

    __weak typeof(self) weakSelf = self; 

    NSMutableArray *tasks = [NSMutableArray new]; 
    AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager]; 
    for (AWSS3TransferManagerUploadRequest *uploadRequestLocal in self.arrayOfUploadRequests objectAtIndex){ 
     [tasks addObject:[[transferManager upload:uploadRequestLocal] continueWithBlock:^id(BFTask *task) { 
      if (task.error != nil) { 
       if(task.error.code != AWSS3TransferManagerErrorCancelled 
        && 
        task.error.code != AWSS3TransferManagerErrorPaused 
        ) 
       { 
        NSLog(@"ERROR: %@",StatusLabelFailed); 
        weakSelf.uploadFailure = @([weakSelf.uploadFailure intValue] + 1); 
       } 
      } else { 
       weakSelf.uploadCount = @([weakSelf.uploadCount intValue] + 1); 
       weakSelf.uploadSuccess = @([weakSelf.uploadSuccess intValue] + 1); 

      } 
      return nil; 

     }]]; 
    } 
+0

谢谢!我添加了要在主线程上执行的代码,但没有解决问题。你可以在你上传的地方分享代码吗?也许问题在那里。你在使用并行还是序列上传? – dms90 2014-11-09 21:21:38

1

我在序列和并行上传组合以解决它。我认为当您并行上传4个以上的任务时,新版本的SDK会有一个错误(跟踪进度),所以我将这些照片以4个小组的形式上传并按顺序执行任务。

上传照片1,2,3,4然后上传照片5,6,7,8,然后...

我报GitHub上的问题:https://github.com/aws/aws-sdk-ios/issues/91