You need to enable JavaScript to run this app.
文档中心
对象存储

对象存储

复制全文
下载 pdf
iOS
快速入门(iOS SDK)
复制全文
下载 pdf
快速入门(iOS SDK)

本文介绍 iOS SDK 的常见操作,包括初始化、创建桶等。

前提条件

使用 iOS SDK前,请确保您已安装 SDK

步骤一:初始化 TOS 客户端

TOSClient 是 TOS 服务的 iOS 客户端,提供了一系列方法来操作 Bucket 和 Object。使用 iOS SDK 向 TOS 发送请求前,需要初始化 TOSClient,并进行必要的配置(AccessKey、SecretKey、Endpoint、Region)。TOS 支持的 Region 及 Endpoint 信息,请参见地域及访问域名

注意

移动终端是一个不受信任的环境,在终端直接保存 AccessKey 和 SecretKey 信息并计算签名,存在极高的风险,推荐您使用 STS 鉴权模式,具体步骤,请参见使用 STS 临时 AK/SK+Token 访问火山引擎 TOS

使用 STS 初始化 TOSClient 示例如下:

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

步骤二:配置请求参数

在对 Bucket 或 Object 发起访问请求之前,需要创建相应的请求结构,并通过上述步骤中初始化的 TOSClient 发送给 TOS。
创建桶的请求示例如下:

TOSCreateBucketInput *create = [TOSCreateBucketInput new];
create.tosBucket = TOS_BUCKET;
create.tosAcl = TOSACLPublicRead;
TOSTask *task = [client createBucket:create];

步骤三:处理请求结果

iOS SDK 的所有操作,都会返回一个 TOSTask,您可以通过该 task 查询请求任务的执行状态并从中获取返回结果,还可以通过 continueWithBlock 为其设置异步回调逻辑。

[task continueWithBlock:^id _Nullable(TOSTask * _Nonnull t) {
    if (!t.error) {
        NSLog(@"Create bucket success.");
        TOSCreateBucketOutput *output = t.result;
        NSLog(@"Bucket location: %@", output.tosLocation);
    } else {
        NSLog(@"Create bucket failed, error: %@.", t.error);
    }
    return nil;
}];

您也可以使用 waitUntilFinished 实现同步等待。

[task waitUntilFinished];
最近更新时间:2024.02.04 18:30:59
这个页面对您有帮助吗?
有用
有用
无用
无用