This document presents common operations of the iOS SDK, including initialization, bucket creation, and so on.
Prerequisites
Step 1: Initialize the TOS client
TOSClient is the iOS client for the TOS service, providing a series of methods for managing Buckets and Objects. Before sending requests to TOS using the iOS SDK, you must initialize TOSClient and configure the required parameters: AccessKey, SecretKey, Endpoint, and Region. For information on the Regions and Endpoints supported by TOS, please refer to Regions and Access Domain Names. warning
Mobile devices constitute an untrusted environment; storing AccessKey and SecretKey information directly on the device and performing signature calculations poses extremely high risks. It is strongly recommended to use the STS authentication mode. For specific steps, please refer to Using STS Temporary AK/SK+Token to Access Volcano Engine TOS. The following example demonstrates how to initialize TOSClient using STS:
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];
Step 2: Configure Request Parameters
Before initiating an access request to a Bucket or Object, you must create the corresponding request structure and send it to TOS using the TOSClient initialized in the preceding steps.
An example request for creating a bucket is as follows:
TOSCreateBucketInput *create = [TOSCreateBucketInput new];
create.tosBucket = TOS_BUCKET;
create.tosAcl = TOSACLPublicRead;
TOSTask *task = [client createBucket:create];
Step 3: Processing Request Results
All operations in the iOS SDK return a TOSTask. You can use the task to query the execution status of the request and obtain the returned result from it. You can also use continueWithBlock to set asynchronous callback logic.
[task continueWithBlock:^id _Nullable(TOSTask * _Nonnull t) {
NSLog(@"Create bucket success.");
TOSCreateBucketOutput *output = t.result;
NSLog(@"Bucket location: %@", output.tosLocation);
NSLog(@"Create bucket failed, error: %@.", t.error);
You can also use waitUntilFinished to achieve synchronous waiting.
[task waitUntilFinished];