最近更新时间:2023-01-05 19:37:22
1. 出现签名不匹配的错误,如何排查?
2. 自建签名服务器怎么算签名?
如果返回“SignatureDoesNotMatch”的错误码,说明签名出现错误。
请检查:
建议使用SDK进行签名,SDK文档:Java、PHP、Python、Android、IOS、JavaScript、Node.js、C#、C/C++、GO。
public void testUseServerSignature() throws SignatureException {
//此示例为分块上传第1步Initiate Multipart Upload
Authorization auth = new Authorization("youak", "yousk");
Request request = new Request();
request.setMethod(HttpMethod.POST);
request.setKey("testsignature");
request.setEndpoint("ks3-cn-beijing.ksyuncs.com"); //自己bucket所在endpoint
request.setBucket("yourbucket");
request.setRegion("BEIJING"); //自己bucket所在region
//设置url参数
Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("uploads", null);
request.setQueryParams(queryParamMap);
//设置header
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "binary/octet-stream");
String dateStr = DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123);
headers.put("Date", dateStr);
request.setHeaders(headers);
//传入请求参数,从服务端获取签名,AuthUtils工具类在java sdk中
String signature = AuthUtils.calcAuthorization(auth, request);
//实际发起请求
HttpPost httpPost = new HttpPost("http://" + request.getEndpoint() + "/" + request.getBucket() + "/" + request.getKey() + "?uploads");
//下面的header传入内容需要和计算签名时保持一致
httpPost.addHeader("Date", dateStr);
httpPost.addHeader("Authorization", signature);
httpPost.addHeader("Content-Type", "binary/octet-stream");
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse responseBody = httpclient.execute(httpPost);
System.out.println(JSON.toJSONString(responseBody));
System.out.println(responseBody.getStatusLine());
} catch (IOException e) {
e.printStackTrace();
}
}
纯净模式