Help Center> Object Storage Service> SDK Reference> iOS> Initialization> Creating an Instance of OBSClient

Creating an Instance of OBSClient

OBSClient functions as the iOS client for accessing OBS. It offers users a series of APIs for interaction with OBS and is used for managing and operating resources, such as buckets and objects, stored in OBS. To use OBS iOS SDK to send a request to OBS, you need to initialize an instance of OBSClient and modify the default configurations in OBSServiceConfiguration based on actual needs.

Sample code for creating an instance of OBSClient using permanent access keys (AK/SK):

NSString *endPoint = @"your-endpoint";
NSString *SK = @"*** Provide your Secret Key ***";
NSString *AK = @"*** Provide your Access Key ***";

// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];

// Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];

// Initialize an instance of OBSClient.
OBSClient *client  = [[OBSClient alloc] initWithConfiguration:conf];

Sample code for creating an instance of OBSClient using temporary access keys (AK/SK and security token):

NSString *endPoint = @"your-endpoint";
NSString *SK = @"*** Provide your Secret Key ***";
NSString *AK = @"*** Provide your Access Key ***";

// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];
// securityToken
credentialProvider.securityToken = @"*** Provide your Security Token ***";

// Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];

// Initialize an instance of OBSClient.
OBSClient *client  = [[OBSClient alloc] initWithConfiguration:conf];

Currently, when multiple resumable upload tasks need to be executed concurrently, an independent instance of OBSClient needs to be initialized for each upload task to process requests.

Sample code for creating an OBSClient instance using a user-defined domain name:

NSString *endPoint = @"your-custom-domain";
NSString *SK = @"*** Provide your Secret Key ***";
NSString *AK = @"*** Provide your Access Key ***";

// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];

// Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];
// Set to use self-defined domain name to access OBS.
conf.defaultDomainMode = OBSDomainModeCustom;

// Initialize an instance of OBSClient.
OBSClient *client  = [[OBSClient alloc] initWithConfiguration:conf];

You can set OBSServiceConfiguration.defaultDomainMode to OBSDomainModeCustom to access OBS using a user-defined domain name. In this case, set endPoint to the user-defined domain name. To access an OBS bucket using a user-defined domain name, you need to bind the user-defined domain name to the access domain name of the bucket. For details, see User-Defined Domain Name Binding and Configuring User-Defined Domain Name Binding.

If CDN is configured for a user-defined domain name, that is, the user-defined domain name is a CDN acceleration domain name, you need to configure CDN to ensure that the user-defined domain name can be used to access OBS. The following uses HUAWEI CLOUD as an example to describe how to configure CDN.

  1. Log in to the HUAWEI CLOUD CDN console and select Domain Name Management from the navigation pane on the left. You can view all configured CDN domain names.
  2. Configure the origin server. Click the user-defined domain name to be used to go to the domain name configuration page. Modify the origin server. Set Type of Primary Origin Server to Domain name, and set Origin Server to the domain name of the OBS bucket to be accessed.

  3. Set the retrieval host. The retrieval host must be specified as the acceleration domain name, that is, the user-defined domain name to access OBS. Otherwise, the retrieval authentication may fail.