You need to enable JavaScript to run this app.
导航

管理对象元信息(iOS SDK)

最近更新时间2024.02.04 18:30:59

首次发布时间2022.11.21 19:11:16

对象元信息是对象的属性描述,包括HTTP标准属性(HTTP Header)和用户自定义元数据(User Meta)两种。本文介绍设置对象元信息的示例代码。

设置自定义元信息

在上传对象时,为对象添加自定义元信息,用于标识对象的用途或属性等。
如下代码展示如何设置对象的自定义元信息。

// 从STS服务获取的临时访问密钥和安全令牌(AccessKey、SecretKey、SecurityToken)
TOSCredential *credential = [[TOSCredential alloc] initWithAccessKey:@"accesskey" secretKey:@"secretkey" securityToken:@"securityToken"];
TOSEndpoint *tosEndpoint = [[TOSEndpoint alloc] initWithURLString:@"endpoint" withRegion:@"region"];
TOSClientConfiguration *config = [[TOSClientConfiguration alloc] initWithEndpoint:tosEndpoint credential:credential];
TOSClient *client = [[TOSClient alloc] initWithConfiguration:config];

TOSSetObjectMetaInput *set = [TOSSetObjectMetaInput new];
set.tosBucket = @"bucket-name";
set.tosKey = @"object-name";
set.tosMeta = @{@"user-key-1":@"use-val-1", @"user-key-2":@"use-val-2"};
TOSTask *task = [_client setObjectMeta:set];
[task continueWithBlock:^id _Nullable(TOSTask * _Nonnull task) {
    if (!task.error) {
        NSLog(@"Set object meta success.");
        TOSSetObjectMetaOutput *output = task.result;
    } else {
        NSLog(@"Set object meta failed, error: %@", task.error);
    }
    return nil;
}];