Просмотр исходного кода

提供 OssConsoleClient 和 OssStoreClient 作为 client

reghao 2 лет назад
Родитель
Сommit
2ebe6971db

+ 2 - 2
oss-api/src/main/java/cn/reghao/oss/api/constant/ObjectType.java

@@ -8,12 +8,12 @@ import java.util.Map;
  * @date 2023-06-13 15:09:09
  */
 public enum ObjectType {
-    Dir(1000, ""),
+    Dir(1000, "目录"),
     Image(1001, "图片"),
     Video(1002, "视频"),
     Audio(1003, "音频"),
     Text(1004, "文本"),
-    Other(1005, "二进制");
+    Other(1005, "其他");
 
     private final int code;
     private final String desc;

+ 1 - 0
oss-api/src/main/java/cn/reghao/oss/api/constant/UploadChannel.java

@@ -7,6 +7,7 @@ import java.util.Map;
  * @author reghao
  * @date 2023-05-23 10:43:25
  */
+@Deprecated
 public enum UploadChannel {
     // 网盘上传(单个文件最大 20GiB)
     disk(1, "file/", 1024L*1024*1024*20),

+ 18 - 0
oss-api/src/main/java/cn/reghao/oss/api/dto/ObjectChannel.java

@@ -0,0 +1,18 @@
+package cn.reghao.oss.api.dto;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * @author reghao
+ * @date 2024-02-26 10:08:12
+ */
+@AllArgsConstructor
+@Getter
+public class ObjectChannel {
+    private String name;
+    private String prefix;
+    private Long maxSize;
+    private String contentType;
+    private String domain;
+}

+ 3 - 2
oss-api/src/main/java/cn/reghao/oss/api/iface/OssServerService.java

@@ -1,6 +1,6 @@
 package cn.reghao.oss.api.iface;
 
-import cn.reghao.oss.api.dto.ServerInfo;
+import cn.reghao.oss.api.dto.ObjectChannel;
 
 /**
  * @author reghao
@@ -8,5 +8,6 @@ import cn.reghao.oss.api.dto.ServerInfo;
  */
 @Deprecated
 public interface OssServerService {
-    ServerInfo getServerInfo(long userId, int channelId);
+    String getUploadToken(int channelId);
+    void createChannel(long userId, ObjectChannel channel);
 }

+ 2 - 3
oss-api/src/main/java/cn/reghao/oss/api/iface/media/ImageFileService.java

@@ -12,8 +12,7 @@ import java.util.Set;
 public interface ImageFileService {
     void deleteByObjectNames(List<String> objectNames);
     void deleteByImageFileIds(List<String> imageFileIds);
-    ImageUrlDto getImageUrl(int channelId, String imageFileId);
-    ImageUrlDto getImageUrl(String imageFileId, long loginUser, int channelId);
+    ImageUrlDto getImageUrl(String channelName, String imageFileId);
     List<ImageUrlDto> getImageUrls(Set<String> imageFileIds);
-    String getSignedUrl(String url, long loginUser, int channelId);
+    String getSignedUrl(String url);
 }

+ 0 - 145
oss-sdk/src/main/java/cn/reghao/oss/sdk/ObjectGetService.java

@@ -1,145 +0,0 @@
-package cn.reghao.oss.sdk;
-
-import cn.reghao.jutil.jdk.result.WebResult;
-import cn.reghao.jutil.jdk.serializer.JsonConverter;
-import cn.reghao.oss.api.rest.UploadFileRet;
-import com.google.gson.reflect.TypeToken;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.io.FileUtils;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.InputStream;
-import java.lang.reflect.Type;
-import java.net.URI;
-import java.net.http.HttpClient;
-import java.net.http.HttpRequest;
-import java.net.http.HttpResponse;
-
-/**
- * @author reghao
- * @date 2022-11-21 17:19:04
- */
-@Slf4j
-public class ObjectGetService {
-    private final String endpoint;
-    private final HttpClient httpClient = HttpClient.newBuilder().build();
-    private final String baseDir = "/opt/tmp/";
-
-    public ObjectGetService(String endpoint) {
-        this.endpoint = endpoint;
-    }
-
-    public void headObject(String objectName) {
-        try {
-            String api = String.format("%s/%s", endpoint, objectName);
-            HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
-                    .version(HttpClient.Version.HTTP_1_1)
-                    .method("HEAD", HttpRequest.BodyPublishers.noBody())
-                    .build();
-
-            HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    public boolean headObject1(String sha256sum) {
-        try {
-            String api = String.format("%s?sha256sum=%s", endpoint, sha256sum);
-            HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
-                    .version(HttpClient.Version.HTTP_1_1)
-                    .method("HEAD", HttpRequest.BodyPublishers.noBody())
-                    .build();
-
-            HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
-            int statusCode = httpResponse.statusCode();
-            if (statusCode == 200) {
-                return true;
-            } else if (statusCode == 404) {
-                return false;
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        return false;
-    }
-
-    public void getObject(String objectName) {
-        try {
-            String version = "1.0.0";
-            String api = String.format("%s/%s?client=%s", endpoint, objectName, version);
-            HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
-                    .version(HttpClient.Version.HTTP_1_1)
-                    .GET()
-                    .build();
-
-            HttpResponse<InputStream> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofInputStream());
-            int statusCode = httpResponse.statusCode();
-            if (statusCode == 200) {
-                String localPath = saveFile(httpResponse.body(), objectName);
-                log.info("saved to {}", localPath);
-            } else {
-                log.error("{}", statusCode);
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    private UploadFileRet getResult(HttpResponse<String> httpResponse) {
-        int statusCode = httpResponse.statusCode();
-        String body = httpResponse.body();
-        if (statusCode != 200) {
-            log.error("{} -> {}", statusCode, body);
-            return null;
-        }
-
-        Type type = new TypeToken<WebResult<UploadFileRet>>(){}.getType();
-        WebResult<UploadFileRet> webResult = JsonConverter.jsonToObject(body, type);
-        if (webResult.getCode() != 0) {
-            log.error("{}", webResult.getMsg());
-            return null;
-        }
-
-        return webResult.getData();
-    }
-
-    public String saveFile(InputStream in, String objectName) {
-        String filePath = baseDir + objectName;
-        File file = new File(filePath);
-        File parentDir = file.getParentFile();
-        try {
-            if (!parentDir.exists()) {
-                FileUtils.forceMkdir(parentDir);
-            }
-
-            FileOutputStream fos = new FileOutputStream(file);
-            // 1MiB
-            int len = 1024*1024*5;
-            byte[] buf = new byte[len];
-            int readLen;
-            while ((readLen = in.read(buf, 0, len)) != -1) {
-                fos.write(buf, 0, readLen);
-            }
-            fos.close();
-            return filePath;
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        return null;
-    }
-
-    public static void main(String[] args) {
-        String endpoint = "";
-        ObjectGetService objectGetService = new ObjectGetService(endpoint);
-
-        String objectName = "video/playback/28d0fd95e224499c9f2cf1d98b4551a5.flv";
-        objectGetService.getObject(objectName);
-
-        /*String sha256sum = "1234567890";
-        objectGetService.headObject1(sha256sum);*/
-    }
-}

+ 223 - 68
oss-sdk/src/main/java/cn/reghao/oss/sdk/OssClient.java → oss-sdk/src/main/java/cn/reghao/oss/sdk/OssConsoleClient.java

@@ -3,6 +3,7 @@ package cn.reghao.oss.sdk;
 import cn.reghao.jutil.jdk.result.WebResult;
 import cn.reghao.jutil.jdk.serializer.JsonConverter;
 import cn.reghao.oss.api.dto.DownloadUrl;
+import cn.reghao.oss.api.dto.ObjectChannel;
 import cn.reghao.oss.api.dto.ObjectInfo;
 import cn.reghao.oss.api.dto.ServerInfo;
 import cn.reghao.oss.api.dto.media.*;
@@ -21,19 +22,77 @@ import java.util.Set;
  * @author reghao
  * @date 2024-02-23 21:47:39
  */
-public class OssClient {
+public class OssConsoleClient {
+    private final HttpClient httpClient = HttpClient.newBuilder().build();
     private final String endpoint;
-    private final HttpClient httpClient;
+    private String token;
 
-    public OssClient(String endpoint) {
+    public OssConsoleClient(String endpoint, String accessKeyId, String accessKeySecret) throws Exception {
         this.endpoint = endpoint;
-        this.httpClient = HttpClient.newBuilder().build();
+        auth(accessKeyId, accessKeySecret);
     }
 
-    public ServerInfo getServerInfo(long userId, int channelId) throws Exception {
-        String api = String.format("%s/api/oss/server/info?userId=%s&channelId=%s", endpoint, userId, channelId);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-        HttpRequest httpRequest = builder.GET().build();
+    private void auth(String accessKeyId, String accessKeySecret) throws Exception {
+        MultiPartBodyPublisher publisher = new MultiPartBodyPublisher()
+                .addPart("accessKeyId", accessKeyId)
+                .addPart("accessKeySecret", accessKeySecret);
+
+        String api = String.format("%s/api/oss/key/auth", endpoint);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .version(HttpClient.Version.HTTP_1_1)
+                .header("content-type", "multipart/form-data; boundary=" + publisher.getBoundary())
+                .POST(publisher.build())
+                .build();
+
+        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
+        int statusCode = httpResponse.statusCode();
+        String body = httpResponse.body();
+        if (statusCode != 200) {
+            String errMsg = String.format("%s -> %s", statusCode, body);
+            throw new Exception(errMsg);
+        }
+
+        Type type = new TypeToken<WebResult<String>>(){}.getType();
+        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
+        if (webResult.getCode() != 0) {
+            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
+            throw new Exception(errMsg);
+        }
+
+        this.token = webResult.getData();
+    }
+
+    public void registerNode(String jsonPayload) throws Exception {
+        String api = String.format("%s/api/oss/store/node", endpoint);
+        HttpClient httpClient = HttpClient.newBuilder().build();
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .header("content-type", "application/json")
+                .POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
+                .build();
+        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
+        int statusCode = httpResponse.statusCode();
+        String body = httpResponse.body();
+        if (statusCode != 200) {
+            String errMsg = String.format("%s -> %s", statusCode, body);
+            throw new Exception(errMsg);
+        }
+
+        Type type = new TypeToken<WebResult<String>>(){}.getType();
+        WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
+        if (webResult.getCode() != 0) {
+            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
+            throw new Exception(errMsg);
+        }
+    }
+
+    public ServerInfo getServerInfo(int channelId) throws Exception {
+        String api = String.format("%s/api/oss/server/info?channelId=%s", endpoint, channelId);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -52,10 +111,63 @@ public class OssClient {
         return webResult.getData();
     }
 
+    public List<ObjectChannel> getObjectChannels(String nodeAddr) throws Exception {
+        String api = String.format("%s/api/oss/channels?nodeAddr=%s", endpoint, nodeAddr);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
+        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
+        int statusCode = httpResponse.statusCode();
+        String body = httpResponse.body();
+        if (statusCode != 200) {
+            String errMsg = String.format("%s -> %s", statusCode, body);
+            throw new Exception(errMsg);
+        }
+
+        Type type = new TypeToken<WebResult<List<ObjectChannel>>>(){}.getType();
+        WebResult<List<ObjectChannel>> webResult = JsonConverter.jsonToObject(body, type);
+        if (webResult.getCode() != 0) {
+            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
+            throw new Exception(errMsg);
+        }
+
+        return webResult.getData();
+    }
+
+    public ObjectChannel getObjectChannel(String nodeAddr, int channelId) throws Exception {
+        String api = String.format("%s/api/oss/channel?nodeAddr=%s&channelId=%s", endpoint, nodeAddr, channelId);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
+        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
+        int statusCode = httpResponse.statusCode();
+        String body = httpResponse.body();
+        if (statusCode != 200) {
+            String errMsg = String.format("%s -> %s", statusCode, body);
+            throw new Exception(errMsg);
+        }
+
+        Type type = new TypeToken<WebResult<ObjectChannel>>(){}.getType();
+        WebResult<ObjectChannel> webResult = JsonConverter.jsonToObject(body, type);
+        if (webResult.getCode() != 0) {
+            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
+            throw new Exception(errMsg);
+        }
+
+        return webResult.getData();
+    }
+
     public ObjectInfo getObjectInfo(String objectId) throws Exception {
         String api = String.format("%s/api/oss/object/info?objectId=%s", endpoint, objectId);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-        HttpRequest httpRequest = builder.GET().build();
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -76,8 +188,11 @@ public class OssClient {
 
     public DownloadUrl getDownloadUrl(String objectId, int channelId, long userId) throws Exception {
         String api = String.format("%s/api/oss/object/url?objectId=%s&channelId=%s&userId=%s", endpoint, objectId, channelId, userId);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-        HttpRequest httpRequest = builder.GET().build();
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -97,20 +212,22 @@ public class OssClient {
     }
 
     public void convertVideo(String videoFileId) throws Exception {
-        String api = String.format("%s/api/media/convert/video/%s", endpoint, videoFileId);
+        String api = String.format("%s/api/oss/media/convert/video/%s", endpoint, videoFileId);
         convert(api);
     }
 
     public void convertAudio(String audioFileId) throws Exception {
-        String api = String.format("%s/api/media/convert/audio/%s", endpoint, audioFileId);
+        String api = String.format("%s/api/oss/media/convert/audio/%s", endpoint, audioFileId);
         convert(api);
     }
 
     private void convert(String api) throws Exception {
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
-        HttpRequest httpRequest = builder.POST(publisher.build()).build();
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .POST(publisher.build())
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -129,14 +246,16 @@ public class OssClient {
 
     @Deprecated
     public void setVideoScope(String videoFileId, int scope) throws Exception {
-        String api = String.format("%s/api/media/scope/video", endpoint);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
         publisher.addPart("videoId", videoFileId);
         publisher.addPart("scope", scope+"");
 
-        HttpRequest httpRequest = builder.POST(publisher.build()).build();
+        String api = String.format("%s/api/oss/media/scope/video", endpoint);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .POST(publisher.build())
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -155,14 +274,16 @@ public class OssClient {
 
     @Deprecated
     public void setAudioScope(String audioFileId, int scope) throws Exception {
-        String api = String.format("%s/api/media/scope/audio", endpoint);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
         publisher.addPart("audioFileId", audioFileId);
         publisher.addPart("scope", scope+"");
 
-        HttpRequest httpRequest = builder.POST(publisher.build()).build();
+        String api = String.format("%s/api/oss/media/scope/audio", endpoint);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .POST(publisher.build())
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -181,14 +302,16 @@ public class OssClient {
 
     @Deprecated
     public void setImagesScope(Set<String> imageFileIds, int scope) throws Exception {
-        String api = String.format("%s/api/media/scope/image", endpoint);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-
+        String api = String.format("%s/api/oss/media/scope/image", endpoint);
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
         publisher.addPart("imageFileIds", imageFileIds.toString());
         publisher.addPart("scope", scope+"");
 
-        HttpRequest httpRequest = builder.POST(publisher.build()).build();
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .POST(publisher.build())
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -206,15 +329,17 @@ public class OssClient {
     }
 
     public void setObjectScope(int scope, String objectId, int contentType) throws Exception {
-        String api = String.format("%s/api/media/scope/object", endpoint);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-
+        String api = String.format("%s/api/oss/media/scope/object", endpoint);
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
         publisher.addPart("scope", scope+"");
         publisher.addPart("objectId", objectId);
         publisher.addPart("contentType", contentType+"");
 
-        HttpRequest httpRequest = builder.POST(publisher.build()).build();
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .POST(publisher.build())
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -232,15 +357,17 @@ public class OssClient {
     }
 
     public void setObjectsScope(int scope, List<String> objectIds, int contentType) throws Exception {
-        String api = String.format("%s/api/media/scope/objects", endpoint);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-
+        String api = String.format("%s/api/oss/media/scope/objects", endpoint);
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
         publisher.addPart("scope", scope+"");
         publisher.addPart("objectIds", objectIds.toString());
         publisher.addPart("contentType", contentType+"");
 
-        HttpRequest httpRequest = builder.POST(publisher.build()).build();
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .POST(publisher.build())
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -258,9 +385,12 @@ public class OssClient {
     }
 
     public AudioInfo getAudioInfo(String audioFileId) throws Exception {
-        String api = String.format("%s/api/media/audio/info/%s", endpoint, audioFileId);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-        HttpRequest httpRequest = builder.GET().build();
+        String api = String.format("%s/api/oss/media/audio/info/%s", endpoint, audioFileId);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -280,9 +410,12 @@ public class OssClient {
     }
 
     public List<AudioUrl> getAudioUrls(String audioFileId, long loginUser) throws Exception {
-        String api = String.format("%s/api/media/audio/url?audioFileId=%s&userId=%s", endpoint, audioFileId, loginUser);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-        HttpRequest httpRequest = builder.GET().build();
+        String api = String.format("%s/api/oss/media/audio/url?audioFileId=%s&userId=%s", endpoint, audioFileId, loginUser);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -302,7 +435,7 @@ public class OssClient {
     }
 
     public void deleteByObjectNames(List<String> objectNames) throws Exception {
-        String api = String.format("%s/api/media/image/delete/name", endpoint);
+        String api = String.format("%s/api/oss/media/image/delete/name", endpoint);
         HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
 
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
@@ -326,7 +459,7 @@ public class OssClient {
     }
 
     public void deleteByImageFileIds(List<String> imageFileIds) throws Exception {
-        String api = String.format("%s/api/media/image/delete/id", endpoint);
+        String api = String.format("%s/api/oss/media/image/delete/id", endpoint);
         HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
 
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
@@ -350,10 +483,13 @@ public class OssClient {
     }
 
     public ImageUrlDto getImageUrl(int channelId, String imageFileId) throws Exception {
-        String api = String.format("%s/api/media/image/url?channelId=%s&imageFileId=%s", endpoint, channelId, imageFileId);
-        //String api = String.format("%s/api/media/image/url?channelId=%s&imageFileId=%s&loginUser=%s", endpoint, channelId, imageFileId);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-        HttpRequest httpRequest = builder.GET().build();
+        String api = String.format("%s/api/oss/media/image/url?channelId=%s&imageFileId=%s", endpoint, channelId, imageFileId);
+        //String api = String.format("%s/api/oss/media/image/url?channelId=%s&imageFileId=%s&loginUser=%s", endpoint, channelId, imageFileId);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -373,9 +509,12 @@ public class OssClient {
     }
 
     public ImageUrlDto getImageUrl(String imageFileId, long loginUser, int channelId) throws Exception {
-        String api = String.format("%s/api/media/image/url?channelId=%s&imageFileId=%s&loginUser=%s", endpoint, channelId, imageFileId, loginUser);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-        HttpRequest httpRequest = builder.GET().build();
+        String api = String.format("%s/api/oss/media/image/url?channelId=%s&imageFileId=%s&loginUser=%s", endpoint, channelId, imageFileId, loginUser);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -395,9 +534,12 @@ public class OssClient {
     }
 
     public List<ImageUrlDto> getImageUrls(Set<String> imageFileIds) throws Exception {
-        String api = String.format("%s/api/media/image/urls?imageFileIds=%s", endpoint, imageFileIds.toString());
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-        HttpRequest httpRequest = builder.GET().build();
+        String api = String.format("%s/api/oss/media/image/urls?imageFileIds=%s", endpoint, imageFileIds.toString());
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -417,9 +559,12 @@ public class OssClient {
     }
 
     public String getSignedUrl(String url, long loginUser, int channelId) throws Exception {
-        String api = String.format("%s/api/media/image/signedurl?channelId=%s&url=%s&loginUser=%s", endpoint, channelId, url, loginUser);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-        HttpRequest httpRequest = builder.GET().build();
+        String api = String.format("%s/api/oss/media/image/signedurl?channelId=%s&url=%s&loginUser=%s", endpoint, channelId, url, loginUser);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -439,7 +584,7 @@ public class OssClient {
     }
 
     public void deleteVideoFile(String videoFileId) throws Exception {
-        String api = String.format("%s/api/media/video/delete/%s", endpoint, videoFileId);
+        String api = String.format("%s/api/oss/media/video/delete/%s", endpoint, videoFileId);
         HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
         HttpRequest httpRequest = builder.DELETE().build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
@@ -459,9 +604,12 @@ public class OssClient {
     }
 
     public VideoInfo getVideoInfo(String videoFileId) throws Exception {
-        String api = String.format("%s/api/media/video/info/%s", endpoint, videoFileId);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-        HttpRequest httpRequest = builder.GET().build();
+        String api = String.format("%s/api/oss/media/video/info/%s", endpoint, videoFileId);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -481,9 +629,12 @@ public class OssClient {
     }
 
     public List<VideoUrlDto> getVideoUrls(String videoFileId, long loginUser) throws Exception {
-        String api = String.format("%s/api/media/video/url?videoFileId=%s&userId=%s", endpoint, videoFileId, loginUser);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-        HttpRequest httpRequest = builder.GET().build();
+        String api = String.format("%s/api/oss/media/video/url?videoFileId=%s&userId=%s", endpoint, videoFileId, loginUser);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
@@ -502,10 +653,14 @@ public class OssClient {
         return webResult.getData();
     }
 
+    @Deprecated
     public LocalDateTime getCreateTime(String videoFileId) throws Exception {
-        String api = String.format("%s/api/media/video/time/%s", endpoint, videoFileId);
-        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
-        HttpRequest httpRequest = builder.GET().build();
+        String api = String.format("%s/api/oss/media/video/time/%s", endpoint, videoFileId);
+        HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                .header("authorization", "Bearer " + token)
+                .version(HttpClient.Version.HTTP_1_1)
+                .GET()
+                .build();
         HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();

+ 95 - 10
oss-sdk/src/main/java/cn/reghao/oss/sdk/ObjectUploadService.java → oss-sdk/src/main/java/cn/reghao/oss/sdk/OssStoreClient.java

@@ -9,9 +9,11 @@ import cn.reghao.jutil.tool.http.DefaultWebRequest;
 import cn.reghao.oss.api.rest.UploadFileRet;
 import com.google.gson.reflect.TypeToken;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
 
 import java.io.BufferedInputStream;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.lang.reflect.Type;
 import java.net.URI;
@@ -29,12 +31,13 @@ import java.util.function.Supplier;
  * @date 2023-06-02 16:19:04
  */
 @Slf4j
-public class ObjectUploadService {
+public class OssStoreClient {
     private final String endpoint;
     private final HttpClient httpClient = HttpClient.newBuilder().build();
     private final DefaultWebRequest webRequest = new DefaultWebRequest();
+    private final String baseDir = "/opt/tmp/";
 
-    public ObjectUploadService(String endpoint) {
+    public OssStoreClient(String endpoint) {
         this.endpoint = endpoint;
     }
 
@@ -67,19 +70,18 @@ public class ObjectUploadService {
         return getResult(httpResponse);
     }
 
-    public UploadFileRet postObjectWithJdkHttp(File file, int channelId, long userId) throws Exception {
+    public UploadFileRet postObjectWithJdkHttp(File file, int channelId, String token) throws Exception {
         String sha256sum = DigestUtil.sha256sum(file.getAbsolutePath());
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher()
                 .addPart("file", Paths.get(file.getAbsolutePath()))
-                .addPart("channelId", ""+channelId)
-                .addPart("client", "client")
-                .addPart("sha256sum", sha256sum);
+                .addPart("client", "oss-sdk-1.0")
+                .addPart("sha256sum", sha256sum)
+                .addPart("channelId", channelId+"");
 
         String api = String.format("%s/", endpoint);
         HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
                 .version(HttpClient.Version.HTTP_1_1)
-                .header("x-user-id", userId+"")
-                //.header("authorization", "Bearer 0123456789")
+                .header("authorization", "Bearer " + token)
                 .header("content-type", "multipart/form-data; boundary=" + publisher.getBoundary())
                 .POST(publisher.build())
                 .build();
@@ -88,12 +90,11 @@ public class ObjectUploadService {
         return getResult(httpResponse);
     }
 
-    public UploadFileRet postObject(File file, int channelId, long userId) throws Exception {
+    public UploadFileRet postObject(File file, int channelId, String token) throws Exception {
         String sha256sum = DigestUtil.sha256sum(file.getAbsolutePath());
         Map<String, String> map = new HashMap<>();
         map.put("channelId", ""+channelId);
         map.put("client", "client");
-        map.put("userId", ""+userId);
         map.put("sha256sum", sha256sum);
         UploadParam uploadParam = new UploadParam(file, map);
 
@@ -132,4 +133,88 @@ public class ObjectUploadService {
 
         return webResult.getData();
     }
+
+    public void headObject(String objectName) {
+        try {
+            String api = String.format("%s/%s", endpoint, objectName);
+            HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                    .version(HttpClient.Version.HTTP_1_1)
+                    .method("HEAD", HttpRequest.BodyPublishers.noBody())
+                    .build();
+
+            HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public boolean headObject1(String sha256sum) {
+        try {
+            String api = String.format("%s?sha256sum=%s", endpoint, sha256sum);
+            HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                    .version(HttpClient.Version.HTTP_1_1)
+                    .method("HEAD", HttpRequest.BodyPublishers.noBody())
+                    .build();
+
+            HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
+            int statusCode = httpResponse.statusCode();
+            if (statusCode == 200) {
+                return true;
+            } else if (statusCode == 404) {
+                return false;
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return false;
+    }
+
+    public void getObject(String objectName) {
+        try {
+            String version = "1.0.0";
+            String api = String.format("%s/%s?client=%s", endpoint, objectName, version);
+            HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
+                    .version(HttpClient.Version.HTTP_1_1)
+                    .GET()
+                    .build();
+
+            HttpResponse<InputStream> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofInputStream());
+            int statusCode = httpResponse.statusCode();
+            if (statusCode == 200) {
+                String localPath = saveFile(httpResponse.body(), objectName);
+                log.info("saved to {}", localPath);
+            } else {
+                log.error("{}", statusCode);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public String saveFile(InputStream in, String objectName) {
+        String filePath = baseDir + objectName;
+        File file = new File(filePath);
+        File parentDir = file.getParentFile();
+        try {
+            if (!parentDir.exists()) {
+                FileUtils.forceMkdir(parentDir);
+            }
+
+            FileOutputStream fos = new FileOutputStream(file);
+            // 1MiB
+            int len = 1024*1024*5;
+            byte[] buf = new byte[len];
+            int readLen;
+            while ((readLen = in.read(buf, 0, len)) != -1) {
+                fos.write(buf, 0, readLen);
+            }
+            fos.close();
+            return filePath;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return null;
+    }
 }

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

@@ -1,8 +1,8 @@
-import cn.reghao.oss.api.constant.UploadChannel;
+import cn.reghao.oss.api.dto.ServerInfo;
 import cn.reghao.oss.api.rest.UploadFileRet;
 import cn.reghao.oss.sdk.ObjectMultipartUploadService;
-import cn.reghao.oss.sdk.ObjectUploadService;
-import cn.reghao.oss.sdk.OssClient;
+import cn.reghao.oss.sdk.OssStoreClient;
+import cn.reghao.oss.sdk.OssConsoleClient;
 import lombok.extern.slf4j.Slf4j;
 
 import java.io.*;
@@ -13,22 +13,9 @@ import java.io.*;
  */
 @Slf4j
 public class ObjectTest {
-    static final String endpoint = "";
-    static ObjectUploadService objectUploadService = new ObjectUploadService(endpoint);
-
-    static void upload(File file) throws Exception {
-        long userId = 10001;
-        UploadFileRet uploadFileRet = objectUploadService.postObject(file, UploadChannel.video.getCode(), userId);
-        //UploadFileRet uploadFileRet = objectUploadService.putObject(file, 1, userId);
-        if (uploadFileRet == null) {
-            log.info("文件上传失败");
-        } else {
-            log.info("{} -> {}", uploadFileRet.getUploadId(), uploadFileRet.getUrl());
-        }
-    }
-
     static void multipartUpload() throws Exception {
-        ObjectMultipartUploadService multipartUploadService = new ObjectMultipartUploadService(endpoint);
+        String storeEndpoint = "http://ossweb.reghao.cn";
+        ObjectMultipartUploadService multipartUploadService = new ObjectMultipartUploadService(storeEndpoint);
         String filePath = "";
         int channelId = 1;
         multipartUploadService.upload(new File(filePath), channelId);
@@ -37,10 +24,34 @@ public class ObjectTest {
     }
 
     public static void main(String[] args) throws Exception {
-        String filePath = "";
+        String consoleUrl = "http://ossweb.reghao.cn";
+        String accessKeyId = "QQ0LlqAJ";
+        String accessKeySecret = "gA1EBpn3SN9ay4EgaX";
+        OssConsoleClient ossConsoleClient = new OssConsoleClient(consoleUrl, accessKeyId, accessKeySecret);
+
+        int channelId = 5;
+        ServerInfo serverInfo = ossConsoleClient.getServerInfo(channelId);
+        if (serverInfo == null) {
+            log.info("获取 server_info 失败");
+            return;
+        }
+        String ossUrl = serverInfo.getOssUrl();
+        String token = serverInfo.getToken();
+
+        OssStoreClient ossStoreClient = new OssStoreClient(ossUrl);
+        String filePath = "/home/reghao/data/image/12.jpg";
         File file = new File(filePath);
-        //objectUploadService.postObjectWithJdkHttp(file, UploadChannel.image.getCode(), 10000);
-        String endpoint = "http://ossweb.reghao.cn";
-        OssClient ossClient = new OssClient(endpoint);
+        UploadFileRet uploadFileRet = ossStoreClient.postObjectWithJdkHttp(file, channelId, token);
+        if (uploadFileRet == null) {
+            log.info("文件上传失败");
+        } else {
+            log.info("{} -> {}", uploadFileRet.getUploadId(), uploadFileRet.getUrl());
+        }
+
+        /*String objectName = "video/playback/28d0fd95e224499c9f2cf1d98b4551a5.flv";
+        ossStoreClient.getObject(objectName);*/
+
+        /*String sha256sum = "1234567890";
+        storeClient.headObject1(sha256sum);*/
     }
 }