融合接口

SDK提供封装好的融合接口,方便用户实现分片上传的功能。

接口定义:

- (AWSTask<AWSS3TransferUtilityUploadTask *> *)uploadData:(NSData *)data
                                                      key:(NSString *)key
                                              contentType:(NSString *)contentType
                                               expression:(nullable AWSS3TransferUtilityUploadExpression *)expression
                                        completionHandler:(nullable AWSS3TransferUtilityUploadCompletionHandlerBlock)completionHandler;

- (AWSTask<AWSS3TransferUtilityUploadTask *> *)uploadData:(NSData *)data
                                                   bucket:(NSString *)bucket
                                                      key:(NSString *)key
                                              contentType:(NSString *)contentType
                                               expression:(nullable AWSS3TransferUtilityUploadExpression *)expression
                                        completionHandler:(nullable AWSS3TransferUtilityUploadCompletionHandlerBlock)completionHandler;

参数:

参数名 类型 说明
bucket NSString bucket名
key NSString 要上传的对象名称
data NSData 文件内容
contentType NSString 文件类型
expression AWSS3TransferUtilityUploadExpression 其他参数,设置progress回调,设置acl
completionHandler AWSS3TransferUtilityUploadCompletionHandlerBlock 上传文件结果回调

代码示例:

- (IBAction)start:(id)sender {
    __weak UploadViewController *weakSelf = self;
    
    NSString *fileName = @"test40M.mp4";
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
    NSData *fileData = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:nil];
    
    AWSS3TransferUtilityUploadExpression * expression = [AWSS3TransferUtilityUploadExpression new];
    //设置公共读
    //[expression setValue:@"public-read" forRequestHeader:@"x-amz-acl"];
    expression.progressBlock = ^(AWSS3TransferUtilityTask * _Nonnull task, NSProgress * _Nonnull progress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            weakSelf.progressView.progress = progress.fractionCompleted;
        });
    };
    
    AWSS3TransferUtilityUploadCompletionHandlerBlock completionHandler = ^(AWSS3TransferUtilityUploadTask *task, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if(error) {
                weakSelf.statusLabel.text = @"Failed to Upload";
            } else {
                weakSelf.statusLabel.text = @"Successfully Uploaded";
            }
        });
    };
 
    AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility defaultS3TransferUtility];
    [[transferUtility uploadData:fileData
                          bucket:self.mBucketName
                             key:fileName
                     contentType:@"video/mp4"
                      expression:expression
               completionHandler:completionHandler] continueWithBlock:^id(AWSTask *task) {
        if (task.error) {
            NSLog(@"Error: %@", task.error);
            return nil;
        }
        
        if (task.result) {
            NSLog(@"Upload Starting!");
        }
        return nil;
    }];
}