我要将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;
}]];
}