Baidu AI Cloud
中国站

百度智能云

Object Storage

Initialization

Confirm Endpoint

To configure the Endpoint configured when you use SDK, you can read the [BOS Access Domain Name](BOS/Developer's Guide/Basic Concept.md#Access Domain Name (Endpoint)#) of Developer's Guide to understand the Endpoint related concept.

At present, Baidu AI Cloud provides multi-region support. See Instruction for Region Selection.

At present, it supports three regions of "North China - Beijing", "South China - Guangzhou", and "East China - Suzhou". Beijing region: http://bj.bcebos.com, Guangzhou region:http://gz.bcebos.com, Suzhou region:http://su.bcebos.com. The corresponding information is as follows:

Access Region Corresponding Endpoint
BJ bj.bcebos.com
GZ gz.bcebos.com
SU su.bcebos.com

Get Key

To use Baidu AI Cloud BOS, you need a valid AK (Access Key ID) and SK (Secret Access Key) for signature authentication. AK/SK is assigned to users by the system, which is a string used to identify users and access BOS for signature authentication. You can get and understand your AK/SK information by following the steps below:

Register Baidu AI Cloud Account

Create AK/SK

Create BosClient

BosClient is the client for BOS services, which provides a series of methods for interaction between the developer and the BOS service.

Create BosClient with AK/SK

You can access BOS in the AK/SK mode, and create a BosClient by referring to the following code:

#include <bcesdk/bos/client.h>
using namespace bcesdk_ns;
int main() {
    std::string ACCESS_KEY_ID = <your-access-key-id>;  // 用户的Access Key ID
    std::string SECRET_ACCESS_KEY = <your-secret-access-key>; // The user’s Secret Access Key

    /* Initialize a Client*/

    //Set logs
    FILE *logfp = fopen("sdk.log", "w");
    sdk_set_log_stream(logfp);
    sdk_set_log_level(SDK_LOG_DEBUG);
    //Set the client
    Client client(ACCESS_KEY_ID, SECRET_ACCESS_KEY);
}

In the above codes, ACCESS_KEY_ID corresponds to “Access Key ID” in the console and SECRET_ACCESS_KEY corresponds to “Access Key Secret” in the console. For their acquisition method, see Operation Guide for ACCESSKEY Management.

The above access mode uses the default domain name as the BOS service address. If you want to specify your domain name, you can specify it by transferring the parameter ENDPOINT.

std::string ACCESS_KEY_ID = <your-access-key-id>;  // Userr's Access Key ID
std::string SECRET_ACCESS_KEY = <your-secret-access-key>; // The user’s Secret Access Key
std::string ENDPOINT = <domain-name>;  // The user-specified domain name


config.endpoint = ENDPOINT;
Client client(ACCESS_KEY_ID, SECRET_ACCESS_KEY, config);

Note: TheENDPOINTparameter can only be defined using the specified domain name containing the region. If not specified, it is the Beijing Region http://bj.bcebos.com by default.

Create BOS Client Using STS

Apply for STS token

BOS can authorize third-party temporary access permission through the STS mechanism. STS (Security Token Service) is the temporary authorization service provided by Baidu AI Cloud. With the help of STS, you can grant a third-party user an access token with the custom valid time and permission. The third-party users can use this credential to directly call the Baidu AI Cloud API or SDK to access the Baidu AI Cloud resources.

To access BOS through STS, users need to apply for an authentication string through the client of STS firstly. For the application method, see Instructions for Baidu AI Cloud STS.

Create BOS client using STS token

After STS is applied, you can configure the STS Token in the BOS Client to implement the creation of the BOS Client through the STS Token.

Code example

Create a BOS Client object using the applied STS Token.

#include <bcesdk/bos/client.h>
using namespace bcesdk_ns;
int main() {
  	//Set logs
    FILE *logfp = fopen("sdk.log", "w");
    sdk_set_log_stream(logfp);
    sdk_set_log_level(SDK_LOG_DEBUG);
  
    std::string ak = <access-key-id>;  // The AK used to access the STS credential.
    std::string sk = <secret-access-key>; // The SK used to access the STS credential.
    std::string sts_token = <session_token>;//SessionToken, which must be carried during the access to the STS credential.    
  	//Use a custom option
    ClientOptions option;  
  	option.endpoint = <domain-name>; 
  	//Configure the STS client
    Client client(Credential(ak, sk, sts_token), option);
}

Configure HTTPS Protocol to Access BOS

The BOS supports the HTTPS transfer 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:

std::string endpoint = "https://bj.bcebos.com";
std::string ak = "ak";
std::string sk = "sk";
ClientOptions config;
config.endpoint = endpoint;
Client client(ak, sk, config);

Configure BosClient

If the user needs to configure the parameters of Client in detail, input the ClientOptions object during the Client creation. ClientOptions is the configuration class of the BOS service, which allows the user to configure the timeout, number of retries, and others for the client.

Set Network Parameters

You can set the basic network parameters using ClientOptions:

ClientOptions config;

// Set the request timeout of HTTP to 120s
config.timeout = 120;
    
// Set additional retries
config.retry = 2;

// Set the number of concurrent requests
config.max_parallel = 10;

Parameter Description

All parameters specified by ClientOptions are shown in the table below:

Parameters Default Value Description
endpoint http://bj.bcebos.com HTTP request terminal
retry 2 The number of additional retries for the HTTP request.
timeout 120 The timeout in seconds, which should be carefully selected according to the request type. For example, a large timeout may be used for downloading a large file.
user_agent cppsdk related You can specify UA when the request is sent.
max_parallel 10 Default number of concurrencies for the concurrent request API. You can directly specify it when you call the API.
multi_part_size 10MB The multipart size of three-step upload or concurrent download is encapsulated in the function.
sign_expire_seconds 1200s Specify the signature timeout.
Previous
SDK Installation
Next
Bucket Management