对象存储服务 OBS-追加上传

时间:2024-06-18 19:37:27

追加上传

追加上传可实现对同一个对象追加数据内容的功能。您可以通过appendObject进行追加上传。示例代码如下:
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"FileName" ofType:@"FileSuffix"];
    NSFileManager *manager = [NSFileManager defaultManager];
    NSDictionary *fileDic = [manager attributesOfItemAtPath:filePath error:nil];
    unsigned long long size = [[fileDic objectForKey:NSFileSize] longLongValue];
    int filesize = size;
    //第一次追加上传
    OBSAppendObjectWithFileRequest *request = [[OBSAppendObjectWithFileRequest alloc] initWithBucketName:@"bucketName" objectKey:@"objectname" uploadFilePath:filePath];
    request.position = [NSNumber numberWithFloat:0];
    
    request.uploadProgressBlock =  ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
        NSLog(@"%0.1f%%",(float)floor(totalBytesSent*10000/totalBytesExpectedToSend)/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];
        
    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(totalBytesSent*10000/totalBytesExpectedToSend)/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次追加上传。
  • 追加上传成功后,可通过
    NSDictionary *temp = [response headers];NSString* nextPosition = [temp valueForKey:@"x-obs-next-append-position"];
    这种方式获取下次追加上传的位置;或者通过getObjectMetadata接口获取下次追加上传的位置。
support.huaweicloud.com/sdk-ios-devg-obs/obs_27_0408.html