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

TOSGetObjectInput *get = [TOSGetObjectInput new];
get.tosBucket = @"bucket-name";
get.tosKey = @"object-name";
TOSTask *task = [client getObject:get];
[task continueWithBlock:^id(TOSTask *task) {
    if (!task.error) {
        NSLog(@"Get object success.");
        TOSGetObjectOutput *output = task.result;
        NSData *date = output.tosContent;
    } else {
        NSLog(@"Get object failed, error: %@" ,task.error);
    }
    return nil;
}];

下载对象指定范围的数据

如果您仅需要读取对象的某个范围数据,可以通过设置 Range 范围读取对象指定范围的数据。如下代码展示如何下载对象指定 Range 范围的数据。

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


TOSGetObjectInput *get = [TOSGetObjectInput new];
get.tosBucket = @"bucket-name";
get.tosKey = @"object-name";
get.tosRangeStart s= @1;
get.tosRangeEnd = @100;
TOSTask *task = [client getObject:get];
[task continueWithBlock:^id(TOSTask *task) {
    if (!task.error) {
        NSLog(@"Get object success.");
        TOSGetObjectOutput *output = task.result;
        NSData *date = output.tosContent;
    } else {
        NSLog(@"Get object failed, error: %@" ,task.error);
    }
    return nil;
}];

下载对象到文件

如下代码展示如何下载一个对象并保存到本地文件。

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

TOSGetObjectToFileInput *get = [TOSGetObjectToFileInput new];
get.tosBucket = @"bucket-name";
get.tosKey = @"object-name";
get.tosFilePath = your_local_filepath;
TOSTask *task = [client getObjectToFile:get];
[task continueWithBlock:^id(TOSTask *task) {
    if (!task.error) {
        NSLog(@"Get object to file success.");
        TOSGetObjectToFileOutput *output = task.result;
    } else {
        NSLog(@"Get object to file failed, error: %@" ,task.error);
    }
    return nil;
}];