diff --git a/.factorypath b/.factorypath
new file mode 100644
index 00000000..4bb12d8e
--- /dev/null
+++ b/.factorypath
@@ -0,0 +1,125 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/README.md b/README.md
index 4aafbbbb..b7071682 100644
--- a/README.md
+++ b/README.md
@@ -74,7 +74,29 @@ com.baidubce
### 示例
-下面以百度云服务器(BCC)为例,给出一个基本的使用示例,详细使用说明请参考各服务的详细说明文档。
+下面以百度云服务器(BCC)为例,给出两种基本的使用示例。
+
+#### 1. 使用 Fluent Builder (推荐)
+
+这是最现代且简洁的初始化方式,支持自动从环境变量或系统属性加载凭证。
+
+```java
+// 默认从环境变量 BCE_ACCESS_KEY_ID 和 BCE_SECRET_ACCESS_KEY 加载凭证
+BccClient client = BccClientBuilder.create()
+ .withRegion(Region.CN_S1) // 广州区域
+ .build();
+```
+
+或者手动指定凭证:
+
+```java
+BccClient client = BccClientBuilder.create()
+ .withCredentials("your-ak", "your-sk")
+ .withRegion(Region.CN_N1) // 北京区域
+ .build();
+```
+
+#### 2. 使用 Configuration 对象 (传统方式)
```java
public class Sample {
@@ -109,6 +131,15 @@ BccClient client = new BccClient(config);
## 配置
+### 自动凭证加载 (Credentials Providers)
+
+SDK 现在支持自动凭证发现。如果不手动调用 `setCredentials`,SDK 会按以下顺序尝试加载凭证:
+
+1. **系统属性 (System Properties)**: `bce.accessKeyId` 和 `bce.secretAccessKey`
+2. **环境变量 (Environment Variables)**: `BCE_ACCESS_KEY_ID` 和 `BCE_SECRET_ACCESS_KEY`
+
+这使得在 CI/CD 环境或 Docker 容器中管理密钥更加安全和方便。
+
### 使用HTTPS协议
该SDK支持使用HTTPS协议访问百度云的服务产品,您可以通过如下两种方式在BCC Java SDK中使用HTTPS访问BCC服务:
diff --git a/Release Notes.txt b/Release Notes.txt
index 36475818..84875fad 100644
--- a/Release Notes.txt
+++ b/Release Notes.txt
@@ -1,5 +1,12 @@
发行说明:记录每次SDK更新的说明,最新版本的SDK包含以前所有版本的更新内容。
---------------------------------------------------------------------
+【版本:v0.10.448】
+涉及产品:通用,SDK 现代化改进:
+1. 新增 BceClientBuilder 和 BosClientBuilder,支持更简洁的 Fluent 链式调用初始化 Client。
+2. 新增 CredentialsProvider 体系,支持从环境变量(BCE_ACCESS_KEY_ID/BCE_SECRET_ACCESS_KEY)和系统属性自动加载凭证。
+3. 扩展 Region 枚举,增加广州、上海、苏州、保定、香港、新加坡等常用区域支持。
+4. 在 AbstractBceClient 中增加 getClientConfiguration 方法,方便获取客户端配置。
+
【版本:v0.10.358】
涉及产品:VPC 新增参数模板、专线网关、安全组、子网等接口;新增CSN带宽包询价接口,更新查询列表、详情等接口出入参字段。
diff --git a/src/main/java/com/baidubce/AbstractBceClient.java b/src/main/java/com/baidubce/AbstractBceClient.java
index ac1fe3e4..5924fdd0 100755
--- a/src/main/java/com/baidubce/AbstractBceClient.java
+++ b/src/main/java/com/baidubce/AbstractBceClient.java
@@ -156,6 +156,15 @@ public String getServiceId() {
return this.serviceId;
}
+ /**
+ * Returns the client configuration for this client.
+ *
+ * @return the client configuration for this client.
+ */
+ public BceClientConfiguration getClientConfiguration() {
+ return this.config;
+ }
+
public BceHttpClient getClient() {
return client;
}
diff --git a/src/main/java/com/baidubce/BceClientBuilder.java b/src/main/java/com/baidubce/BceClientBuilder.java
new file mode 100644
index 00000000..dfa36686
--- /dev/null
+++ b/src/main/java/com/baidubce/BceClientBuilder.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2014 Baidu, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package com.baidubce;
+
+import com.baidubce.auth.BceCredentials;
+import com.baidubce.auth.BceCredentialsProvider;
+import com.baidubce.auth.DefaultBceCredentials;
+import com.baidubce.auth.StaticCredentialsProvider;
+
+/**
+ * Abstract builder for BCE clients.
+ *
+ * @param The builder type.
+ * @param The client type.
+ */
+public abstract class BceClientBuilder, C> {
+
+ protected BceClientConfiguration config = new BceClientConfiguration();
+
+ @SuppressWarnings("unchecked")
+ protected T self() {
+ return (T) this;
+ }
+
+ public T withCredentials(String accessKeyId, String secretAccessKey) {
+ return withCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKey));
+ }
+
+ public T withCredentials(BceCredentials credentials) {
+ return withCredentialsProvider(new StaticCredentialsProvider(credentials));
+ }
+
+ public T withCredentialsProvider(BceCredentialsProvider credentialsProvider) {
+ this.config.setCredentialsProvider(credentialsProvider);
+ return self();
+ }
+
+ public T withEndpoint(String endpoint) {
+ this.config.setEndpoint(endpoint);
+ return self();
+ }
+
+ public T withRegion(Region region) {
+ this.config.setRegion(region);
+ return self();
+ }
+
+ public T withProtocol(Protocol protocol) {
+ this.config.setProtocol(protocol);
+ return self();
+ }
+
+ /**
+ * Builds the client with the current configuration.
+ *
+ * @return The built client.
+ */
+ public abstract C build();
+}
diff --git a/src/main/java/com/baidubce/BceClientConfiguration.java b/src/main/java/com/baidubce/BceClientConfiguration.java
index 6846462d..22e2c030 100755
--- a/src/main/java/com/baidubce/BceClientConfiguration.java
+++ b/src/main/java/com/baidubce/BceClientConfiguration.java
@@ -16,6 +16,8 @@
import static com.google.common.base.Preconditions.checkNotNull;
import com.baidubce.auth.BceCredentials;
+import com.baidubce.auth.BceCredentialsProvider;
+import com.baidubce.auth.DefaultBceCredentialsProviderChain;
import com.baidubce.http.RetryPolicy;
import com.google.common.base.Joiner;
@@ -155,6 +157,11 @@ public class BceClientConfiguration {
*/
private BceCredentials credentials = null;
+ /**
+ * The provider for BCE credentials.
+ */
+ private BceCredentialsProvider credentialsProvider = new DefaultBceCredentialsProviderChain();
+
/**
* determines whether redirects should be handled automatically
*
@@ -222,6 +229,7 @@ public BceClientConfiguration(BceClientConfiguration other) {
this.endpoint = other.endpoint;
this.region = other.region;
this.credentials = other.credentials;
+ this.credentialsProvider = other.credentialsProvider;
this.redirectsEnabled = other.redirectsEnabled;
this.requestPayer = other.requestPayer;
}
@@ -254,6 +262,7 @@ public BceClientConfiguration(BceClientConfiguration other, String endpoint) {
this.socketBufferSizeInBytes = other.socketBufferSizeInBytes;
this.region = other.region;
this.credentials = other.credentials;
+ this.credentialsProvider = other.credentialsProvider;
this.redirectsEnabled = other.redirectsEnabled;
this.requestPayer = other.requestPayer;
}
@@ -919,7 +928,13 @@ public BceClientConfiguration withRegion(Region region) {
* @return the BCE credentials used by the client to sign HTTP requests.
*/
public BceCredentials getCredentials() {
- return this.credentials;
+ if (this.credentials != null) {
+ return this.credentials;
+ }
+ if (this.credentialsProvider != null) {
+ return this.credentialsProvider.getCredentials();
+ }
+ return null;
}
/**
@@ -946,6 +961,36 @@ public BceClientConfiguration withCredentials(BceCredentials credentials) {
return this;
}
+ /**
+ * Returns the credentials provider used by the client.
+ *
+ * @return the credentials provider used by the client.
+ */
+ public BceCredentialsProvider getCredentialsProvider() {
+ return this.credentialsProvider;
+ }
+
+ /**
+ * Sets the credentials provider used by the client.
+ *
+ * @param credentialsProvider the credentials provider to be used by the client.
+ */
+ public void setCredentialsProvider(BceCredentialsProvider credentialsProvider) {
+ checkNotNull(credentialsProvider, "credentialsProvider should not be null.");
+ this.credentialsProvider = credentialsProvider;
+ }
+
+ /**
+ * Sets the credentials provider used by the client, and returns the updated configuration instance.
+ *
+ * @param credentialsProvider the credentials provider to be used by the client.
+ * @return the updated configuration instance.
+ */
+ public BceClientConfiguration withCredentialsProvider(BceCredentialsProvider credentialsProvider) {
+ this.setCredentialsProvider(credentialsProvider);
+ return this;
+ }
+
@Override
public String toString() {
return "BceClientConfiguration [ \n userAgent=" + userAgent
diff --git a/src/main/java/com/baidubce/Region.java b/src/main/java/com/baidubce/Region.java
index 431df0a4..54d05a80 100644
--- a/src/main/java/com/baidubce/Region.java
+++ b/src/main/java/com/baidubce/Region.java
@@ -23,7 +23,13 @@
*/
public enum Region {
- CN_N1("bj");
+ CN_N1("bj"),
+ CN_S1("gz"),
+ CN_E1("sh"),
+ CN_E2("su"),
+ CN_N2("bd"),
+ AP_S1("hk"),
+ AP_S2("sin");
/**
* The list of ID's representing each region.
diff --git a/src/main/java/com/baidubce/auth/BceCredentialsProvider.java b/src/main/java/com/baidubce/auth/BceCredentialsProvider.java
new file mode 100644
index 00000000..66b4ec1e
--- /dev/null
+++ b/src/main/java/com/baidubce/auth/BceCredentialsProvider.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2014 Baidu, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package com.baidubce.auth;
+
+/**
+ * Interface for providing BCE credentials.
+ */
+public interface BceCredentialsProvider {
+
+ /**
+ * Returns the BCE credentials.
+ *
+ * @return the BCE credentials.
+ */
+ BceCredentials getCredentials();
+}
diff --git a/src/main/java/com/baidubce/auth/DefaultBceCredentialsProviderChain.java b/src/main/java/com/baidubce/auth/DefaultBceCredentialsProviderChain.java
new file mode 100644
index 00000000..b8879aff
--- /dev/null
+++ b/src/main/java/com/baidubce/auth/DefaultBceCredentialsProviderChain.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2014 Baidu, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package com.baidubce.auth;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.baidubce.BceClientException;
+
+/**
+ * Default implementation of {@link BceCredentialsProvider} that chain multiple providers
+ * in the following order:
+ *
+ * - Java System Properties
+ * - Environment Variables
+ *
+ */
+public class DefaultBceCredentialsProviderChain implements BceCredentialsProvider {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(DefaultBceCredentialsProviderChain.class);
+
+ private final List providers = new ArrayList();
+
+ public DefaultBceCredentialsProviderChain() {
+ providers.add(new SystemPropertyCredentialsProvider());
+ providers.add(new EnvironmentVariableCredentialsProvider());
+ }
+
+ @Override
+ public BceCredentials getCredentials() {
+ for (BceCredentialsProvider provider : providers) {
+ try {
+ BceCredentials credentials = provider.getCredentials();
+ if (credentials != null && credentials.getAccessKeyId() != null
+ && credentials.getSecretKey() != null) {
+ return credentials;
+ }
+ } catch (Exception e) {
+ LOGGER.debug("Unable to load credentials from " + provider.getClass().getSimpleName()
+ + ": " + e.getMessage());
+ }
+ }
+ throw new BceClientException("Unable to load credentials from any provider in the chain.");
+ }
+}
diff --git a/src/main/java/com/baidubce/auth/EnvironmentVariableCredentialsProvider.java b/src/main/java/com/baidubce/auth/EnvironmentVariableCredentialsProvider.java
new file mode 100644
index 00000000..80b35c6b
--- /dev/null
+++ b/src/main/java/com/baidubce/auth/EnvironmentVariableCredentialsProvider.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2014 Baidu, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package com.baidubce.auth;
+
+import com.baidubce.BceClientException;
+
+/**
+ * {@link BceCredentialsProvider} implementation that loads credentials from
+ * environment variables.
+ */
+public class EnvironmentVariableCredentialsProvider implements BceCredentialsProvider {
+
+ private static final String ACCESS_KEY_ENV = "BCE_ACCESS_KEY_ID";
+ private static final String SECRET_KEY_ENV = "BCE_SECRET_ACCESS_KEY";
+
+ @Override
+ public BceCredentials getCredentials() {
+ String accessKey = System.getenv(ACCESS_KEY_ENV);
+ String secretKey = System.getenv(SECRET_KEY_ENV);
+
+ if (accessKey == null || secretKey == null) {
+ throw new BceClientException("Unable to load credentials from environment variables "
+ + ACCESS_KEY_ENV + " and " + SECRET_KEY_ENV);
+ }
+
+ return new DefaultBceCredentials(accessKey, secretKey);
+ }
+}
diff --git a/src/main/java/com/baidubce/auth/StaticCredentialsProvider.java b/src/main/java/com/baidubce/auth/StaticCredentialsProvider.java
new file mode 100644
index 00000000..12e0a033
--- /dev/null
+++ b/src/main/java/com/baidubce/auth/StaticCredentialsProvider.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2014 Baidu, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package com.baidubce.auth;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Simple implementation of {@link BceCredentialsProvider} that just wraps a static
+ * {@link BceCredentials} object.
+ */
+public class StaticCredentialsProvider implements BceCredentialsProvider {
+
+ private final BceCredentials credentials;
+
+ public StaticCredentialsProvider(BceCredentials credentials) {
+ checkNotNull(credentials, "credentials should not be null.");
+ this.credentials = credentials;
+ }
+
+ @Override
+ public BceCredentials getCredentials() {
+ return this.credentials;
+ }
+}
diff --git a/src/main/java/com/baidubce/auth/SystemPropertyCredentialsProvider.java b/src/main/java/com/baidubce/auth/SystemPropertyCredentialsProvider.java
new file mode 100644
index 00000000..ac7a57a4
--- /dev/null
+++ b/src/main/java/com/baidubce/auth/SystemPropertyCredentialsProvider.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2014 Baidu, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package com.baidubce.auth;
+
+import com.baidubce.BceClientException;
+
+/**
+ * {@link BceCredentialsProvider} implementation that loads credentials from
+ * Java system properties.
+ */
+public class SystemPropertyCredentialsProvider implements BceCredentialsProvider {
+
+ private static final String ACCESS_KEY_PROPERTY = "bce.accessKeyId";
+ private static final String SECRET_KEY_PROPERTY = "bce.secretAccessKey";
+
+ @Override
+ public BceCredentials getCredentials() {
+ String accessKey = System.getProperty(ACCESS_KEY_PROPERTY);
+ String secretKey = System.getProperty(SECRET_KEY_PROPERTY);
+
+ if (accessKey == null || secretKey == null) {
+ throw new BceClientException("Unable to load credentials from Java system properties "
+ + ACCESS_KEY_PROPERTY + " and " + SECRET_KEY_PROPERTY);
+ }
+
+ return new DefaultBceCredentials(accessKey, secretKey);
+ }
+}
diff --git a/src/main/java/com/baidubce/services/bcc/BccClientBuilder.java b/src/main/java/com/baidubce/services/bcc/BccClientBuilder.java
new file mode 100644
index 00000000..fdd6ed3a
--- /dev/null
+++ b/src/main/java/com/baidubce/services/bcc/BccClientBuilder.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2014 Baidu, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package com.baidubce.services.bcc;
+
+import com.baidubce.BceClientBuilder;
+
+/**
+ * Builder for {@link BccClient}.
+ */
+public class BccClientBuilder extends BceClientBuilder {
+
+ @Override
+ public BccClient build() {
+ return new BccClient(this.config);
+ }
+
+ public static BccClientBuilder create() {
+ return new BccClientBuilder();
+ }
+}
diff --git a/src/main/java/com/baidubce/services/bos/BosClientBuilder.java b/src/main/java/com/baidubce/services/bos/BosClientBuilder.java
new file mode 100644
index 00000000..87e8b1c9
--- /dev/null
+++ b/src/main/java/com/baidubce/services/bos/BosClientBuilder.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2014 Baidu, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package com.baidubce.services.bos;
+
+import com.baidubce.BceClientBuilder;
+
+/**
+ * Builder for {@link BosClient}.
+ */
+public class BosClientBuilder extends BceClientBuilder {
+
+ @Override
+ public BosClient build() {
+ return new BosClient(new BosClientConfiguration(this.config));
+ }
+
+ public static BosClientBuilder create() {
+ return new BosClientBuilder();
+ }
+}
diff --git a/src/test/java/com/baidubce/auth/CredentialsProviderTest.java b/src/test/java/com/baidubce/auth/CredentialsProviderTest.java
new file mode 100644
index 00000000..4a865252
--- /dev/null
+++ b/src/test/java/com/baidubce/auth/CredentialsProviderTest.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2014 Baidu, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package com.baidubce.auth;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.baidubce.BceClientException;
+
+public class CredentialsProviderTest {
+
+ private static final String ACCESS_KEY_PROPERTY = "bce.accessKeyId";
+ private static final String SECRET_KEY_PROPERTY = "bce.secretAccessKey";
+
+ @After
+ public void tearDown() {
+ System.clearProperty(ACCESS_KEY_PROPERTY);
+ System.clearProperty(SECRET_KEY_PROPERTY);
+ }
+
+ @Test
+ public void testStaticCredentialsProvider() {
+ BceCredentials creds = new DefaultBceCredentials("ak", "sk");
+ StaticCredentialsProvider provider = new StaticCredentialsProvider(creds);
+ Assert.assertEquals(creds, provider.getCredentials());
+ }
+
+ @Test
+ public void testSystemPropertyCredentialsProvider() {
+ System.setProperty(ACCESS_KEY_PROPERTY, "prop-ak");
+ System.setProperty(SECRET_KEY_PROPERTY, "prop-sk");
+
+ SystemPropertyCredentialsProvider provider = new SystemPropertyCredentialsProvider();
+ BceCredentials creds = provider.getCredentials();
+
+ Assert.assertNotNull(creds);
+ Assert.assertEquals("prop-ak", creds.getAccessKeyId());
+ Assert.assertEquals("prop-sk", creds.getSecretKey());
+ }
+
+ @Test(expected = BceClientException.class)
+ public void testSystemPropertyCredentialsProviderMissing() {
+ SystemPropertyCredentialsProvider provider = new SystemPropertyCredentialsProvider();
+ provider.getCredentials();
+ }
+
+ @Test
+ public void testDefaultBceCredentialsProviderChain() {
+ System.setProperty(ACCESS_KEY_PROPERTY, "chain-ak");
+ System.setProperty(SECRET_KEY_PROPERTY, "chain-sk");
+
+ DefaultBceCredentialsProviderChain chain = new DefaultBceCredentialsProviderChain();
+ BceCredentials creds = chain.getCredentials();
+
+ Assert.assertNotNull(creds);
+ Assert.assertEquals("chain-ak", creds.getAccessKeyId());
+ }
+}
diff --git a/src/test/java/com/baidubce/services/bos/BosClientBuilderTest.java b/src/test/java/com/baidubce/services/bos/BosClientBuilderTest.java
new file mode 100644
index 00000000..13be936c
--- /dev/null
+++ b/src/test/java/com/baidubce/services/bos/BosClientBuilderTest.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2014 Baidu, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package com.baidubce.services.bos;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.baidubce.BceClientConfiguration;
+import com.baidubce.Protocol;
+import com.baidubce.Region;
+
+public class BosClientBuilderTest {
+
+ @Test
+ public void testBuild() {
+ BosClient client = BosClientBuilder.create()
+ .withCredentials("ak", "sk")
+ .withRegion(Region.CN_S1)
+ .withProtocol(Protocol.HTTPS)
+ .build();
+
+ Assert.assertNotNull(client);
+ BceClientConfiguration config = client.getClientConfiguration();
+ Assert.assertNotNull(config);
+ Assert.assertEquals("ak", config.getCredentials().getAccessKeyId());
+ Assert.assertEquals("sk", config.getCredentials().getSecretKey());
+ Assert.assertEquals(Region.CN_S1, config.getRegion());
+ Assert.assertEquals(Protocol.HTTPS, config.getProtocol());
+ }
+}