Parcourir la source

update oss-sdk

reghao il y a 2 ans
Parent
commit
08e445a143

+ 3 - 5
oss-sdk/src/main/java/cn/reghao/oss/sdk/ObjectBasicService.java

@@ -17,7 +17,6 @@ import java.net.http.HttpRequest;
 import java.net.http.HttpResponse;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.function.Supplier;
 
@@ -73,12 +72,11 @@ public class ObjectBasicService {
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
     }
 
-    public void postObject(String key, File file, String contentType)
+    public void postObject(File file, int channelId)
             throws URISyntaxException, IOException, InterruptedException {
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher()
                 .addPart("file", Paths.get(file.getAbsolutePath()))
-                .addPart("channelId", 1+"");
-                //.addPart("contentType", contentType);
+                .addPart("channelId", channelId+"");
 
         String api = String.format("%s/", endpoint);
         HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
@@ -115,6 +113,6 @@ public class ObjectBasicService {
     public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
         String filePath = "/home/reghao/Downloads/video.mp4";
         ObjectBasicService objectBasicService = new ObjectBasicService("http://localhost:8010");
-        objectBasicService.postObject("a/b/c", new File(filePath), "");
+        objectBasicService.postObject(new File(filePath), 1);
     }
 }

+ 127 - 37
oss-sdk/src/main/java/cn/reghao/oss/sdk/ObjectMultipartUploadService.java

@@ -1,18 +1,29 @@
 package cn.reghao.oss.sdk;
 
+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.DigestUtil;
-import cn.reghao.oss.sdk.model.UploadFilePart;
+import cn.reghao.jutil.jdk.serializer.JsonConverter;
+import cn.reghao.jutil.tool.http.DefaultWebRequest;
+import cn.reghao.oss.common.UploadFilePart;
+import cn.reghao.oss.common.UploadFileRet;
+import cn.reghao.oss.common.UploadPrepareRet;
+import cn.reghao.oss.common.UploadedPart;
+import com.google.gson.reflect.TypeToken;
 import lombok.extern.slf4j.Slf4j;
 
 import java.io.*;
+import java.lang.reflect.Type;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
 import java.net.http.HttpResponse;
-import java.nio.file.Path;
-import java.security.NoSuchAlgorithmException;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * @author reghao
@@ -23,21 +34,71 @@ public class ObjectMultipartUploadService {
     static String endpoint = "http://localhost:8010/";
     static FilePart filePart = new FilePart();
     static HttpClient httpClient = HttpClient.newBuilder().build();
+    static WebRequest webRequest = new DefaultWebRequest();
 
-    static void create() {
+    static void create() throws URISyntaxException, IOException, InterruptedException {
+        MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
+        publisher.addPart("filename", "1.zip")
+                .addPart("size", 1+"")
+                .addPart("sha256sum", "1234567890");
+
+        String authorization = "";
+        String api = "http://localhost:8010/?create";
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .version(HttpClient.Version.HTTP_1_1)
+                .header("Authorization", authorization)
+                .header("Content-Type", "multipart/form-data; boundary=" + publisher.getBoundary())
+                .POST(publisher.build())
+                .build();
+
+        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
+        String body = httpResponse.body();
+        Type type = new TypeToken<WebResult<UploadPrepareRet>>(){}.getType();
+        WebResult<UploadPrepareRet> webResult = JsonConverter.jsonToObject(body, type);
+        if (webResult.getCode() != 0) {
+            String errMsg = webResult.getMsg();
+        }
+
+        UploadPrepareRet uploadPrepareRet = webResult.getData();
+        System.out.println();
+    }
+
+    static void get() throws URISyntaxException, IOException, InterruptedException {
+        String authorization = "";
+        String api = "http://localhost:8010/?multipart";
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .version(HttpClient.Version.HTTP_1_1)
+                .header("Authorization", authorization)
+                //.header("Content-Type", "multipart/form-data; boundary=" + publisher.getBoundary())
+                .GET()
+                .build();
+
+        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
+        String body = httpResponse.body();
+        Type type = new TypeToken<WebResult<UploadedPart>>(){}.getType();
+        WebResult<UploadedPart> webResult = JsonConverter.jsonToObject(body, type);
+        if (webResult.getCode() != 0) {
+            String errMsg = webResult.getMsg();
+        }
 
+        UploadedPart uploadedPart = webResult.getData();
     }
 
-    static void postObject(UploadFilePart uploadFilePart)
+    /**
+     * jdk http 实现
+     * 存在 bug: https://bugs.openjdk.org/browse/JDK-8222968
+     *
+     * @param
+     * @return
+     * @date 2023-05-24 14:56:50
+     */
+    static void postObject1(byte[] bytes, UploadFilePart uploadFilePart)
             throws URISyntaxException, IOException, InterruptedException {
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
-        byte[] bytes = uploadFilePart.getBytes();
         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
-        publisher
-                .addPart("file", () -> bais, uploadFilePart.getFilename(), "")
+        publisher.addPart("file", () -> bais, uploadFilePart.getFilename(), "")
                 //.addPart("file", Path.of("/home/reghao/Downloads/video.mp4"))
                 .addPart("channelId", uploadFilePart.getChannelId()+"")
-                .addPart("pid", uploadFilePart.getPid())
                 .addPart("identifier", uploadFilePart.getIdentifier())
                 .addPart("filename", uploadFilePart.getFilename())
                 .addPart("relativePath", uploadFilePart.getRelativePath())
@@ -45,8 +106,7 @@ public class ObjectMultipartUploadService {
                 .addPart("chunkSize", uploadFilePart.getChunkSize()+"")
                 .addPart("currentChunkSize", uploadFilePart.getCurrentChunkSize()+"")
                 .addPart("totalChunks", uploadFilePart.getTotalChunks()+"")
-                .addPart("chunkNumber", uploadFilePart.getChunkNumber()+"")
-                .addPart("partSha256sum", uploadFilePart.getPartSha256sum());
+                .addPart("chunkNumber", uploadFilePart.getChunkNumber()+"");
 
         String authorization = "";
         String api = "http://localhost:8010/?multipart";
@@ -61,43 +121,73 @@ public class ObjectMultipartUploadService {
         System.out.println();
     }
 
-    static void upload(File file) throws IOException, NoSuchAlgorithmException, URISyntaxException, InterruptedException {
-        // 分段大小在 5MB - 5GB 之间,只有最后一个分段才允许小于 5MB,不可避免的
-        int minPartSize = filePart.getPartSize();
-        long len = file.length();
-        long total = len/minPartSize;
-        if (len % minPartSize != 0) {
-            total += 1;
+    static void postObject(byte[] bytes, UploadFilePart uploadFilePart) {
+        UploadParam uploadParam = new UploadParam(bytes, "");
+        Map<String, String> params = new HashMap<>();
+        params.put("channelId", uploadFilePart.getChannelId()+"");
+        params.put("identifier", uploadFilePart.getIdentifier());
+        params.put("filename", uploadFilePart.getFilename());
+        params.put("relativePath", uploadFilePart.getRelativePath());
+        params.put("totalSize", uploadFilePart.getTotalSize()+"");
+        params.put("chunkSize", uploadFilePart.getChunkSize()+"");
+        params.put("currentChunkSize", uploadFilePart.getCurrentChunkSize()+"");
+        params.put("totalChunks", uploadFilePart.getTotalChunks()+"");
+        params.put("chunkNumber", uploadFilePart.getChunkNumber()+"");
+        uploadParam.setTextParams(params);
+
+        String api = "http://localhost:8010/?multipart";
+        WebResponse webResponse = webRequest.upload(api, uploadParam);
+        int statusCode = webResponse.getStatusCode();
+        if (statusCode != 200) {
+            log.error("请求失败");
+        }
+
+        String body = webResponse.getBody();
+        Type type = new TypeToken<WebResult<UploadFileRet>>(){}.getType();
+        WebResult<UploadFileRet> webResult = JsonConverter.jsonToObject(body, type);
+        if (webResult.getCode() != 0) {
+            String errMsg = webResult.getMsg();
         }
 
-        UploadFilePart uploadFilePart = getUploadFilePart(file);
-        postObject(uploadFilePart);
-        for (int i = 0; i < total; i++) {
-            int start = i*minPartSize;
-            byte[] part = filePart.getPart(file.getAbsolutePath(), minPartSize, start);
+        UploadFileRet uploadFileRet = webResult.getData();
+        if (!uploadFileRet.isMerged()) {
+            log.info("继续上传");
+        } else {
+            log.info("分片上传并合并完成");
         }
     }
 
-    static UploadFilePart getUploadFilePart(File file) throws IOException, NoSuchAlgorithmException {
-        int channelId = 1;
-        String pid = "0";
+    static void upload(String pid, int channelId, File file) throws Exception {
         String identifier = DigestUtil.sha256sum(new FileInputStream(file));
         String filename = file.getName();
         String relativePath = file.getAbsolutePath();
+
         long totalSize = file.length();
-        long chunkSize = 1024*1024*10;
-        UploadFilePart uploadFilePart = new UploadFilePart(channelId, pid, identifier, filename, relativePath,
-                totalSize, chunkSize, 10);
-
-        uploadFilePart.setBytes(new byte[0]);
-        uploadFilePart.setCurrentChunkSize(1024);
-        uploadFilePart.setChunkNumber(1);
-        uploadFilePart.setPartSha256sum("abcdefg");
-        return uploadFilePart;
+        // 分段大小在 5MB - 5GB 之间,只有最后一个分段才允许小于 5MB,不可避免的
+        int chunkSize = filePart.getPartSize();
+        long totalChunks = totalSize/chunkSize;
+        if (totalSize % chunkSize != 0) {
+            totalChunks += 1;
+        }
+
+        for (long i = 0; i < totalChunks; i++) {
+            long start = i*chunkSize;
+            byte[] part = filePart.getPart(file.getAbsolutePath(), chunkSize, start);
+            long chunkNumber = i + 1;
+            int currentChunkSize = part.length;
+            UploadFilePart uploadFilePart = new UploadFilePart(pid, channelId, identifier, filename, relativePath,
+                    totalSize, chunkSize, totalChunks, chunkNumber, currentChunkSize);
+            postObject(part, uploadFilePart);
+        }
     }
 
     public static void main(String[] args) throws Exception {
-        String filePath = "/home/reghao/Downloads/video.mp4";
-        upload(new File(filePath));
+        String filePath = "/home/reghao/Downloads/abc.mp4";
+
+        String pid = "0";
+        int channelId = 1;
+        upload(pid, channelId, new File(filePath));
+        //create();
+        //get();
     }
 }