最近更新时间:2025-01-02 15:49:30
以下代码中有部分类来自KS3 Java SDK,如:Authorization
、Request
、AuthUtils
等,如果不想引用KS3 Java SDK,可将这些类复制到您的项目中并稍作修改即可使用。
/**
* 计算V2签名
*
* @param ak access key
* @param sk secret key
* @param endpoint endpoint
* @param bucketName bucket name
* @param objectKey object key
* @param requestMethod request method
* @param queryParams query parameters
* @param headers request headers
* @return authorization string
* @throws SignatureException if signature calculation fails
*/
public static String calculateAuthorization(String ak, String sk, String endpoint, String bucketName, String objectKey, String requestMethod, Map<String, String> queryParams, Map<String, String> headers) throws SignatureException {
if (StringUtils.isBlank(ak)) {
throw new IllegalArgumentException("access key is required");
}
if (StringUtils.isBlank(sk)) {
throw new IllegalArgumentException("secret key is required");
}
if (StringUtils.isBlank(requestMethod)) {
throw new IllegalArgumentException("request method is required");
}
// 请求头中的Date必须有,且格式为:Tue, 30 Nov 2021 06:29:38 GMT。若Date时间与KS3服务端时间相差15分钟以上,则KS3将返回403错误。
if (headers == null) {
headers = new HashMap<>();
}
String dateStr = headers.get(HttpHeaders.Date.toString());
// 如果客户端不支持发送Date请求头,则必须增加x-kss-date请求头,且格式与Date相同
if (StringUtils.isBlank(dateStr)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
if (entry.getKey().equalsIgnoreCase("x-kss-date")) {
dateStr = entry.getValue();
break;
}
}
}
if (StringUtils.isBlank(dateStr)) {
throw new IllegalArgumentException("Request header 'Date' is required");
}
Date date = DateUtils.convertStr2Date(dateStr, DateUtils.DATETIME_PROTOCOL.RFC1123);
if (date == null) {
throw new IllegalArgumentException("Invalid date format: " + dateStr);
}
Authorization authorization = new Authorization(ak, sk);
Request request = new Request();
request.setEndpoint(endpoint);
request.setBucket(bucketName);
request.setKey(objectKey);
request.setHeaders(headers);
request.setMethod(HttpMethod.valueOf(requestMethod.toUpperCase()));
if (queryParams != null) {
request.setQueryParams(queryParams);
}
return AuthUtils.calcAuthorization(authorization, request);
}
public void listBucketsSample() throws SignatureException {
String ak = "yourAccessKey";
String sk = "yourSecretKey";
String endpoint = "yourEndpoint";
String requestMethod = "GET";
String bucketName = "";
String objectKey = "";
Map<String, String> headers = new HashMap<>();
// 必须header: Date,如果客户端不支持发送Date请求头,则必须增加x-kss-date请求头,且格式与Date相同
headers.put(HttpHeaders.Date.toString(), DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// headers.put("x-kss-date", DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// 计算签名
String authorization = calculateAuthorization(ak, sk, endpoint, bucketName, objectKey, requestMethod, null, headers);
// 使用签名请求KS3,bucketName, objectKey, requestMethod, queryParams, headers 请务必与签名时保持一致!
// sendHttpRequest(endpoint, bucketName, objectKey, authorization, requestMethod, null, headers, null);
}
public void listObjectsSample() throws SignatureException {
String ak = "yourAccessKey";
String sk = "yourSecretKey";
String endpoint = "yourEndpoint";
String requestMethod = "GET";
String bucketName = "yourBucket";
String objectKey = "";
Map<String, String> queryParams = new HashMap<>();
queryParams.put("prefix", "test");
Map<String, String> headers = new HashMap<>();
// 必须header: Date,如果客户端不支持发送Date请求头,则必须增加x-kss-date请求头,且格式与Date相同
headers.put(HttpHeaders.Date.toString(), DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// headers.put("x-kss-date", DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// 计算签名
String authorization = calculateAuthorization(ak, sk, endpoint, bucketName, objectKey, requestMethod, queryParams, headers);
// 使用签名请求KS3,bucketName, objectKey, requestMethod, queryParams, headers 请务必与签名时保持一致!
// ByteArrayInputStream inputStream = new ByteArrayInputStream("this is a test file".getBytes(StandardCharsets.UTF_8));
// sendHttpRequest(endpoint, bucketName, objectKey, authorization, requestMethod, queryParams, headers, inputStream);
}
private void putObjectSample(String bucketName, String objectKey) throws SignatureException {
String ak = "yourAccessKey";
String sk = "yourSecretKey";
String endpoint = "yourEndpoint";
String requestMethod = "Put";
Map<String, String> headers = new HashMap<>();
// 必须header: Date,如果客户端不支持发送Date请求头,则必须增加x-kss-date请求头,且格式与Date相同
headers.put(HttpHeaders.Date.toString(), DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// headers.put("x-kss-date", DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// 可选header
headers.put(HttpHeaders.CannedAcl.toString(), "public-read");
headers.put(HttpHeaders.ContentType.toString(), "application/xml");
// 计算签名
String authorization = calculateAuthorization(ak, sk, endpoint, bucketName, objectKey, requestMethod, null, headers);
// 使用签名请求KS3,bucketName, objectKey, requestMethod, queryParams, headers请务必与签名时保持一致!
// sendHttpRequest(endpoint, bucketName, objectKey, authorization, requestMethod, null, headers, null);
}
private void headObjectSample(String bucketName, String objectKey) throws SignatureException {
String ak = "yourAccessKey";
String sk = "yourSecretKey";
String endpoint = "yourEndpoint";
String requestMethod = "head";
Map<String, String> headers = new HashMap<>();
// 必须header: Date,如果客户端不支持发送Date请求头,则必须增加x-kss-date请求头,且格式与Date相同
headers.put(HttpHeaders.Date.toString(), DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// headers.put("x-kss-date", DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// 计算签名
String authorization = calculateAuthorization(ak, sk, endpoint, bucketName, objectKey, requestMethod, null, headers);
// 使用签名请求KS3,bucketName, objectKey, requestMethod, queryParams, headers请务必与签名时保持一致!
// sendHttpRequest(endpoint, bucketName, objectKey, authorization, requestMethod, null, headers, null);
}
private void getObjectAclSample(String bucketName, String objectKey) throws SignatureException {
String ak = "yourAccessKey";
String sk = "yourSecretKey";
String endpoint = "yourEndpoint";
String requestMethod = "get";
Map<String, String> queryParams = new HashMap<>();
queryParams.put("acl", "");
Map<String, String> headers = new HashMap<>();
// 必须header: Date,如果客户端不支持发送Date请求头,则必须增加x-kss-date请求头,且格式与Date相同
headers.put(HttpHeaders.Date.toString(), DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// headers.put("x-kss-date", DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// 可选 header
headers.put(HttpHeaders.ContentType.toString(), "application/xml");
// 计算签名
String authorization = calculateAuthorization(ak, sk, endpoint, bucketName, objectKey, requestMethod, queryParams, headers);
// 使用签名请求KS3,bucketName, objectKey, requestMethod, queryParams, headers请务必与签名时保持一致!
// sendHttpRequest(endpoint, bucketName, objectKey, authorization, requestMethod, queryParams, headers, null);
}
private void getObjectSample(String bucketName, String objectKey) throws SignatureException {
String ak = "yourAccessKey";
String sk = "yourSecretKey";
String endpoint = "yourEndpoint";
String requestMethod = "get";
Map<String, String> queryParams = null;
Map<String, String> headers = new HashMap<>();
// 必须header: Date,如果客户端不支持发送Date请求头,则必须增加x-kss-date请求头,且格式与Date相同
headers.put(HttpHeaders.Date.toString(), DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// headers.put("x-kss-date", DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// 可选header
headers.put(HttpHeaders.ContentType.toString(), "application/xml");
// 计算签名
String authorization = calculateAuthorization(ak, sk, endpoint, bucketName, objectKey, requestMethod, queryParams, headers);
// 使用签名请求KS3,bucketName, objectKey, requestMethod, queryParams, headers请务必与签名时保持一致!
// sendHttpRequest(endpoint, bucketName, objectKey, authorization, requestMethod, queryParams, headers, null);
}
private void deleteObjectSample(String bucketName, String objectKey) throws SignatureException {
String ak = "yourAccessKey";
String sk = "yourSecretKey";
String endpoint = "yourEndpoint";
String requestMethod = "delete";
Map<String, String> queryParams = new HashMap<>();
Map<String, String> headers = new HashMap<>();
// 必须header: Date,如果客户端不支持发送Date请求头,则必须增加x-kss-date请求头,且格式与Date相同
headers.put(HttpHeaders.Date.toString(), DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// headers.put("x-kss-date", DateUtils.convertDate2Str(new Date(), DateUtils.DATETIME_PROTOCOL.RFC1123));
// 可选header
headers.put(HttpHeaders.ContentType.toString(), "application/xml");
// 计算签名
String authorization = calculateAuthorization(ak, sk, endpoint, bucketName, objectKey, requestMethod, queryParams, headers);
// 使用签名请求KS3,bucketName, objectKey, requestMethod, queryParams, headers 请务必与签名时保持一致!
// sendHttpRequest(endpoint, bucketName, objectKey, authorization, requestMethod, queryParams, headers, null);
}
纯净模式