NSURLSession and amazon S3 uploads

后端 未结 7 1861
傲寒
傲寒 2020-12-12 21:27

I have an app which is currently uploading images to amazon S3. I have been trying to switch it from using NSURLConnection to NSURLSession so that the uploads can continue w

7条回答
  •  被撕碎了的回忆
    2020-12-12 22:01

    Recently Amazon has updated there AWS api to 2.2.4. speciality of this update is that, it supports background uploading, you don't have to use NSURLSession to upload videos its pretty simple, you can use following source block to test it, I have tested against with my older version, it is 30 - 40 % faster than the previous version

    in AppDelegate.m didFinishLaunchingWithOptions method // ~GM~ setup cognito for AWS V2 configurations

    AWSStaticCredentialsProvider *staticProvider = [[AWSStaticCredentialsProvider alloc] initWithAccessKey:@"xxxx secretKey:@"xxxx"];  
    
    AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSWest2                                                                 credentialsProvider:staticProvider];
    
    AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;
    

    in handleEventsForBackgroundURLSession method

    [AWSS3TransferUtility interceptApplication:application
           handleEventsForBackgroundURLSession:identifier
                             completionHandler:completionHandler];
    

    in upload class

    NSURL *fileURL = // The file to upload.
    
    AWSS3TransferUtilityUploadExpression *expression = [AWSS3TransferUtilityUploadExpression new];
    expression.uploadProgress = ^(AWSS3TransferUtilityTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
        dispatch_async(dispatch_get_main_queue(), ^{
            // Do something e.g. Update a progress bar.
        });
    };
    
    AWSS3TransferUtilityUploadCompletionHandlerBlock completionHandler = ^(AWSS3TransferUtilityUploadTask *task, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            // Do something e.g. Alert a user for transfer completion.
            // On failed uploads, `error` contains the error object.
        });
    };
    
    AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility defaultS3TransferUtility];
    [[transferUtility uploadFile:fileURL
                          bucket:@"YourBucketName"
                             key:@"YourObjectKeyName"
                     contentType:@"text/plain"
                      expression:expression
                completionHander:completionHandler] continueWithBlock:^id(AWSTask *task) {
        if (task.error) {
            NSLog(@"Error: %@", task.error);
        }
        if (task.exception) {
            NSLog(@"Exception: %@", task.exception);
        }
        if (task.result) {
            AWSS3TransferUtilityUploadTask *uploadTask = task.result;
            // Do something with uploadTask.
        }
    
        return nil;
    }];
    

    More references: https://aws.amazon.com/blogs/mobile/amazon-s3-transfer-utility-for-ios/

提交回复
热议问题