Amazon S3传输实用程序在后台无法运行

时间:2019-01-01 10:06:00

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

我必须将大于4GB的大文件上传到亚马逊s3上。亚马逊sdk提供了两个从s3上载和下载的选项。一个是aws3transfermanager,另一个是aws3transferutility。我实际上想使用awss3transferutility,因为我希望该应用程序继续在后台上传。 awss3transferutility具有两个功能uploadFile和其他uploadFileUsingMultiPart。 uploadFile函数实际上在后台运行,但是在网络更改或删除时,上传从0开始。因此,我现在正在使用uploadFileUsingMultiPart函数,这样在网络上上传不会从0重新开始。但是此uploadFileUsingMultiPart函数将不会继续在后台上传。在最新版本中,他们在awstransferutility中引入了这个uploadFileUsingMultiPart函数。因此,我期望上载将在网络失败的情况下继续在后台进行,但不会在后台继续进行。我只是想问是与sdk相关的bug还是我做错了什么

这是我目前的代码

//在应用程序委托中

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler {
    [AWSS3TransferUtility interceptApplication:application
           handleEventsForBackgroundURLSession:identifier
                             completionHandler:completionHandler];
}

//在ViewController中

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged:)
                                                 name:kReachabilityChangedNotification
                                               object:nil];

    __weak typeof(self) weakSelf = self;
    expression = [AWSS3TransferUtilityMultiPartUploadExpression new];
    expression.progressBlock = ^(AWSS3TransferUtilityMultiPartUploadTask *  task, NSProgress *  progress) {
       typeof(self) newWeakSelf = weakSelf;

       dispatch_async(dispatch_get_main_queue(), ^{
           // Do something e.g. Alert a user for transfer completion.

           NSLog(@"progress value %f",progress.fractionCompleted);
           // On failed uploads, `error` contains the error object.
           newWeakSelf->progressView.progress = progress.fractionCompleted;
       });
   };

   completionHandler = ^(AWSS3TransferUtilityMultiPartUploadTask *task, NSError *error) {
       dispatch_async(dispatch_get_main_queue(), ^{
           NSLog(@"uploading completed ");
       });
   };


   AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:@"us-west-1:7a24b199-e4b2-4657-9627-sdfs4ssdff"];
   AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];
   // AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;

   AWSS3TransferUtilityConfiguration *tfConfig = [AWSS3TransferUtilityConfiguration new];
   tfConfig.retryLimit = 5;
   tfConfig.multiPartConcurrencyLimit = [NSNumber numberWithInteger:3];
   [AWSS3TransferUtility registerS3TransferUtilityWithConfiguration:configuration transferUtilityConfiguration:tfConfig forKey:@"transfer-utility-with-advanced-options"];

   transferUtility = [AWSS3TransferUtility S3TransferUtilityForKey:@"transfer-utility-with-advanced-options"];
}

-(void)startUploading {
    NSString *filePath =   [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
    NSURL *fileURL =  [NSURL fileURLWithPath:filePath];
    NSString *fileContentTypeStr = @"video/mp4";

    //  NSData *data = [NSData dataWithContentsOfURL:fileURL];
    //  AWSTask *task = [transferUtility uploadDataUsingMultiPart:data bucket:@"sibme-development" key:@"temp/testfilenew/testfile1.mp4" contentType:fileContentTypeStr expression:expression completionHandler:completionHandler ];

    AWSTask *task = [transferUtility uploadFileUsingMultiPart:fileURL bucket:@"development" key:@"temp/testfilenew/testfile.mp4" contentType:fileContentTypeStr expression:expression completionHandler:completionHandler];

    [task continueWithBlock:^id _Nullable(AWSTask * _Nonnull t) {
        if (t.result) {
          self->uplaodTask = t.result;
        }
        return nil;
    }];
}

2 个答案:

答案 0 :(得分:0)

我不是这方面的专家,但是我注意到您的代码中有一些东西-桶区域:

initWithRegionType:AWSRegion USEast1 identityPoolId:@ “美国西部-1 :...

一旦我的代码中出现类似的不匹配情况,存储桶区域和Cognito身份池区域将不匹配,这会引起问题。在您的代码中可能会出现这种情况,也可能不是,只是根据我的经验添加一些信息。

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:@"us-west-1:7a24b199-e4b2-4657-9627-sdfs4ssdff"];
   AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];

答案 1 :(得分:0)

如果您认为SDK有问题,可以尝试使用最新版本的SDK,如果仍然遇到问题,请通过一些复制步骤在https://github.com/aws-amplify/aws-sdk-ios中打开问题。