全部文档
当前文档

暂无内容

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

文档中心

JAVA

最近更新时间:2024-08-12 10:30:40

  • 配置pom.xml

    
    <dependency>
          <groupId>com.ksyun</groupId>
          <artifactId>ksyun-sdk-java-common</artifactId>
          <version>0.0.2</version>
    </dependency>
  • AWS签名demo

    package example;
    
    import common.Credential;
    import common.aws.AWS4EncryptionFactory;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    import java.net.URI;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    public class KsyunIamClientAws4 {
    
        private static final String AK = "Your AK"; // 请替换为您的AccessKey
        private static final String SK = "Your SK"; // 请替换为您的SecretKey
        private static final String SERVIER = "iam"; //访问的服务
        private static final String REGION = "cn-beijing-6"; //访问的区域
    
        private static final String API_URL = "http://iam.api.ksyun.com"; //服务地址
    
        private static final String HOST = "iam.api.ksyun.com"; // 请求头Host
    
        /**
         *
         * @param head 请求头
         * @param params 请求参数
         * @param requestMethod get|post
         */
        private static void enhanceAws4Signature(Map<String, String> head, Map<String, Object> params, String requestMethod) {
            Credential credential = new Credential(AK, SK, REGION);
            AWS4EncryptionFactory aws4EncryptionFactory = new AWS4EncryptionFactory(credential.getSecretKey(), credential.getSignStr(), SERVIER, credential.getRegion());
    
    
            //设置请求参数
            if (params != null) {
                params.entrySet().forEach(entry -> {
                    aws4EncryptionFactory.setParamMap(entry.getKey(), entry.getValue());
                });
            }
    
            //设置请求头
            if (head != null) {
                head.entrySet().forEach(entry -> {
                    aws4EncryptionFactory.setHeadMap(entry.getKey(), entry.getValue());
                });
            }
    
            //AWS 加密
            aws4EncryptionFactory.generateSignature(requestMethod);
    
            //回填AWS4 签名
            String authorization = aws4EncryptionFactory.getHead().get(AWS4EncryptionFactory.X_Authorization);
            String xAmzDate = aws4EncryptionFactory.getHead().get(AWS4EncryptionFactory.X_AMZ_DATA);
            head.put(AWS4EncryptionFactory.X_Authorization, authorization);
            head.put(AWS4EncryptionFactory.X_AMZ_DATA, xAmzDate);
        }
    
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> params = new HashMap<>();
            //固定参数
            params.put("Action", "ListUsers"); //接口名称
            params.put("Version", "2015-11-01"); //接口版本
    
            //接口参数
            params.put("Marker", "1");
            params.put("MaxItems", "100");
    
            Map<String, String> header = new HashMap<>();
            header.put("Accept", "application/json");
            header.put("Host", HOST);
    
            //AWS4
            enhanceAws4Signature(header, params, "get");
    
            // 拼接请求URL
            String queryString = params.entrySet().stream()
                    .map(entry -> entry.getKey() + "=" + entry.getValue())
                    .collect(Collectors.joining("&"));
            URI requestURL = new URI(API_URL + "?" + queryString);
    
    
            // 发送请求并处理响应
            HttpClient client = HttpClients.createDefault();
            HttpGet request = new HttpGet(requestURL.toString());
    
            //设置请求头
            header.entrySet().forEach(entry -> {
                request.setHeader(entry.getKey(), entry.getValue());
            });
    
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String responseBody = EntityUtils.toString(entity);
                System.out.println("Response Status: " + response.getStatusLine());
                System.out.println("Response Body: " + responseBody);
            }
        }
    }

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

纯净模式

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