初始化
确认Endpoint
在确认您使用SDK时配置的Endpoint时,可先参考阅读API参考中关于 API服务域名 的部分,理解Endpoint相关的概念。 百度智能云目前开放了多区域支持,请参考区域选择说明。 对应信息为:
| 访问区域 | 对应Endpoint | 
|---|---|
| 北京 | bci.bj.baidubce.com | 
| 广州 | bci.gz.baidubce.com | 
| 苏州 | bci.su.baidubce.com | 
| 保定 | bci.bd.baidubce.com | 
| 武汉 | bci.fwh.baidubce.com | 
获取密钥
要使用百度智能云BCI,您需要拥有一个有效的 AK(Access Key ID)和SK(Secret Access Key)用来进行签名认证。AK/SK是由系统分配给用户的,均为字符串,用于标识用户,为访问BCI做签名验证。 可以通过如下步骤获得并了解您的AK/SK信息:
新建BciClient
BciClient是BCI服务的客户端,为开发者与BCI服务进行交互提供了一系列的方法。
使用AK/SK新建BciClient
通过AK/SK方式访问BCI,用户可以参考如下代码新建一个BciClient:
1public class Sample {
2public static void main(String[] args) {
3    String ACCESS_KEY_ID = <your-access-key-id>;                   // 用户的    Access Key ID
4    String SECRET_ACCESS_KEY = <your-secret-access-key>;           // 用户的Secret Access Key
5    String ENDPOINT = <domain-name>;                               // 用户自己指定的域名
6
7    BciClientConfiguration config = new BciClientConfiguration();
8    config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
9    config.setEndpoint(ENDPOINT);
10    BciClient client = new BciClient(config);
11}
            使用STS创建BciClient
申请STS token
BCI可以通过STS机制实现第三方的临时授权访问。STS(Security Token Service)是百度智能云提供的临时授权服务。通过STS,您可以为第三方用户颁发一个自定义时效和权限的访问凭证。第三方用户可以使用该访问凭证直接调用百度智能云的API或SDK访问百度智能云资源。
通过STS方式访问BCI,用户需要先通过STS的client申请一个认证字符串,申请方式可参见百度智能云STS使用介绍。
用STS token新建BciClient
申请好STS后,可将STStoken配置到BciClient中,用户可以参考如下代码新建一个BciClient:
1public class StsExample {
2    private static final String STS_ENDPOINT = "http://sts.bj.baidubce.com";
3    private static final String ENDPOINT = "bci.bj.baidubce.com";
4    private static final String ACCESS_KEY_ID = "your accesskey id";
5    private static final String SECRET_ACCESS_KEY = "your secret accesskey";
6
7    public static void main(String[] args) {
8        BceCredentials credentials = new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY);
9        StsClient client = new StsClient(
10                new BceClientConfiguration().withEndpoint(STS_ENDPOINT).withCredentials(credentials)
11        );
12        GetSessionTokenResponse response = client.getSessionToken(new GetSessionTokenRequest());
13        // or simply call:
14        // GetSessionTokenResponse response = client.getSessionToken();
15        // or you can specify limited permissions with ACL:
16        // GetSessionTokenResponse response = client.getSessionToken(new GetSessionTokenRequest().withAcl("blabla"));
17        // build DefaultBceSessionCredentials object from response:
18        BceCredentials bcistsCredentials = new DefaultBceSessionCredentials(
19                response.getAccessKeyId(),
20                response.getSecretAccessKey(),
21                response.getSessionToken());
22        System.out.println("==================================");
23        System.out.println("GetSessionToken result:");
24        System.out.println("    accessKeyId:  " + response.getAccessKeyId());
25        System.out.println("    secretAccessKey:  " + response.getSecretAccessKey());
26        System.out.println("    securityToken:  " + response.getSessionToken());
27        System.out.println("    expiresAt:  " + response.getExpiration().toString());
28        System.out.println("==================================");
29
30        // build bci client
31        BciClientConfiguration config = new BciClientConfiguration();
32        config.setCredentials(bcistsCredentials);
33        config.setEndpoint(ENDPOINT);
34        BciClient bciClient = new BciClient(config);
35    }
36}
            注意: 目前使用STS配置client时,无论对应bucket的区域在哪里,endpoint都需配置为
http://sts.bj.baidubce.com, 但创建BciClient时,仍需使用BCI的endpoint,如bci.bj.baidubce.com、bci.su.baidubce.com等.
配置自定义域名访问BCI
使用自定义域名
如果希望使用自定义域名作为访问BCI的endpoint,在控制台将自定义域名和BCI某个bucket绑定之后,配置endpoint为自定义域名并打开CnameEnabled开关,例如cdn-test.cdn.bcebci.com,配置代码如下:
1 String ACCESS_KEY_ID = <your-access-key-id>;                   // 用户的Access Key ID
2 String SECRET_ACCESS_KEY = <your-secret-access-key>;           // 用户的Secret Access Key
3 String ENDPOINT = "https://cdn-test.cdn.bcebci.com";           // 用户自己指定的域名
4
5 BciClientConfiguration config = new BciClientConfiguration();
6 config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
7 config.setEndpoint(ENDPOINT);
8 config.setCnameEnabled(true);
9 BciClient client = new BciClient(config);
            配置HTTPS协议访问BCI
BCI支持HTTPS传输协议,您可以通过如下两种方式在BCI Java SDK中使用HTTPS访问BCI服务:
- 
在endpoint中指明https:
Java1String endpoint = "https://bci.bj.baidubce.com"; 2String ak = "ak"; 3String sk = "sk"; 4BciClientConfiguration config = new BciClientConfiguration(); 5config.setEndpoint(ENDPOINT); 6config.setCredentials(new DefaultBceCredentials(ak, sk)); 7BciClient client = new BciClient(config); - 
通过调用setProtocol方法设置https协议:
Java1String endpoint = "bci.bj.baidubce.com"; // endpoint中不包含protocol 2String ak = "ak"; 3String sk = "sk"; 4BciClientConfiguration config = new BciClientConfiguration(); 5config.setCredentials(new DefaultBceCredentials(ak, sk)); 6config.setEndpoint(ENDPOINT); 7config.setProtocol(Protocol.HTTPS); // 如果不指明, 则使用http 8BciClient client = new BciClient(config);注意:如果在endpoint中指明了protocol, 则endpoint中的生效, 另外单独再调用setProtocol()不起作用。
Java1String endpoint = "http://bci.bj.baidubce.com"; 2String ak = "ak"; 3String sk = "sk"; 4BciClientConfiguration config = new BciClientConfiguration(); 5config.setCredentials(new DefaultBceCredentials(ak, sk)); 6config.setEndpoint(ENDPOINT); 7config.setProtocol(Protocol.HTTPS); // endpoint中已经指明, 此为无效操作, 对http也是如此 8BciClient client = new BciClient(config); 
配置BciClient
如果用户需要配置BciClient的一些细节的参数,可以在构造BciClient的时候传入BciClientConfiguration对象。 BciClientConfiguration是BCI服务的配置类,可以为客户端配置代理,最大连接数等参数。
使用代理
下面一段代码可以让客户端使用代理访问BCI服务:
1String ACCESS_KEY_ID = <your-access-key-id>;                   // 用户的Access Key ID
2String SECRET_ACCESS_KEY = <your-secret-access-key>;           // 用户的Secret Access Key
3String ENDPOINT = <domain-name>;                               // 用户自己指定的域名
4
5// 创建BciClientConfiguration实例
6BciClientConfiguration config = new BciClientConfiguration();
7
8// 配置代理为本地8080端口
9config.setProxyHost("127.0.0.1");
10config.setProxyPort(8080);
11
12// 创建BCI客户端
13config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
14config.setEndpoint(ENDPOINT);
15BciClient client = new BciClient(config);
            使用上面的代码段,客户端的所有操作都会通过127.0.0.1地址的8080端口做代理执行。
对于有用户验证的代理,可以通过下面的代码段配置用户名和密码:
1// 创建BciClientConfiguration实例
2BciClientConfiguration config = new BciClientConfiguration();
3    
4// 配置代理为本地8080端口
5config.setProxyHost("127.0.0.1");
6config.setProxyPort(8080);
7    
8//设置用户名和密码
9config.setProxyUsername(<username>);                             //用户名
10config.setProxyPassword(<password>);                             //密码
            设置网络参数
用户可以用BciClientConfiguration对基本网络参数进行设置:
1BciClientConfiguration config = new BciClientConfiguration();
2    
3// 设置HTTP最大连接数为10
4config.setMaxConnections(10);
5    
6// 设置TCP连接超时为5000毫秒
7config.setConnectionTimeoutInMillis(5000);
8    
9// 设置Socket传输数据超时的时间为2000毫秒
10config.setSocketTimeoutInMillis(2000);
            设置同步PUT
对PUT操作,默认使用CloseableHttpAsyncClient,可能会出现用户进程执行完未退出的现象。用户可以通过BciClientConfiguration设置所有BCI请求都通过BciClient同步的方式:
1BciClientConfiguration config = new BciClientConfiguration();
2
3// 设置PUT操作为同步方式,默认异步
4config.setEnableHttpAsyncPut(false);
            参数说明
通过BciClientConfiguration能指定的所有参数如下表所示:
| 参数 | 说明 | 
|---|---|
| CnameEnabled | 使用cname访问BCI资源 | 
| ConnectionTimeoutInMillis | 建立连接的超时时间(单位:毫秒) | 
| Credentials | 客户端用于签署HTTP请求的BCE凭据 | 
| EnableHttpAsyncPut | 异步put | 
| Endpoint | 访问域名 | 
| LocalAddress | 本地地址 | 
| MaxConnections | 允许打开的最大HTTP连接数 | 
| Protocol | 连接协议类型 | 
| ProxyDomain | 访问NTLM验证的代理服务器的Windows域名 | 
| ProxyHost | 代理服务器主机地址 | 
| ProxyPassword | 代理服务器验证的密码 | 
| ProxyPort | 代理服务器端口 | 
| ProxyPreemptiveAuthenticationEnabled | 是否设置用户代理认证 | 
| ProxyUsername | 代理服务器验证的用户名 | 
| ProxyWorkstation | NTLM代理服务器的Windows工作站名称 | 
| Region | 地域 | 
| RetryPolicy | 连接重试策略 | 
| SocketBufferSizeInBytes | Socket缓冲区大小 | 
| SocketTimeoutInMillis | 通过打开的连接传输数据的超时时间(单位:毫秒) | 
| StreamBufferSize | 流文件缓冲区大小 | 
| UserAgent | 用户代理,指HTTP的User-Agent头 | 
| RedirectsEnabled | 是否开启HTTP重定向。默认开启 | 
