Skip to content

Commit 76b012e

Browse files
author
aldoli
committed
ready to release v5.6.54
1 parent 6a5d46e commit 76b012e

File tree

12 files changed

+140
-48
lines changed

12 files changed

+140
-48
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
## [5.6.54]
8+
- default use https request
9+
- use strict sign
10+
- refine list buckets
11+
712
## [5.6.53] - 2021-8-31
813
- add clientConfig new params
914
- update CI request httpProtocol rules

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.qcloud</groupId>
66
<artifactId>cos_api</artifactId>
7-
<version>5.6.53</version>
7+
<version>5.6.54</version>
88
<packaging>jar</packaging>
99
<name>cos-java-sdk</name>
1010
<description>java sdk for qcloud cos</description>

src/main/java/com/qcloud/cos/COS.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.net.URL;
2525
import java.util.Date;
2626
import java.util.List;
27+
import java.util.Map;
2728

2829
import com.qcloud.cos.exception.CosClientException;
2930
import com.qcloud.cos.exception.CosServiceException;
@@ -2062,9 +2063,10 @@ void deleteBucketReplicationConfiguration(
20622063
* request or handling the response.
20632064
* @see COS#generatePresignedUrl(String, String, Date)
20642065
* @see COS#generatePresignedUrl(String, String, Date, HttpMethodName)
2066+
*
2067+
* public URL generatePresignedUrl(String bucketName, String key, Date expiration)
2068+
* throws CosClientException;
20652069
*/
2066-
public URL generatePresignedUrl(String bucketName, String key, Date expiration)
2067-
throws CosClientException;
20682070

20692071
/**
20702072
* <p>
@@ -2101,7 +2103,7 @@ public URL generatePresignedUrl(String bucketName, String key, Date expiration)
21012103
* @see COS#generatePresignedUrl(String, String, Date, HttpMethodName)
21022104
*/
21032105
public URL generatePresignedUrl(String bucketName, String key, Date expiration,
2104-
HttpMethodName method) throws CosClientException;
2106+
HttpMethodName method, Map<String, String> headers, Map<String, String> params) throws CosClientException;
21052107

21062108
/**
21072109
* <p>

src/main/java/com/qcloud/cos/COSClient.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,19 +2701,23 @@ public void deleteBucketReplicationConfiguration(
27012701
invoke(request, voidCosResponseHandler);
27022702
}
27032703

2704-
@Override
2705-
public URL generatePresignedUrl(String bucketName, String key, Date expiration)
2706-
throws CosClientException {
2707-
return generatePresignedUrl(bucketName, key, expiration, HttpMethodName.GET);
2708-
}
2709-
27102704
@Override
27112705
public URL generatePresignedUrl(String bucketName, String key, Date expiration,
2712-
HttpMethodName method) throws CosClientException {
2706+
HttpMethodName method, Map<String, String> headers, Map<String, String> params) throws CosClientException {
27132707
GeneratePresignedUrlRequest request =
27142708
new GeneratePresignedUrlRequest(bucketName, key, method);
27152709
request.setExpiration(expiration);
27162710

2711+
for (Entry<String, String> entry : params.entrySet()) {
2712+
request.addRequestParameter(entry.getKey(), entry.getValue());
2713+
}
2714+
2715+
request.putCustomRequestHeader(Headers.HOST, this.clientConfig.getEndpointBuilder().buildGeneralApiEndpoint(bucketName));
2716+
2717+
for (Entry<String, String> entry : headers.entrySet()) {
2718+
request.putCustomRequestHeader(entry.getKey(), entry.getValue());
2719+
}
2720+
27172721
return generatePresignedUrl(request);
27182722
}
27192723

src/main/java/com/qcloud/cos/ClientConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class ClientConfig {
6565
**/
6666
public static final BackoffStrategy DEFAULT_BACKOFF_STRATEGY = PredefinedBackoffStrategies.DEFAULT;
6767
private Region region;
68-
private HttpProtocol httpProtocol = HttpProtocol.http;
68+
private HttpProtocol httpProtocol = HttpProtocol.https;
6969
private String endPointSuffix = null;
7070
private EndpointBuilder endpointBuilder = null;
7171
private EndpointResolver endpointResolver = new DefaultEndpointResolver();

src/main/java/com/qcloud/cos/auth/COSSigner.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,22 @@
3030

3131
import java.util.Date;
3232
import java.util.HashMap;
33+
import java.util.HashSet;
3334
import java.util.Map;
3435
import java.util.Map.Entry;
35-
import java.util.TreeMap;
3636
import java.util.Set;
37-
import java.util.HashSet;
38-
39-
import org.apache.commons.codec.digest.DigestUtils;
40-
import org.apache.commons.codec.digest.HmacUtils;
37+
import java.util.TreeMap;
4138

4239
import com.qcloud.cos.Headers;
40+
import com.qcloud.cos.exception.CosClientException;
4341
import com.qcloud.cos.http.CosHttpRequest;
4442
import com.qcloud.cos.http.HttpMethodName;
4543
import com.qcloud.cos.internal.CosServiceRequest;
4644
import com.qcloud.cos.utils.UrlEncoderUtils;
4745

46+
import org.apache.commons.codec.digest.DigestUtils;
47+
import org.apache.commons.codec.digest.HmacUtils;
48+
4849
public class COSSigner {
4950
private static Set<String> needSignedHeaderSet = new HashSet<>();
5051
private Boolean isCIWorkflowRequest = false;
@@ -79,11 +80,6 @@ public <X extends CosServiceRequest> void sign(CosHttpRequest<X> request, COSCre
7980
((COSSessionCredentials) cred).getSessionToken());
8081
}
8182
}
82-
public String buildAuthorizationStr(HttpMethodName methodName, String resouce_path,
83-
COSCredentials cred, Date expiredTime) {
84-
return buildAuthorizationStr(methodName, resouce_path, new HashMap<String, String>(),
85-
new HashMap<String, String>(), cred, expiredTime);
86-
}
8783

8884
public String buildPostObjectSignature(String secretKey, String keyTime, String policy) {
8985
String signKey = HmacUtils.hmacSha1Hex(secretKey, keyTime);
@@ -152,14 +148,26 @@ public boolean needSignedHeader(String header) {
152148
}
153149

154150
private Map<String, String> buildSignHeaders(Map<String, String> originHeaders) {
151+
Boolean hasHost = false;
155152
Map<String, String> signHeaders = new HashMap<>();
156153
for (Entry<String, String> headerEntry : originHeaders.entrySet()) {
157154
String key = headerEntry.getKey().toLowerCase();
155+
156+
if (key.equals("host")) {
157+
hasHost = true;
158+
}
159+
158160
if(needSignedHeader(key)) {
159161
String value = headerEntry.getValue();
160162
signHeaders.put(key, value);
161163
}
162164
}
165+
166+
if (!hasHost) {
167+
String msg = String.format("buildAuthorization missing header: host. %s", originHeaders);
168+
throw new CosClientException(msg);
169+
}
170+
163171
return signHeaders;
164172
}
165173

src/main/java/com/qcloud/cos/demo/BucketDemo.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,27 @@ public static void JudgeBucketExistDemo() {
144144
// 关闭客户端
145145
cosclient.shutdown();
146146
}
147+
148+
public static void ListBuckets() {
149+
// 1 初始化用户身份信息(appid, secretId, secretKey)
150+
COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "****************************");
151+
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
152+
ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
153+
// 3 生成cos客户端
154+
COSClient cosclient = new COSClient(cred, clientConfig);
155+
156+
List<Bucket> buckets = cosclient.listBuckets();
157+
158+
for (Bucket bucket : buckets) {
159+
System.out.println(bucket.getName());
160+
System.out.println(bucket.getLocation());
161+
System.out.println(bucket.getOwner());
162+
System.out.println(bucket.getType());
163+
System.out.println(bucket.getBucketType());
164+
}
165+
}
166+
167+
public static void main(String[] args) {
168+
ListBuckets();
169+
}
147170
}

src/main/java/com/qcloud/cos/demo/GeneratePresignedUrlDemo.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.net.ProtocolException;
77
import java.net.URL;
88
import java.util.Date;
9+
import java.util.HashMap;
10+
import java.util.Map;
911

1012
import com.qcloud.cos.COSClient;
1113
import com.qcloud.cos.ClientConfig;
@@ -45,6 +47,12 @@ public static void GenerateSimplePresignedDownloadUrl() {
4547
Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
4648
req.setExpiration(expirationDate);
4749

50+
// 填写本次请求的参数
51+
req.addRequestParameter("param1", "value1");
52+
53+
// 填写本次请求的头部。Host 头部会自动补全,不需要填写
54+
req.putCustomRequestHeader("header1", "value1");
55+
4856
URL url = cosclient.generatePresignedUrl(req);
4957
System.out.println(url.toString());
5058

@@ -83,6 +91,13 @@ public static void GeneratePresignedDownloadUrlWithOverrideResponseHeader() {
8391
// 这里设置签名在半个小时后过期
8492
Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
8593
req.setExpiration(expirationDate);
94+
95+
// 填写本次请求的参数
96+
req.addRequestParameter("param1", "value1");
97+
98+
// 填写本次请求的头部。Host 头部会自动补全,不需要填写
99+
req.putCustomRequestHeader("header1", "value1");
100+
86101
URL url = cosclient.generatePresignedUrl(req);
87102

88103
System.out.println(url.toString());
@@ -114,17 +129,22 @@ public static void GeneratePresignedDownloadUrlAnonymous() {
114129
// 生成预签名的上传连接
115130
public static void GeneratePresignedUploadUrl() {
116131
// 1 初始化用户身份信息(secretId, secretKey)
117-
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
132+
COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "********************************");
118133
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
119-
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
134+
ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
120135
// 3 生成cos客户端
121136
COSClient cosclient = new COSClient(cred, clientConfig);
122137
// bucket名需包含appid
123138
String bucketName = "mybucket-1251668577";
124139

125140
String key = "aaa.txt";
126141
Date expirationTime = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
127-
URL url = cosclient.generatePresignedUrl(bucketName, key, expirationTime, HttpMethodName.PUT);
142+
// 填写本次请求的 header。Host 头部会自动补全,只需填入其他头部
143+
Map<String, String> headers = new HashMap<String,String>();
144+
// 填写本次请求的 params。
145+
Map<String, String> params = new HashMap<String,String>();
146+
147+
URL url = cosclient.generatePresignedUrl(bucketName, key, expirationTime, HttpMethodName.PUT, headers, params);
128148
try {
129149
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
130150
connection.setDoOutput(true);
@@ -145,4 +165,7 @@ public static void GeneratePresignedUploadUrl() {
145165
}
146166

147167

168+
public static void main(String[] args) {
169+
GeneratePresignedUploadUrl();
170+
}
148171
}

src/main/java/com/qcloud/cos/internal/XmlResponsesSaxParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,10 @@ else if (in("ListAllMyBucketsResult", "Buckets", "Bucket")) {
10411041
currentBucket.setCreationDate(creationDate);
10421042
} else if (name.equals("Location")) {
10431043
currentBucket.setLocation(getText());
1044+
} else if (name.equals("BucketType")) {
1045+
currentBucket.setBucketType(getText());
1046+
} else if (name.equals("Type")) {
1047+
currentBucket.setType(getText());
10441048
}
10451049
}
10461050
}

src/main/java/com/qcloud/cos/model/Bucket.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public class Bucket implements Serializable {
3434
/** The location of the bucket */
3535
private String location = null;
3636

37+
/** The type of the bucket */
38+
private String bucketType = null;
39+
40+
/** The type set by internal */
41+
private String type = null;
42+
3743
/**
3844
* Constructs a bucket without any name specified.
3945
*
@@ -132,5 +138,19 @@ public void setLocation(String location) {
132138
this.location = location;
133139
}
134140

141+
public void setBucketType(String bucketType) {
142+
this.bucketType = bucketType;
143+
}
144+
145+
public String getBucketType() {
146+
return this.bucketType;
147+
}
148+
149+
public void setType(String type) {
150+
this.type = type;
151+
}
135152

153+
public String getType() {
154+
return this.type;
155+
}
136156
}

0 commit comments

Comments
 (0)