ios - 使用适用于 iOS 的 AWS S3 SDK v2 上传图像 : Track progress

标签 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”关联的 block 对前 4 张照片执行,然后其余的只是上传但不跟踪进度。

有什么想法吗?

谢谢!

最佳答案

我不确定这是否对您有帮助,但我附上了我的代码,用于说明如何显示使用 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]:
            });

我使用上传 Controller 上传文件:

-(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;

        }]];
    }

关于ios - 使用适用于 iOS 的 AWS S3 SDK v2 上传图像 : Track progress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26593316/

相关文章:

amazon-web-services - Boto3 在 AWS CodeBuild 中运行时使用什么凭证?

java - 通过 Flink REST API 在 AWS EMR 上运行 Flink 作业

iOS UITableViewCell setSelected :animated: always has animated = NO

iphone - 如何在系统级别捕获和记录 iOS 触摸事件?

ios - 即使导入了 UIKit,也使用未声明的类型 'UITableViewSection'

ios - 如何在 Swift 中从 UserDefaults 中检索字符串?

ios - 在哪里放置 UITableViewCell 逻辑?

sql - 有没有办法从 SQL 查询 AWS RDS Oracle DB 标识符?

ios - 如何在 CNContact 中快速查看联系人来源?

ios - 重启后 iOS 模拟器上的 BootStrap 错误仍然存​​在