更新时间:2026-08-28 GMT+08:00
分享

追加上传

追加上传可实现对同一个对象追加数据内容的功能。您可以通过appendObject进行追加上传。示例代码如下:
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"FileName" ofType:@"FileSuffix"];
    //第一次追加上传
    OBSAppendObjectWithFileRequest *request = [[OBSAppendObjectWithFileRequest alloc] initWithBucketName:@"bucketname" objectKey:@"objectname" uploadFilePath:filePath];
    request.position = [NSNumber numberWithLongLong:0];
    
    request.uploadProgressBlock =  ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
        NSLog(@"%0.1f%%",(float)floor((totalBytesExpectedToSend > 0) ? (totalBytesSent*10000/totalBytesExpectedToSend) : 0)/100);
        
    };
 
    __block NSString* nextPosition = nil;
    [client appendObject:request completionHandler:^(OBSAppendObjectResponse *response, NSError *error) {
        NSLog(@"%@",response);
        //下次上传位置
        NSDictionary *temp = [response headers];
        nextPosition = [temp valueForKey:@"x-obs-next-append-position"];
        NSLog(@"nextPosition:%@", nextPosition);
    }];
    
    //第二次追加上传
    request = [[OBSAppendObjectWithFileRequest alloc] initWithBucketName:@"bucketname" objectKey:@"objectname" uploadFilePath:filePath];
        
    if (nextPosition == nil) {
        // 首次追加上传未返回x-obs-next-append-position,默认从位置0开始追加
        nextPosition = @"0";
    }
    int nextPositionInt = [nextPosition intValue];
    request.position = [NSNumber numberWithInt:nextPositionInt];
    request.uploadProgressBlock =  ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
        NSLog(@"%0.1f%%",(float)floor((totalBytesExpectedToSend > 0) ? (totalBytesSent*10000/totalBytesExpectedToSend) : 0)/100);
        
    };
    [client appendObject:request completionHandler:^(OBSAppendObjectResponse *response, NSError *error) {
        NSLog(@"%@",response);
        //下次上传位置
        NSDictionary *temp = [response headers];
        nextPosition = [temp valueForKey:@"x-obs-next-append-position"];
        NSLog(@"nextPosition:%@", nextPosition);
    }];
  • putObject上传的对象可覆盖appendObject上传的对象,覆盖后对象变为普通对象,不可再进行追加上传。
  • 第一次调用追加上传时,如果已存在同名的普通对象,则会抛出异常(HTTP状态码为409)。
  • 追加上传返回的ETag是当次追加数据内容的ETag,不是完整对象的ETag。
  • 单次追加上传的内容不能超过5GB,且最多支持10000次追加上传。
  • 追加上传成功后,可通过以下方式获取下次追加上传的位置;或者通过getObjectMetadata接口获取下次追加上传的位置。
    NSDictionary *temp = [response headers];NSString* nextPosition = [temp valueForKey:@"x-obs-next-append-position"];

    当前SDK的文件上传接口(OBSPutObjectWithFileRequest、OBSUploadFileRequest)的uploadFilePath参数仅接受NSString类型的文件路径,不支持直接传入[NSURL fileURLWithPath:@"xxx"]构造的NSURL对象。如需使用NSURL,请先通过[fileURL path]获取文件路径字符串,再传入uploadFilePath参数。

相关文档