百度智能云

All Product Document

          Object Storage

          Initialization

          Confirm Endpoint

          Before confirming the Endpoint you configured when using the SDK, read the section of the Developer Guide on BOS Access Domain Name to understand Endpoint-related concepts. Baidu AI Cloud currently has opened access to multi-region support, please refer to Region Selection Description.

          Currently, it supports "North China-Beijing", "South China-Guangzhou" and "East China-Suzhou". Beijing: http://bj.bcebos.com; Guangzhou: http://gz.bcebos.com; Suzhou: http://su.bcebos.com The corresponding information is:

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

          Get the Key

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

          Register Baidu AI Cloud Account

          Create AK/SK

          Create BOSClient

          BosClient is the client of BOS service, providing methods for developers to interact with BOS service.

          Use AK/SK to Create BosClient

          Access BOS through AK/SK. You can refer to the following code to create BosClient:

          public class Sample { 
          public static void main(String[] args) { 
              String ACCESS_KEY_ID =<your-access-key-id>;                   // User's Access Key ID 
              String SECRET_ACCESS_KEY =<your-secret-access-key>;           // User's Secret Access Key 
                  
              // Initialize a BosClient 
              BosClientConfiguration config = new BosClientConfiguration(); 
              config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY)); 
              BosClient client = new BosClient(config); 
              } 
          } 

          In the above code, ACCESS_KEY_ID and SECRET_ACCESS_KEY correspond to the "Access Key ID" and the "Access Key Secret" in the console, respectively. For more information on how to access it, please see the <Operation Guide Manage ACCESSKEY>.

          The above method uses the default domain name as the service address of BOS. If you need to specify the domain name, specify it by introducing into the ENDPOINT parameter.

          String ACCESS_KEY_ID =<your-access-key-id>;                   // User's Access Key ID 
          String SECRET_ACCESS_KEY =<your-secret-access-key>;           // User's Secret Access Key 
          String ENDPOINT =<domain-name>;                               // User-specified domain name 
          
          BosClientConfiguration config = new BosClientConfiguration(); 
          config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY)); 
          config.setEndpoint(ENDPOINT); 
          BosClient client = new BosClient(config); 

          Note: The ENDPOINT parameter can only be defined with the specified domain name of the areas included. If not specified, it defaults to the Beijing area http://bj.bcebos.com.

          Create BosClient with STS

          Apply for STS Token

          BOS can implement temporary authorized access by a third party through the STS mechanism. STS (Security Token Service) is a temporary authorization service provided by Baidu AI Cloud. With STS, you can issue a third-party user with a custom time-based and privileged access credential. The third-party user can use the access credential to directly call API or SDK of Baidu AI Cloud to access its resources.

          To access BOS via STS, users need to apply for an authentication string via the client of STS; for the application method, see Introduction to Use of Baidu AI Cloud STS.

          Create a BOSClient with STS token

          After applying for STS, configure STStoken to BosClient, and users can create a BosClient by reference to the following codes:

          public class StsExample {
              private static final String STS_ENDPOINT = "http://sts.bj.baidubce.com";
              private static final String ACCESS_KEY_ID = "your accesskey id";
              private static final String SECRET_ACCESS_KEY = "your secret accesskey";
          
              public static void main(String[] args) {
                  BceCredentials credentials = new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY);
                  StsClient client = new StsClient(
                          new BceClientConfiguration().withEndpoint(STS_ENDPOINT).withCredentials(credentials)
                  );
                  GetSessionTokenResponse response = client.getSessionToken(new GetSessionTokenRequest());
                  // or simply call:
                  // GetSessionTokenResponse response = client.getSessionToken();
                  // or you can specify limited permissions with ACL:
                  // GetSessionTokenResponse response = client.getSessionToken(new GetSessionTokenRequest().withAcl("blabla"));
                  // build DefaultBceSessionCredentials object from response:
                  BceCredentials bosstsCredentials = new DefaultBceSessionCredentials(
                          response.getAccessKeyId(),
                          response.getSecretAccessKey(),
                          response.getSessionToken());
                  System.out.println("==================================");
                  System.out.println("GetSessionToken result:");
                  System.out.println("    accessKeyId:  " + response.getAccessKeyId());
                  System.out.println("    secretAccessKey:  " + response.getSecretAccessKey());
                  System.out.println("    securityToken:  " + response.getSessionToken());
                  System.out.println("    expiresAt:  " + response.getExpiration().toString());
                  System.out.println("==================================");
          
                  // build bos client
                  BosClientConfiguration config = new BosClientConfiguration();
                  config.setCredentials(tempCredentials);
                  BosClient bosClient = new BosClient(config);
              }
          }

          Note: Currently, when STS is used to configure client, wherever the endpoint of the corresponding BOS service is, endpoint needs to be configured as http://sts.bj.baidubce.com.

          Configure HTTPS to Access BOS

          BOS supports HTTPS, and you can access to BOS service with HTTP in BOS Java SDK with the following 2 methods:

          • Indicate https in endpoint.

              String endpoint = "https://bj.bcebos.com";
              String ak = "ak";
              String sk = "sk";
              BosClientConfiguration config = new BosClientConfiguration();
              config.setCredentials(new DefaultBceCredentials(ak, sk));
              BosClient client = new BosClient(config);
          • Set the https protocol by calling the setProtocol method:

              String endpoint = "bj.bcebos.com"; // endpoint中不包含protocol
              String ak = "ak";
              String sk = "sk";
              BosClientConfiguration config = new BosClientConfiguration();
              config.setCredentials(new DefaultBceCredentials(ak, sk));
              config.setEndpoint(ENDPOINT);
              config.setProtocol(Protocol.HTTPS); // If protocol is not indicated, http is used.
              BosClient client = new BosClient(config);

            Note: If protocol is indicated in endpoint, the entry in endpoint takes effect, and a separate call to setProtocol () does not work.

              String endpoint = "http://bj.bcebos.com"; 
              String ak = "ak"; 
              String sk = "sk"; 
              BosClientConfiguration config = new BosClientConfiguration(); 
              config.setCredentials(new DefaultBceCredentials(ak, sk)); 
              config.setEndpoint(ENDPOINT);    
              config.setProtocol(Protocol.HTTPS); // As indicated in endpoint, this is an invalid operation, as is http. 
              BosClient client = new BosClient(config); 

          Configure BosClient

          To configure parameters of some details of BosClient, users can introduce BosClientConfiguration object when constructing BosClient. BosClientConfiguration is the configuration class of BOS service, and it can configure agent, maximum connection number and other parameters for the client.

          Use Agent

          The following codes enable client to use agent to access BOS service:

          String ACCESS_KEY_ID =<your-access-key-id>;                   // User's Access Key ID 
          String SECRET_ACCESS_KEY =<your-secret-access-key>;           // User's Secret Access Key 
          String ENDPOINT =<domain-name>;                               // User-specified domain name 
          
          // Create BosClientConfiguration instance 
          BosClientConfiguration config = new BosClientConfiguration(); 
          
          // Configure the agent as local 8080 port 
          config.setProxyHost("127.0.0.1"); 
          config.setProxyPort(8080); 
          
          // Create BOS client 
          config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY)); 
          config.setEndpoint(ENDPOINT); 
          BosClient client = new BosClient(config); 

          Using the code segment above, all operations of client are executed as an agent via the 8080 port of 127.0.0.1 address.

          For the agent with user authentication, the following code segment can be used to configure username and password:

          // Create BosClientConfiguration instance 
          BosClientConfiguration config = new BosClientConfiguration(); 
              
          // Configure the agent as local 8080 port 
          config.setProxyHost("127.0.0.1"); 
          config.setProxyPort(8080); 
              
          //Set username and password 
          config.setProxyUsername(<username>);                             //User name 
          config.setProxyPassword(<password>);                             //Password 

          Set Network Parameters

          Users can set the basic network parameters with BosClientConfiguration:

          BosClientConfiguration config = new BosClientConfiguration(); 
              
          // Set the maximum number of HTTP connections to 10.
          config.setMaxConnections(10); 
              
          // Set TCP connection timeout to 5,000 milliseconds 
          config.setConnectionTimeout(5000); 
              
          // Set the timeout for socket data transmission to 2,000 milliseconds 
          config.setSocketTimeout(2000); 

          Parameter Description

          All parameters that can be specified through BosClientConfiguration are shown in the following table:

          Parameter Description
          UserAgent User agent, referring to the HTTP User-Agent header
          Protocol Connection protocol type
          ProxyDomain Windows domain name for access to NTLM verified proxy server
          ProxyHost Proxy server host address
          ProxyPort Proxy server port
          ProxyUsername User name for proxy server authentication
          ProxyPassword Password for proxy server authentication
          ProxyPreemptiveAuthenticationEnabled Whether to set up user agent authentication
          ProxyWorkstation Windows workstation name of the NTLM proxy server
          LocalAddress Local address
          ConnectionTimeoutInMillis Timeout for establishing a connection (unit: millisecond)
          SocketTimeoutInMillis Timeout for transmitting data over open connections (unit: millisecond)
          MaxConnections Maximum number of HTTP connections allowed to open
          RetryPolicy Connection retry policy
          SocketBufferSizeInBytes Socket buffer size
          StreamBufferSize Stream file buffer size
          UserAgent The User Agent, refers to the user-agent HTTP headers
          RedirectsEnabled Whether to enable HTTP redirection. The default open
          Previous
          SDK Installation
          Next
          Bucket Management