Initialization
Determine Endpoint
Before configuring the Endpoint for SDK usage, please refer to the developer guide section on [BOS Access Domain Name](BOS/Developer Guide/Basic concepts.md#Endpoint) to understand Endpoint-related concepts. Baidu AI Cloud currently supports multiple regions. Please refer to[Region Selection Guide](Reference/Region Selection Instructions/Region.md).
Refer to the following link for region and endpoint
https://cloud.baidu.com/doc/BOS/s/akrqd2wcx
Common examples are as follows:
| Access region | Endpoint |
|---|---|
| BJ | bj.bcebos.com |
| BD | bd.bcebos.com |
| SU | su.bcebos.com |
| GZ | gz.bcebos.com |
| CD | cd.bcebos.com |
| HKG | hkg.bcebos.com |
| FWH | fwh.bcebos.com |
| FSH | fsh.bcebos.com |
Retrieve access key
To use Baidu AI Cloud BOS, you need a valid AK (Access Key ID) and SK (Secret Access Key) for signature certification. AK/SK are system-assigned strings used to identify users and perform signature certification for BOS. Your AK/SK information can be obtained and understood through the following steps:
Register a Baidu AI Cloud account
Create a BosClient
BosClient functions as a client for BOS services, giving developers various methods to interact with these services.
Create a BosClient with AK/SK
Users can refer to the following code to create a BosClient to access BOS with AK/SK:
1#include <bcesdk/bos/client.h>
2using namespace bcesdk_ns;
3int main() {
4 std::string ACCESS_KEY_ID = <your-access-key-id>; // User’s Access Key ID
5 std::string SECRET_ACCESS_KEY = <your-secret-access-key>; // User’s Secret Access Key
6 /* Initialize a Client*/
7 // Set log
8 FILE *logfp = fopen("sdk.log", "w");
9 sdk_set_log_stream(logfp);
10 sdk_set_log_level(SDK_LOG_DEBUG);
11 //Set client
12 Client client(ACCESS_KEY_ID, SECRET_ACCESS_KEY);
13}
In the code above, ACCESS_KEY_ID corresponds to “Access Key ID” in the console. SECRET_ACCESS_KEY corresponds to “Access Key Secret” in the console. For the method to retrieve them, refer to the Guide - [Manage ACCESSKEY](Reference/Retrieve AK and SK/How to Obtain AKSK.md).
The method above uses the default domain name as BOS's service address. If you want to set a custom domain name, specify it using the ENDPOINT parameter.
1std::string ACCESS_KEY_ID = <your-access-key-id>; // User’s Access Key ID
2 std::string SECRET_ACCESS_KEY = <your-secret-access-key>; // User’s Secret Access Key
3 std::string ENDPOINT = <domain-name>; // User-defined domain name
4config.endpoint = ENDPOINT;
5Client client(ACCESS_KEY_ID, SECRET_ACCESS_KEY, config);
Note:
The ENDPOINTparameter must use region-specific domain names (e.g., http://aihc.bj.baidubce.com for Beijing). If unspecified, it defaults to the Beijing regionhttp://bj.bcebos.com.
Create a BOS client with STS
Request STS Token
BOS allows temporary third-party access authorization using the STS mechanism. STS (Security Token Service) is a temporary authorization tool provided by Baidu AI Cloud, enabling you to issue access credentials with customized validity periods and permissions for third-party users. These users can use the credentials to call Baidu AI Cloud APIs or SDKs directly to access cloud resources.
To access BOS via STS, users must first request a certification string through the STS client. For instructions on obtaining STS credentials, refer to Baidu AI Cloud STS Usage Guide.
Create BOS Client with STS Token
After obtaining the STS credentials, configure the STS Token in the BOS Client to enable the creation of the BOS Client with the token.
Code example
Use the acquired STS Token to create a BOS Client object.
1#include <bcesdk/bos/client.h>
2using namespace bcesdk_ns;
3int main() {
4 // Set log
5 FILE *logfp = fopen("sdk.log", "w");
6 sdk_set_log_stream(logfp);
7 sdk_set_log_level(SDK_LOG_DEBUG);
8
9 std::string ak = <access-key-id>; // AK for STS credential access
10 std::string sk = <secret-access-key>; // SK for STS credential access
11 std::string sts_token = <session_token>;//SessionToken, must be carried when accessing with STS credentials
12 //Use custom options
13 ClientOptions option;
14 option.endpoint = <domain-name>;
15 //Configure STS client
16 Client client(Credential(ak, sk, sts_token), option);
17}
Configure HTTPS access to BOS
BOS supports HTTPS transport protocol. You can use HTTPS to access the BOS service in the BOS C++ SDK in the following two ways:
Specify HTTPS in the endpoint:
1std::string endpoint = "https://bj.bcebos.com";
2std::string ak = "ak";
3std::string sk = "sk";
4ClientOptions config;
5config.endpoint = endpoint;
6Client client(ak, sk, config);
Configure BosClient
If detailed client parameters need configuration, pass a ClientOptions object during Client construction. ClientOptions, the configuration class for BOS services, allows settings like timeout durations, retry counts, and other client specifications.
Set network parameters
Users may set the basic network parameters with ClientOptions:
1ClientOptions config;
2 // Set HTTP request timeout to 120 s
3config.timeout = 120;
4
5 // Set additional retry attempts
6config.retry = 2;
7 // Set the count of concurrent requests
8config.max_parallel = 10;
Parameter description
The following parameters can be configured via ClientOptions:
| Parameters | Default value | Description |
|---|---|---|
| endpoint | http://bj.bcebos.com |
HTTP request terminal |
| retry | 2 | Additional retry attempts for HTTP requests |
| timeout | 120 | It is the timeout in seconds, which should be carefully selected according to the request type. For example, downloading a large file may require a relatively longer timeout. |
| connect_timeout_ms | 10000 | It is the HTTP connection timeout in milliseconds, with the default value 10 seconds. |
| user_agent | Related to cppsdk | You can specify the UA when sending the request |
| max_parallel | 10 | Default concurrency count of concurrent request API (it can also be specified directly via parameters when calling the API) |
| multi_part_size | 10MB | The functional function encapsulates the part size limit for three-step upload or concurrent download. |
| sign_expire_seconds | 1200s | Specify the timeout duration for signature |
| calculate_md5_on | false | Specify whether to calculate md5 when sending a request |
| cname_enabled | false | Specify whether to use a custom domain name when sending requests. Before enabling, you need to apply to activate the custom domain name for the bucket |
| auto_cname_enabled | true | It is enabled by default, and the four-level domain name of virtual host is used to access BOS, otherwise the GET request will encounter a 403 error. |
| proxy_protocol | 1200s | Specify the timeout duration for signature |
| client_enable_cancel | false | Specify whether requests sent by BosClient can be actively canceled by other threads. When enabled, cancellation must also be set in the request that is intended to be canceled |
