搜索本产品文档关键词
初始化
所有文档
menu

云数据库 TableStorage

初始化

确认Endpoint

TableStorage目前支持“华北-保定”和“华南-广州”两个区域,对应Endpoint如下:

访问区域
对应Endpoint
华北-保定 bts.bd.baidubce.com
华南-广州 bts.gz.baidubce.com

鉴权和认证

要使用百度智能云产品,您需要拥有一个百度智能云账号和一个有效的 AK(Access Key ID)、SK(Secret Access Key)用来进行签名认证。

可以通过如下步骤获得并了解您的AK/SK信息: 1. 注册百度智能云账号 2. 创建AK/SK

新建TableStorageClient

TableStorageClient是与Table Storage服务交互的方法类,为开发者提供了与TableStorage交互的一系列方法。

使用AK/SK新建TableStorageClient

使用AK/SK访问TableStorage,可以参考以下代码创建TableStorageClient:

Plain Text
1public class Sample {
2    public static void main(String[] args) {
3        String accessKeyId = <your-access-key-id>              // 用户的Access Key ID
4        String secretAccessKey = <your-secret-access-key>      // 用户的Secret Access Key
5    	String endpoint = <domain-nam>                         // 用户期望访问的域名
6    	String instanceName = <instance-name>                  // 用户期望访问的instance
7
8        // 初始化一个TableStorageClient
9        BceClientConfiguration conf = new BceClientConfiguration();
10        conf.setCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKey));
11        conf.setEndpoint(endpoint);
12        TableStorageClient client = new TableStorageClient(conf, instanceName);
13    }
14}

在上面代码中,“accessKeyId”对应控制台中的“Access Key ID”,“secretAccessKey”对应控制台中的“Access Key Secret”,获取方式请参考《管理ACCESSKEY》。

用STS token新建TableStorageClient

TableStorage可以通过STS机制实现第三方的临时授权访问。STS(Security Token Service)是百度智能云提供的临时授权服务,详细介绍可以参见临时授权访问

使用STS token访问TableStorage,首先需要访问STS服务,申请STS token。申请到STS token之后,可将STS token配置到TableStorageClient中,可以参考如下代码新建TableStorageClient:

Plain Text
1public class Sample {
2    public static void main(String[] args) {
3        BceCredentials credentials = new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY);
4        StsClient client = new StsClient(
5                new BceClientConfiguration().withEndpoint(STS_ENDPOINT).withCredentials(credentials)
6        );
7        GetSessionTokenResponse response = client.getSessionToken(new GetSessionTokenRequest());
8        // or simply call;
9        // GetSessionTokenResponse response = client.getSessionToken();
10        // or you can specify limited permissions with ACL:
11        // GetSessionTokenResponse response = client.getSessionToken(new GetSessionTokenRequest().withAcl("blabla"));
12        // build DefaultBceSessionCredentials object from response:
13        BceCredentials tableStorageStsCredentials = new DefaultBceSessionCredentials(
14                response.getAccessKeyId(),
15                response.getSecretAccessKey(),
16                response.getSessionToken());
17        System.out.println("==================================");
18        System.out.println("GetSessionToken result:");
19        System.out.println("    accessKeyId:  " + response.getAccessKeyId());
20        System.out.println("    secretAccessKey:  " + response.getSecretAccessKey());
21        System.out.println("    securityToken:  " + response.getSessionToken());
22        System.out.println("    expiresAt:  " + response.getExpiration().toString());
23        System.out.println("==================================");
24
25        // build TableStorage client
26        BceClientConfiguration config = new BceClientConfiguration();
27        config.setCredentials(tableStorageStsCredentials);
28        config.setEndpoint(TABLESTORAGE_ENDPOINT);
29        TableStorageClient tableStorageClient = new TableStorageClient(config, INSTANCE_NAME);
30    }
31}

注意:目前使用STS配置client时,无论对应TableStorage服务的endpoint在哪里,endpoint都需配置为http://sts.bj.baidubce.com

配置HTTPS协议访问TableStorage

TableStorage服务支持HTTPS传输协议,您可以通过如下两种方式在TableStorage Java SDK中使用HTTPS访问:

在endpoint中指明https

Plain Text
1BceClientConfiguration conf = new BceClientConfiguration();
2conf.setCredentials(new DefaultBceCredentials(ACCESS_KEY, SECRET_ACCESS_KEY));
3conf.setEndpoint("https://bts.bd.baidubce.com");
4TableStorageClient client = new TableStorageClient(conf, INSTANCE_NAME, false);

通过调用setProtocol方法设置https协议

Plain Text
1BceClientConfiguration conf = new BceClientConfiguration();
2conf.setCredentials(new DefaultBceCredentials(ACCESS_KEY, SECRET_ACCESS_KEY));
3conf.setEndpoint("bts.bd.baidubce.com");
4conf.setProtocol(Protocol.HTTPS);
5TableStorageClient client = new TableStorageClient(conf, INSTANCE_NAME, false);

注意:如果在endpoint中指明了protocol, 则endpoint中的生效, 另外单独再调用setProtocol()不起作用。

配置TableStorageClient

TableStorageClient的网络参数可以通过BceClientConfiguration配置, 包括以下参数:

参数
说明
UserAgent 用户代理,指HTTP Header中的User-Agent
Protocol 网络协议类型,支持HTTP和HTTPS
LocalAddress 本地地址
ConnectionTimeoutInMillis 建立连接的超时时间(单位:毫秒)
SocketTimeoutInMillis 数据传输的超时时间(单位:毫秒)
MaxConnections 允许建立的最大连接数
RetryPolicy 重试策略,默认为DefaultRetryPolicy

说明:如无特殊需求,以上参数均可使用默认值。

上一篇
安装
下一篇
表操作