簡體   English   中英

使用適用於iOS的AWS S3 SDK v2上傳圖像:跟蹤進度

[英]Upload images using AWS S3 SDK v2 for iOS: Track progress

我要將15張照片上傳到AWS S3(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);
}

然后我創建了一個BFTask的NSArray

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

我遇到的問題是,與“ uploadProgress”相關聯的塊將針對前4張照片執行,然后其余照片僅上載但不跟蹤進度。

任何想法?

謝謝!

我不確定這是否對您有幫助,但是我附上我的代碼,以顯示如何使用AWS開發工具包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;

        }]];
    }

為了修復它,我按順序和並行上傳進行了合並。 我認為新版SDK會在您並行上傳4個以上任務時出現錯誤(跟蹤進度),因此我以4組為單位上傳照片並按順序執行任務。

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

我在GitHub上報告了一個問題: https : //github.com/aws/aws-sdk-ios/issues/91

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM