使用AWS S3 SDK v2 for iOS上传图像:跟踪进度

时间:2014-10-27 17:30:18

标签: ios objective-c amazon-web-services amazon-s3

我正在将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张照片执行,然后其余的只会上传但不会跟踪进度。

有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分: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;

        }]];
    }

答案 1 :(得分:1)

我按顺序和并行上传组合以修复它。我认为当您上传4个以上的任务时,SDK的新版本存在一个错误(跟踪进度),所以我将这些照片分组上传并按顺序执行任务。

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

我在GitHub上报告了一个问题:https://github.com/aws/aws-sdk-ios/issues/91