Skip to content

Commit 9b2a9bf

Browse files
author
jordanqin
committed
update qcloud sdk to 5.9.15
1 parent ec0557e commit 9b2a9bf

File tree

6 files changed

+185
-65
lines changed

6 files changed

+185
-65
lines changed

QCloudCosXml/cos-android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ android {
3333

3434
consumerProguardFiles 'proguard-rules.pro'
3535

36-
// multiDexEnabled true
36+
multiDexEnabled true
3737
}
3838

3939
buildTypes {

QCloudCosXml/cos-android/src/androidTest/java/com/tencent/cos/xml/core/MyCOSXmlSignSourceProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public <T> String source(HttpRequest<T> request) throws QCloudClientException {
179179
// 默认URL参数字段参与计算,需要减去设置的不需要签名的 params
180180
if (parametersRequiredToSign.size() < 1) {
181181
Map<String, List<String>> queryNameValues = QCloudHttpUtils.getQueryPair(request.url());
182-
for (String noSignParam : request.getNoSignHeaders()) {
182+
for (String noSignParam : request.getNoSignParams()) {
183183
queryNameValues.remove(QCloudHttpUtils.urlDecodeString(noSignParam));
184184
}
185185
parametersRequiredToSign.addAll(queryNameValues.keySet());

QCloudCosXml/cos-android/src/androidTest/java/com/tencent/cos/xml/server_sign/MyCOSXmlSignSourceProvider.java

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@
3131
import android.util.Log;
3232

3333
import com.tencent.qcloud.core.auth.AuthConstants;
34-
import com.tencent.qcloud.core.auth.QCloudCredentials;
35-
import com.tencent.qcloud.core.auth.QCloudSignSourceProvider;
3634
import com.tencent.qcloud.core.auth.Utils;
3735
import com.tencent.qcloud.core.common.QCloudClientException;
3836
import com.tencent.qcloud.core.http.HttpConfiguration;
3937
import com.tencent.qcloud.core.http.HttpRequest;
40-
import com.tencent.qcloud.core.http.QCloudHttpRequest;
4138
import com.tencent.qcloud.core.util.QCloudHttpUtils;
4239
import com.tencent.qcloud.core.util.QCloudStringUtils;
4340

@@ -57,18 +54,19 @@
5754
import java.util.TreeSet;
5855

5956
/**
57+
* 签名物料source计算器
6058
* 提供COS请求中参与签名的字段
6159
* <p>
6260
* 具体请参考:<a href="https://cloud.tencent.com/document/product/436/7778#.E7.AD.BE.E5.90.8D.E6.AD.A5.E9.AA.A4">签名步骤</a>中的 步骤6:生成 StringToSign
6361
*/
6462

65-
public class MyCOSXmlSignSourceProvider implements QCloudSignSourceProvider {
63+
public class MyCOSXmlSignSourceProvider {
6664

67-
private Set<String> parametersRequiredToSign;
68-
private Set<String> parametersSigned;
65+
private final Set<String> parametersRequiredToSign;
66+
private final Set<String> parametersSigned;
6967

70-
private Set<String> headerKeysRequiredToSign;
71-
private Set<String> headerKeysSigned;
68+
private final Set<String> headerKeysRequiredToSign;
69+
private final Set<String> headerKeysSigned;
7270
private Map<String, List<String>> headerPairs;
7371

7472
private String signTime;
@@ -85,35 +83,12 @@ public MyCOSXmlSignSourceProvider() {
8583
noSignParams = new HashSet<>();
8684
}
8785

88-
public void parameter(String key) {
89-
parametersRequiredToSign.add(key);
90-
}
91-
92-
public void parameters(Set<String> keys) {
93-
if(keys != null){
94-
parametersRequiredToSign.addAll(keys);
95-
}
96-
}
97-
98-
public void header(String key) {
99-
headerKeysRequiredToSign.add(key);
100-
}
101-
10286
public void headers(Set<String> keys) {
10387
if(keys != null){
10488
headerKeysRequiredToSign.addAll(keys);
10589
}
10690
}
10791

108-
/**
109-
* 设置签名使用的 headers 参数,默认读取的是 {@link QCloudHttpRequest#headers()}
110-
*
111-
* @param headerPairs 键值对,签名使用的 headers 参数
112-
*/
113-
public void setHeaderPairsForSign(Map<String, List<String>> headerPairs) {
114-
this.headerPairs = headerPairs;
115-
}
116-
11792
void setSignTime(String signTime) {
11893
this.signTime = signTime;
11994
}
@@ -126,12 +101,6 @@ public void addNoSignParam(String key){
126101
noSignParams.add(key);
127102
}
128103

129-
@Override
130-
public <T> void onSignRequestSuccess(HttpRequest<T> request, QCloudCredentials credentials,
131-
String authorization) {
132-
133-
}
134-
135104
// 只会签名如下 header
136105
private final List<String> needToSignHeaders = Arrays.asList(
137106
"cache-control",
@@ -152,7 +121,6 @@ public <T> void onSignRequestSuccess(HttpRequest<T> request, QCloudCredentials c
152121
"transfer-encoding"
153122
);
154123

155-
@Override
156124
public <T> String source(HttpRequest<T> request) throws QCloudClientException {
157125
if (request == null) {
158126
return null;
@@ -187,7 +155,7 @@ public <T> String source(HttpRequest<T> request) throws QCloudClientException {
187155
// 默认URL参数字段参与计算,需要减去设置的不需要签名的 params
188156
if (parametersRequiredToSign.size() < 1) {
189157
Map<String, List<String>> queryNameValues = QCloudHttpUtils.getQueryPair(request.url());
190-
for (String noSignParam : request.getNoSignHeaders()) {
158+
for (String noSignParam : request.getNoSignParams()) {
191159
queryNameValues.remove(QCloudHttpUtils.urlDecodeString(noSignParam));
192160
}
193161
for (String noSignParam : noSignParams) {

QCloudCosXml/cos-android/src/androidTest/java/com/tencent/cos/xml/server_sign/MyCOSXmlSigner.java

Lines changed: 124 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import com.tencent.qcloud.core.common.QCloudAuthenticationException;
2929
import com.tencent.qcloud.core.common.QCloudClientException;
3030

31+
import java.util.List;
3132
import java.util.Locale;
33+
import java.util.Map;
3234

3335
/**
3436
* COS签名器<br>
@@ -38,19 +40,134 @@
3840
*/
3941
public class MyCOSXmlSigner {
4042
/**
41-
* 写死的签名
43+
* 临时秘钥
4244
*/
4345
public static final SessionQCloudCredentials credentials = new SessionQCloudCredentials(
44-
"xxxxxxxxxxxxxxxxxxxxxxxxx",
45-
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
46-
"xxxxxxxxxxxxxxxxxxxxxxxxxx",
47-
1111111111, 222222222
46+
"xxxxxxxxxxxxxxxxxxxxxxxx",
47+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
48+
"xxxxxxxxxxxxxxxxxxxxxxx",
49+
111111111, 11111111
4850
);
4951

50-
public static MyQCloudSelfSigner.SignResult sign(String source, String realHeaderList, String realParameterList) throws QCloudClientException{
52+
/**
53+
* 简单上传最大size
54+
* 限定上传文件大小最大值,单位Byte。大于限制上传文件大小的最大值会被判为上传失败
55+
*/
56+
public final static long SIMPLE_MAX_SIZE = 100 * 1024 * 1024;
57+
/**
58+
* 简单上传最大size header key
59+
*/
60+
public final static String SIMPLE_MAX_SIZE_HEADER_KEY = "x-cos-fsize-max";
61+
/**
62+
* Upload Part最大size
63+
* 用于UploadPart,限定上传分块大小最大值,单位 Byte。大于限制上传文件大小的最大值会被判为上传失败
64+
*/
65+
public final static long UPLOAD_PART_MAX_SIZE = 1024 * 1024;
66+
/**
67+
* Upload Part最大size header key
68+
*/
69+
public final static String UPLOAD_PART_MAX_SIZE_HEADER_KEY = "x-cos-psize-max";
70+
/**
71+
* CompleteMultipartUpload最多分块数量
72+
* 用于CompleteMultipartUpload,限定上传分块的数量。分块数量超过限制,请求会被拒绝。
73+
*/
74+
public final static int Complete_MAX_NUM = 100;
75+
/**
76+
* CompleteMultipartUpload最多分块数量 header key
77+
*/
78+
public final static String Complete_MAX_NUM_HEADER_KEY = "x-cos-pnum-max";
79+
80+
/**
81+
* 限定放通或不放通若干类型的 mimetype(可以限制上传文件的类型。一些比较常用的类型可以,特别少见的类型可能不太准确)
82+
*/
83+
// public final static String MIME_LIMIT = "text/plain;img/jpg;img/*";
84+
public final static String MIME_LIMIT = "!text/plain";
85+
86+
/**
87+
* mimetype header key
88+
*/
89+
public final static String MIME_LIMIT_HEADER_KEY = "x-cos-mime-limit";
90+
91+
/**
92+
* 签名方法
93+
* @param source 签名物料 采用了客户端拼source的方法,原因是需要端侧需要根据source进行分块上传签名缓存,如果服务端拼source的话可能和端侧不一致导致缓存失效
94+
* @param realHeaderList 要签名的header key集合字符串
95+
* @param realParameterList 要签名的param key集合字符串
96+
* @param httpMethod http请求方法
97+
* @param path http请求path
98+
* @param headers http请求header map
99+
* @param queryNameValues http请求query map
100+
* @return 签名结果
101+
*/
102+
public static MyQCloudSelfSigner.SignResult sign(String source, String realHeaderList, String realParameterList,
103+
String httpMethod, String path,
104+
Map<String, List<String>> headers, Map<String, List<String>> queryNameValues) throws QCloudClientException{
51105
if (credentials == null) {
52106
throw new QCloudClientException(new QCloudAuthenticationException("Credentials is null."));
53107
}
108+
109+
// 上传各api接口格式请参考:
110+
// PUT Object (简单上传) https://cloud.tencent.com/document/product/436/7749
111+
// Initiate Multipart Upload(初始化分块上传) https://cloud.tencent.com/document/product/436/7746
112+
// List Parts(查询特定分块上传中的已上传的块) https://cloud.tencent.com/document/product/436/7747
113+
// Upload Part(将对象按照分块的方式上传到 COS) https://cloud.tencent.com/document/product/436/7750
114+
// Complete Multipart Upload(完成整个分块上传) https://cloud.tencent.com/document/product/436/7742
115+
// Abort Multipart Upload(舍弃一个分块上传并删除已上传的块) https://cloud.tencent.com/document/product/436/7740
116+
117+
// --------------- 处理业务逻辑 开始 ------------------
118+
// 如果是简单Put或Upload Part请求
119+
if("put".equals(httpMethod)){
120+
if(!queryNameValues.containsKey("partNumber")){
121+
// 简单put
122+
// 限制大小 判断x-cos-fsize-max是否存在并限制
123+
if(headers.containsKey(SIMPLE_MAX_SIZE_HEADER_KEY)){
124+
long simpleMaxSize = Long.parseLong(headers.get(SIMPLE_MAX_SIZE_HEADER_KEY).get(0));
125+
if(simpleMaxSize > SIMPLE_MAX_SIZE){
126+
throw new QCloudClientException(new QCloudAuthenticationException("x-cos-fsize-max exceed the limit"));
127+
}
128+
} else {
129+
throw new QCloudClientException(new QCloudAuthenticationException("x-cos-fsize-max can not be empty"));
130+
}
131+
} else {
132+
// Upload Part
133+
// 限制大小 判断x-cos-psize-max是否存在并限制
134+
if(headers.containsKey(UPLOAD_PART_MAX_SIZE_HEADER_KEY)){
135+
long uploadPartMaxSize = Long.parseLong(headers.get(UPLOAD_PART_MAX_SIZE_HEADER_KEY).get(0));
136+
if(uploadPartMaxSize > UPLOAD_PART_MAX_SIZE){
137+
throw new QCloudClientException(new QCloudAuthenticationException("x-cos-psize-max exceed the limit"));
138+
}
139+
} else {
140+
throw new QCloudClientException(new QCloudAuthenticationException("x-cos-psize-max can not be empty"));
141+
}
142+
}
143+
144+
// 限制格式 判断x-cos-mime-limit是否存在并限制
145+
if(headers.containsKey(MIME_LIMIT_HEADER_KEY)){
146+
String mimeLimit = (headers.get(MIME_LIMIT_HEADER_KEY).get(0));
147+
if(!MIME_LIMIT.equals(mimeLimit)){
148+
throw new QCloudClientException(new QCloudAuthenticationException("x-cos-mime-limit incorrect"));
149+
}
150+
} else {
151+
throw new QCloudClientException(new QCloudAuthenticationException("x-cos-mime-limit can not be empty"));
152+
}
153+
}
154+
// 如果是CompleteMultipartUpload请求 限制大小 判断x-cos-pnum-max是否存在并限制
155+
if("post".equals(httpMethod) && queryNameValues.containsKey("uploadId")){
156+
if(headers.containsKey(Complete_MAX_NUM_HEADER_KEY)){
157+
long completeMaxNum = Integer.parseInt(headers.get(Complete_MAX_NUM_HEADER_KEY).get(0));
158+
if(completeMaxNum > Complete_MAX_NUM){
159+
throw new QCloudClientException(new QCloudAuthenticationException("x-cos-pnum-max exceed the limit"));
160+
}
161+
} else {
162+
throw new QCloudClientException(new QCloudAuthenticationException("x-cos-pnum-max can not be empty"));
163+
}
164+
}
165+
166+
167+
// 可以使用httpMethod、path、headers、queryNameValues做其他业务处理
168+
// --------------- 处理业务逻辑 结束 ------------------
169+
170+
// --------------- 计算签名开始 ------------------
54171
StringBuilder authorization = new StringBuilder();
55172

56173
String keyTime = credentials.getKeyTime();
@@ -69,6 +186,7 @@ public static MyQCloudSelfSigner.SignResult sign(String source, String realHeade
69186
.append(realParameterList.toLowerCase(Locale.ROOT)).append("&")
70187
.append(AuthConstants.Q_SIGNATURE).append("=").append(signature);
71188
String auth = authorization.toString();
189+
// --------------- 计算签名结束 ------------------
72190
return new MyQCloudSelfSigner.SignResult(auth, credentials.getToken());
73191
}
74192

QCloudCosXml/cos-android/src/androidTest/java/com/tencent/cos/xml/server_sign/MyQCloudSelfSigner.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66
import com.tencent.qcloud.core.common.QCloudClientException;
77
import com.tencent.qcloud.core.http.HttpConstants;
88
import com.tencent.qcloud.core.http.QCloudHttpRequest;
9+
import com.tencent.qcloud.core.util.QCloudHttpUtils;
910

1011
import java.util.HashMap;
1112
import java.util.Iterator;
13+
import java.util.List;
14+
import java.util.Locale;
1215
import java.util.Map;
1316

1417
/**
18+
* 实现自签名器
1519
* <p>
1620
* Created by jordanqin on 2023/8/11 17:20.
17-
* Copyright 2010-2020 Tencent Cloud. All Rights Reserved.
21+
* Copyright 2010-2023 Tencent Cloud. All Rights Reserved.
1822
*/
1923
public class MyQCloudSelfSigner implements QCloudSelfSigner {
2024
private static final String TAG = "MyQCloudSelfSigner";
@@ -41,7 +45,7 @@ public void sign(QCloudHttpRequest request) throws QCloudClientException {
4145
int sourceId = source.hashCode();
4246
SignResult signResult = lookupValidSignResult(sourceId);
4347
if (signResult == null) {
44-
signResult = fetchNewSignResult(sourceProvider, source);
48+
signResult = fetchNewSignResult(sourceProvider, source, request);
4549
Log.d(TAG, signResult.authorization);
4650
cacheSignResultAndCleanUp(sourceId, signResult);
4751
}
@@ -58,9 +62,17 @@ public void sign(QCloudHttpRequest request) throws QCloudClientException {
5862
/**
5963
* 远程获取签名结果
6064
*/
61-
private SignResult fetchNewSignResult(MyCOSXmlSignSourceProvider sourceProvider, String source) throws QCloudClientException {
65+
private SignResult fetchNewSignResult(MyCOSXmlSignSourceProvider sourceProvider, String source, QCloudHttpRequest request) throws QCloudClientException {
6266
Log.d(TAG, "fetchNewSignResult");
63-
return MyCOSXmlSigner.sign(source, sourceProvider.getRealHeaderList(), sourceProvider.getRealParameterList());
67+
68+
// 获取请求内容传给服务端,用于做业务处理
69+
String httpMethod = request.method().toLowerCase(Locale.ROOT);
70+
String path = QCloudHttpUtils.urlDecodeString(request.url().getPath());
71+
Map<String, List<String>> headers = request.headers();
72+
Map<String, List<String>> queryNameValues = QCloudHttpUtils.getQueryPair(request.url());
73+
74+
return MyCOSXmlSigner.sign(source, sourceProvider.getRealHeaderList(), sourceProvider.getRealParameterList(),
75+
httpMethod, path, headers, queryNameValues);
6476
}
6577

6678
/**

0 commit comments

Comments
 (0)