Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions .factorypath

Large diffs are not rendered by default.

33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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服务:
Expand Down
7 changes: 7 additions & 0 deletions Release Notes.txt
Original file line number Diff line number Diff line change
@@ -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带宽包询价接口,更新查询列表、详情等接口出入参字段。

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/baidubce/AbstractBceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/baidubce/BceClientBuilder.java
Original file line number Diff line number Diff line change
@@ -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 <T> The builder type.
* @param <C> The client type.
*/
public abstract class BceClientBuilder<T extends BceClientBuilder<T, C>, 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();
}
47 changes: 46 additions & 1 deletion src/main/java/com/baidubce/BceClientConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/baidubce/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/baidubce/auth/BceCredentialsProvider.java
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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:
* <ul>
* <li>Java System Properties</li>
* <li>Environment Variables</li>
* </ul>
*/
public class DefaultBceCredentialsProviderChain implements BceCredentialsProvider {

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultBceCredentialsProviderChain.class);

private final List<BceCredentialsProvider> providers = new ArrayList<BceCredentialsProvider>();

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.");
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading