Amazons3传输实用程序无法在后台工作



我必须将一个类似4gb的大文件上传到amazons3上。amazonsdk提供了从s3上传和下载的两个选项。一个是awss3transfermanager,另一个则是awss3 transferutility。我实际上想使用awss3transferutility,因为我希望应用程序继续在后台上传。awss3transferutility有两个函数uploadFile和其他uploadFileUsingMultiPart。uploadFile功能实际上在后台工作,但在网络更改或删除时从0开始上传。出于这个原因,我经常使用uploadFileUsingMultiPart功能,这样上传就不会在网络失败时从0重新启动。但是这个uploadFileUsingMultiPart函数不会在后台继续上传。在他们的最新版本中,他们在awstransfertility中引入了这个uploadFileUsingMultiPart函数。所以我原以为上传会在网络失败的情况下在后台继续,但不会在后台继续。我只是想问一下是不是与sdk有关的错误,或者我在上做错了什么

这是我目前使用的代码

//应用内代理

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

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

initWithRegionType:AWSRegionUSEast1identityPoolId:@"us-west-1:…

一旦我的代码中出现类似的不匹配,bucket区域和Cognito身份池区域就不匹配,这就造成了问题。你的代码可能是这样,也可能不是这样,但只是根据我的经验添加一些信息。

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

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

最新更新