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

简单上传(iOS SDK)

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

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

通过此接口,您可以上传对象到指定的桶中,上传的数据内容包括数据和本地文件。

上传数据

如下代码展示如何上传数据到目标桶中。

// 从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];

TOSPutObjectInput *put = [TOSPutObjectInput new];
put.tosBucket = @"bucket-name";
put.tosKey = @"object-name";
put.tosMeta = @{@"usermeta-key-1" : @"usermeta-value-1", @"usermeta-key-2" : @"usermeta-value-2"};
put.tosContent = your_data; // NSData
TOSTask *task = [client putObject:put];
[task continueWithBlock:^id(TOSTask *task) {
    if (!task.error) {
        NSLog(@"Put object success.");
        TOSPutObjectOutput *output = task.result;
    } else {
        NSLog(@"Put object failed, error: %@" ,task.error);
    }
    return nil;
}];

上传本地文件

如下代码展示如何将本地文件上传到目标桶中。

TOSCredential *credential = [[TOSCredential alloc] initWithAccessKey:@"accesskey" secretKey:@"secretkey"];
TOSEndpoint *tosEndpoint = [[TOSEndpoint alloc] initWithURLString:@"endpoint" withRegion:@"region"];
TOSClientConfiguration *config = [[TOSClientConfiguration alloc] initWithEndpoint:tosEndpoint credential:credential];
TOSClient *client = [[TOSClient alloc] initWithConfiguration:config];

TOSPutObjectFromFileInput *put = [TOSPutObjectFromFileInput new];
put.tosBucket = @"bucket-name";
put.tosKey = @"object-name";
put.tosMeta = @{@"usermeta-key-1" : @"usermeta-value-1", @"usermeta-key-2" : @"usermeta-value-2"};
put.tosFilePath = @"your-file-path";
TOSTask *task = [client putObjectFromFile:put];
[task continueWithBlock:^id(TOSTask *task) {
    if (!task.error) {
        NSLog(@"Put object from file success.");
        TOSPutObjectFromFileOutput *output = task.result;
    } else {
        NSLog(@"Put object from file failed, error: %@" ,task.error);
    }
    return nil;
}];