reghao преди 2 години
родител
ревизия
bc74d850b0
променени са 1 файла, в които са добавени 33 реда и са изтрити 15 реда
  1. 33 15
      oss-sdk/src/test/java/ObjectTest.java

+ 33 - 15
oss-sdk/src/test/java/ObjectTest.java

@@ -3,8 +3,10 @@ import cn.reghao.jutil.jdk.http.UploadParam;
 import cn.reghao.jutil.jdk.http.WebRequest;
 import cn.reghao.jutil.jdk.http.WebResponse;
 import cn.reghao.jutil.jdk.io.FilePart;
+import cn.reghao.jutil.jdk.result.WebResult;
 import cn.reghao.jutil.jdk.security.Base64Util;
 import cn.reghao.jutil.jdk.security.DigestUtil;
+import cn.reghao.jutil.jdk.serializer.JsonConverter;
 import cn.reghao.jutil.tool.http.DefaultWebRequest;
 import com.amazonaws.auth.AWSStaticCredentialsProvider;
 import com.amazonaws.auth.BasicAWSCredentials;
@@ -12,11 +14,15 @@ import com.amazonaws.client.builder.AwsClientBuilder;
 import com.amazonaws.services.s3.AmazonS3;
 import com.amazonaws.services.s3.AmazonS3ClientBuilder;
 import com.amazonaws.services.s3.model.*;
+import com.google.gson.reflect.TypeToken;
 import lombok.extern.slf4j.Slf4j;
 
 import java.io.*;
+import java.lang.reflect.Type;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
 import java.util.*;
 
 /**
@@ -26,15 +32,15 @@ import java.util.*;
 @Slf4j
 public class ObjectTest {
     static String region = "chengdu";
-    static String endpoint = "http://oss-cn-chengdu.reghao.cn/";
-    //static String endpoint = "http://localhost:8010/";
+    //static String endpoint = "http://oss.reghao.cn/";
+    static String endpoint = "http://localhost:8010/";
     static String bucketName = "tnb";
-    static String accessKey = "accesskey123456";
-    static String secretKey = "secretKey123456";
+    static String accessKeyId = "accesskey123456";
+    static String secretAccessKey = "secretKey123456";
     static AmazonS3 s3Client;
     static {
         s3Client = AmazonS3ClientBuilder.standard()
-                .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
+                .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKeyId, secretAccessKey)))
                 .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region))
                 .withPathStyleAccessEnabled(false)
                 .withChunkedEncodingDisabled(true)
@@ -57,9 +63,22 @@ public class ObjectTest {
 
     static WebRequest webRequest = new DefaultWebRequest();
     static void post(String key, File file) throws IOException {
-        String url = "http://localhost:8020";
-        WebResponse webResponse1 = webRequest.postFormData(url, new HashMap<>());
-        String policy = webResponse1.getBody();
+        String url = "http://localhost:8020/ajax/policy";
+        WebResponse webResponse1 = webRequest.get(url);
+        String body = webResponse1.getBody();
+        Type type = new TypeToken<WebResult<Map>>(){}.getType();
+        WebResult<Map> webResult = JsonConverter.jsonToObject(body, type);
+        Map map1 = webResult.getData();
+        String signature = (String) map1.get("signature");
+        String policy = (String) map1.get("policy");
+
+        // x-amz-credential header 的值
+        // 用于计算签名的凭据, 它提供访问密钥 ID 和范围信息, 标识签名对其有效的区域和服务
+        // 字符串格式:<your-access-key-id>/<date>/<aws-region>/<aws-service>/aws4_request
+        // 示例:AKIAIOSFODNN7EXAMPLE/20130728/us-east-1/s3/aws4_request
+        String credential = accessKeyId + "/" + LocalDate.now().toString().replace("-", "") + "/" + region + "/s3/aws4_request";
+        //String policy = "";
+        //String signature = "";
 
         String contentType = Files.probeContentType(Path.of(file.getAbsolutePath()));
         String api = "http://localhost:8010/";
@@ -71,13 +90,12 @@ public class ObjectTest {
         map.put("contentType", "image/jpeg");
         map.put("xAmzMetaUuid", "14365123651274");
         map.put("xAmzServerSideEncryption", "AES256");
-        map.put("xAmzCredential", "AKIAIOSFODNN7EXAMPLE/20151229/us-east-1/s3/aws4_request");
+        map.put("xAmzCredential", credential);
         map.put("xAmzAlgorithm", "AWS4-HMAC-SHA256");
         map.put("xAmzDate", "20151229T000000Z");
         map.put("xAmzMetaTag", "");
-        //map.put("policy", "<Base64-encoded policy string>");
-        map.put("policy", "policy");
-        map.put("xAmzSignature", "<signature-value>");
+        map.put("policy", policy);
+        map.put("xAmzSignature", signature);
         uploadParam.setTextParams(map);
 
         WebResponse webResponse = webRequest.upload(api, uploadParam);
@@ -175,10 +193,10 @@ public class ObjectTest {
     public static void main(String[] args) throws Exception {
         String filePath = "/home/reghao/Downloads/sxd.mp4";
         File file = new File(filePath);
-        String key = String.format("a/%s", file.getName());
-        //put(key, file);
+        String key = String.format("a/b/c/d/%s", file.getName());
+        put(key, file);
         //upload(key, file);
-        post(key, file);
+        //post(key, file);
 
         /*FileInputStream fis = new FileInputStream(filePath);
         String sha256Hex = DigestUtil.sha256sum(fis);