全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

V2签名计算及使用(Java)

最近更新时间:2025-01-02 15:49:30

V2 签名计算方法

以下代码中有部分类来自KS3 Java SDK,如:AuthorizationRequestAuthUtils等,如果不想引用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);
    }

V2 签名使用示例

1. 计算并使用V2签名列举存储空间

    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);
    }

2. 计算并使用V2签名列举存储空间中的对象

    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);
    }

3. 计算并使用V2签名上传文件

    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);
    }

4. 计算并使用V2签名获取文件的metadata

    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);
    }

5. 计算并使用V2签名获取文件的ACL

    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);
    }

6. 计算并使用V2签名下载文件

    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);
    }

7. 计算并使用V2签名删除文件

    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);
    }

使用外链:

1. 管理外链

2. 通过外链上传文件

文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈