Преглед на файлове

更新 oss-common 和 oss-sdk

reghao преди 2 години
родител
ревизия
bef80058f4

+ 0 - 1
oss-common/src/main/java/cn/reghao/oss/common/UploadFilePart.java

@@ -16,7 +16,6 @@ import java.io.Serializable;
 public class UploadFilePart implements Serializable {
     private static final long serialVersionUID = 1L;
 
-    private String pid;
     private int channelId;
     // 文件标识
     private String identifier;

+ 19 - 25
oss-sdk/src/main/java/cn/reghao/oss/sdk/ObjectMultipartUploadService.java

@@ -27,23 +27,27 @@ import java.util.Map;
 
 /**
  * @author reghao
- * @date 2022-12-09 10:45:42
+ * @date 2023-06-02 16:19:04
  */
 @Slf4j
 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();
+    private final String endpoint;
+    private final FilePart filePart = new FilePart();
+    private final HttpClient httpClient = HttpClient.newBuilder().build();
+    private final WebRequest webRequest = new DefaultWebRequest();
 
-    static void create() throws URISyntaxException, IOException, InterruptedException {
+    public ObjectMultipartUploadService(String endpoint) {
+        this.endpoint = endpoint;
+    }
+
+    public 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";
+        String api = endpoint + "/?create";
         HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                 .version(HttpClient.Version.HTTP_1_1)
                 .header("Authorization", authorization)
@@ -63,9 +67,9 @@ public class ObjectMultipartUploadService {
         System.out.println();
     }
 
-    static void get() throws URISyntaxException, IOException, InterruptedException {
+    public void get() throws URISyntaxException, IOException, InterruptedException {
         String authorization = "";
-        String api = "http://localhost:8010/?multipart";
+        String api = endpoint + "/?multipart";
         HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                 .version(HttpClient.Version.HTTP_1_1)
                 .header("Authorization", authorization)
@@ -92,7 +96,7 @@ public class ObjectMultipartUploadService {
      * @return
      * @date 2023-05-24 14:56:50
      */
-    static void postObject1(byte[] bytes, UploadFilePart uploadFilePart)
+    public void postObject1(byte[] bytes, UploadFilePart uploadFilePart)
             throws URISyntaxException, IOException, InterruptedException {
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
@@ -109,7 +113,7 @@ public class ObjectMultipartUploadService {
                 .addPart("chunkNumber", uploadFilePart.getChunkNumber()+"");
 
         String authorization = "";
-        String api = "http://localhost:8010/?multipart";
+        String api = endpoint + "/?multipart";
         HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                 .version(HttpClient.Version.HTTP_1_1)
                 .header("Authorization", authorization)
@@ -121,7 +125,7 @@ public class ObjectMultipartUploadService {
         System.out.println();
     }
 
-    static void postObject(byte[] bytes, UploadFilePart uploadFilePart) {
+    public void postObject(byte[] bytes, UploadFilePart uploadFilePart) {
         UploadParam uploadParam = new UploadParam(bytes, "");
         Map<String, String> params = new HashMap<>();
         params.put("channelId", uploadFilePart.getChannelId()+"");
@@ -135,7 +139,7 @@ public class ObjectMultipartUploadService {
         params.put("chunkNumber", uploadFilePart.getChunkNumber()+"");
         uploadParam.setTextParams(params);
 
-        String api = "http://localhost:8010/?multipart";
+        String api = endpoint + "/?multipart";
         WebResponse webResponse = webRequest.upload(api, uploadParam);
         int statusCode = webResponse.getStatusCode();
         if (statusCode != 200) {
@@ -157,7 +161,7 @@ public class ObjectMultipartUploadService {
         }
     }
 
-    static void upload(String pid, int channelId, File file) throws Exception {
+    public void upload(File file, int channelId) throws Exception {
         String identifier = DigestUtil.sha256sum(new FileInputStream(file));
         String filename = file.getName();
         String relativePath = file.getAbsolutePath();
@@ -175,19 +179,9 @@ public class ObjectMultipartUploadService {
             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,
+            UploadFilePart uploadFilePart = new UploadFilePart(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/abc.mp4";
-
-        String pid = "0";
-        int channelId = 1;
-        upload(pid, channelId, new File(filePath));
-        //create();
-        //get();
-    }
 }

+ 1 - 1
oss-sdk/src/main/java/cn/reghao/oss/sdk/ObjectUploadService.java

@@ -22,7 +22,7 @@ import java.util.function.Supplier;
 
 /**
  * @author reghao
- * @date 2022-11-21 17:19:04
+ * @date 2023-06-02 16:19:04
  */
 @Slf4j
 public class ObjectUploadService {

+ 18 - 3
oss-sdk/src/test/java/ObjectTest.java

@@ -1,4 +1,5 @@
 import cn.reghao.oss.common.UploadFileRet;
+import cn.reghao.oss.sdk.ObjectMultipartUploadService;
 import cn.reghao.oss.sdk.ObjectUploadService;
 import lombok.extern.slf4j.Slf4j;
 
@@ -6,13 +7,14 @@ import java.io.*;
 
 /**
  * @author reghao
- * @date 2022-06-20 14:31:14
+ * @date 2023-06-02 16:19:04
  */
 @Slf4j
 public class ObjectTest {
-    static ObjectUploadService objectUploadService = new ObjectUploadService("https://oss.reghao.cn");
+    static final String endpoint = "https://oss.reghao.cn";
 
-    public static void main(String[] args) {
+    static void upload() {
+        ObjectUploadService objectUploadService = new ObjectUploadService(endpoint);
         String filePath = "/home/reghao/Downloads/sxd.mp4";
         File file = new File(filePath);
 
@@ -28,4 +30,17 @@ public class ObjectTest {
             log.info("{} -> {}", uploadFileRet.getUploadId(), uploadFileRet.getUrl());
         }
     }
+
+    static void multipartUpload() throws Exception {
+        ObjectMultipartUploadService multipartUploadService = new ObjectMultipartUploadService(endpoint);
+
+        String filePath = "/home/reghao/Downloads/abc.mp4";
+        int channelId = 1;
+        multipartUploadService.upload(new File(filePath), channelId);
+        //multipartUploadService.create();
+        //multipartUploadService.get();
+    }
+
+    public static void main(String[] args) {
+    }
 }