2828import com .tencent .qcloud .core .common .QCloudAuthenticationException ;
2929import com .tencent .qcloud .core .common .QCloudClientException ;
3030
31+ import java .util .List ;
3132import java .util .Locale ;
33+ import java .util .Map ;
3234
3335/**
3436 * COS签名器<br>
3840 */
3941public 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
0 commit comments