Bläddra i källkod

使用 POST 上传对象

reghao 2 år sedan
förälder
incheckning
b47dee4450
1 ändrade filer med 37 tillägg och 1 borttagningar
  1. 37 1
      oss-sdk/src/test/java/ObjectTest.java

+ 37 - 1
oss-sdk/src/test/java/ObjectTest.java

@@ -1,7 +1,11 @@
 import cn.reghao.jutil.jdk.converter.ByteHex;
+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.security.Base64Util;
 import cn.reghao.jutil.jdk.security.DigestUtil;
+import cn.reghao.jutil.tool.http.DefaultWebRequest;
 import com.amazonaws.auth.AWSStaticCredentialsProvider;
 import com.amazonaws.auth.BasicAWSCredentials;
 import com.amazonaws.client.builder.AwsClientBuilder;
@@ -11,6 +15,8 @@ import com.amazonaws.services.s3.model.*;
 import lombok.extern.slf4j.Slf4j;
 
 import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.*;
 
 /**
@@ -49,6 +55,35 @@ public class ObjectTest {
         System.out.println();
     }
 
+    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 contentType = Files.probeContentType(Path.of(file.getAbsolutePath()));
+        String api = "http://localhost:8010/";
+        UploadParam uploadParam = new UploadParam(file, contentType);
+        Map<String, String> map = new HashMap<>();
+        map.put("key", key);
+        map.put("acl", "public-read");
+        map.put("successActionRedirect", "http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html");
+        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("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>");
+        uploadParam.setTextParams(map);
+
+        WebResponse webResponse = webRequest.upload(api, uploadParam);
+        int statusCode = webResponse.getStatusCode();
+    }
+
     static boolean objectExist(String bucketName, String objectName) {
         return s3Client.doesObjectExist(bucketName, objectName);
     }
@@ -142,7 +177,8 @@ public class ObjectTest {
         File file = new File(filePath);
         String key = String.format("a/%s", file.getName());
         //put(key, file);
-        upload(key, file);
+        //upload(key, file);
+        post(key, file);
 
         /*FileInputStream fis = new FileInputStream(filePath);
         String sha256Hex = DigestUtil.sha256sum(fis);