Control uploads to S3 using BFTask and AWS SDK iOS

2019-08-28 10:19发布

问题:

I am using BFTask together with AWS SDK v2 for iOS to upload and download files to AWS S3 storage. The following code works very well but I am wondering if anyone know how I can gain more control over the maximum number of uploads to allow and also better approach to receiving feedback for upload progress. I have read the documentation for both AWS SDK v2, the source code, and the BFTask readme but I am still uncertain how I gain control. For example, how would I edit the following code to limit the number of BFTasks to be run simultaneously to 3 and also receive number of bytes currently uploaded out of total number of bytes?

-(void) uploadAllFileRequests
{
    AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
    __block int uploadCount = 0;
    __block int uploadSuccess= 0;
    __block int uploadFailure= 0;

    NSMutableArray *tasks = [NSMutableArray new];
    unsigned long totalnumberoffiles=self.arrayOfUploadRequests.count;

    for (__block AWSS3TransferManagerUploadRequest *uploadRequestLocal in self.arrayOfUploadRequests)
    {

        [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);
                    uploadFailure ++;
                }
            } else {
                uploadCount ++;
                uploadSuccess ++;
                NSLog(@"ETag: %@           %@ : %3.2f",[task.result valueForKey:@"ETag"], StatusLabelUploading, (uploadCount*1.0/totalnumberoffiles)*100.);
            }
            return nil;     
        }]];
    }
    [[BFTask taskForCompletionOfAllTasks:tasks] continueWithSuccessBlock:^id(BFTask *task)
     {
         NSLog(@"Finished:  Success: %i - Failed: %i  -",uploadSuccess,uploadFailure);
         return nil;
     }];
}

Here self.arrayOfUploadRequests is an array containing AWSS3TransferManagerUploadRequest. Suggestions are greatly appreciated. Thanks!

回答1:

AWSRequest, a superclass of AWSS3TransferManagerUploadRequest, has properties called uploadProgress and downloadProgress. You can implement AWSNetworkingUploadProgressBlock and AWSNetworkingDownloadProgressBlock to retrieve the progress feedback.

One way to limit the number of concurrent uploads to three is to sequentially execute three tasks in parallel. You can combine In sequence and In parallel sections of this blog post.