2014-10-27 49 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