فهرست منبع

oss-sdk 对象上传失败直接抛出异常

reghao 2 سال پیش
والد
کامیت
2ffd76ad3a

+ 4 - 5
oss-sdk/src/main/java/cn/reghao/oss/sdk/ObjectGetService.java

@@ -136,11 +136,10 @@ public class ObjectGetService {
         String endpoint = "https://oss.reghao.cn";
         ObjectGetService objectGetService = new ObjectGetService(endpoint);
 
-        String objectName = "image/p/ff4a21bd93f94d6c834330a6f139d9f8.webp";
-        //objectGetService.getObject(objectName);
+        String objectName = "video/playback/28d0fd95e224499c9f2cf1d98b4551a5.flv";
+        objectGetService.getObject(objectName);
 
-        String sha256sum = "1234567890";
-        sha256sum = "f3622a93e86efec48c3dad1891f94823569e72dc81e149d4b7f521ab392995ef";
-        objectGetService.headObject1(sha256sum);
+        /*String sha256sum = "1234567890";
+        objectGetService.headObject1(sha256sum);*/
     }
 }

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

@@ -41,120 +41,96 @@ public class ObjectUploadService {
         this.endpoint = endpoint;
     }
 
-    public UploadFileRet putObject(File file, int channelId, long userId) {
-        try {
-            String sha256sum = DigestUtil.sha256sum(file.getAbsolutePath());
-            String api = String.format("%s/", endpoint);
-            HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api))
-                    .version(HttpClient.Version.HTTP_1_1)
-                    .header("x-content-sha256sum", sha256sum)
-                    .header("x-channel-id", channelId+"")
-                    .header("x-user-id", userId+"");
-
-            HttpRequest httpRequest = builder.PUT(HttpRequest.BodyPublishers.ofFile(Path.of(file.getAbsolutePath()))).build();
-            HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
-            return getResult(httpResponse);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        return null;
+    public UploadFileRet putObject(File file, int channelId, long userId) throws Exception {
+        String sha256sum = DigestUtil.sha256sum(file.getAbsolutePath());
+        String api = String.format("%s/", endpoint);
+        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api))
+                .version(HttpClient.Version.HTTP_1_1)
+                .header("x-content-sha256sum", sha256sum)
+                .header("x-channel-id", channelId+"")
+                .header("x-user-id", userId+"");
+
+        HttpRequest httpRequest = builder.PUT(HttpRequest.BodyPublishers.ofFile(Path.of(file.getAbsolutePath()))).build();
+        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
+        return getResult(httpResponse);
     }
 
-    public UploadFileRet putObject(InputStream inputStream, int channelId, int userId) {
-        try {
-            String api = String.format("%s/", endpoint);
-            HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api))
-                    .version(HttpClient.Version.HTTP_1_1)
-                    .header("x-content-sha256sum", "1234567890")
-                    .header("x-channel-id", channelId+"")
-                    .header("x-user-id", userId+"");
-
-            BufferedInputStream bis = new BufferedInputStream(inputStream);
-            Supplier<? extends InputStream> streamSupplier = (Supplier<BufferedInputStream>) () -> bis;
-            HttpRequest httpRequest = builder.PUT(HttpRequest.BodyPublishers.ofInputStream(streamSupplier)).build();
-            HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
-            return getResult(httpResponse);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
+    public UploadFileRet putObjectWithJdkHttp(InputStream inputStream, int channelId, int userId) throws Exception {
+        String api = String.format("%s/", endpoint);
+        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api))
+                .version(HttpClient.Version.HTTP_1_1)
+                .header("x-content-sha256sum", "1234567890")
+                .header("x-channel-id", channelId+"")
+                .header("x-user-id", userId+"");
+
+        BufferedInputStream bis = new BufferedInputStream(inputStream);
+        Supplier<? extends InputStream> streamSupplier = (Supplier<BufferedInputStream>) () -> bis;
+        HttpRequest httpRequest = builder.PUT(HttpRequest.BodyPublishers.ofInputStream(streamSupplier)).build();
+        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
+        return getResult(httpResponse);
+    }
 
-        return null;
+    public UploadFileRet postObjectWithJdkHttp(File file, int channelId, long userId) 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);
+
+        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("content-type", "multipart/form-data; boundary=" + publisher.getBoundary())
+                .POST(publisher.build())
+                .build();
+
+        HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
+        return getResult(httpResponse);
     }
 
-    public UploadFileRet postObject1(File file, int channelId, long userId) {
-        try {
-            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);
-
-            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("content-type", "multipart/form-data; boundary=" + publisher.getBoundary())
-                    .POST(publisher.build())
-                    .build();
-
-            HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
-            return getResult(httpResponse);
-        } catch (Exception e) {
-            e.printStackTrace();
+    public UploadFileRet postObject(File file, int channelId, long userId) 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);
+
+        String api = String.format("%s/", endpoint);
+        WebResponse webResponse = webRequest.upload(api, uploadParam);
+        int statusCode = webResponse.getStatusCode();
+        String body = webResponse.getBody();
+        if (statusCode != 200) {
+            String errMsg = String.format("%s -> %s", statusCode, body);
+            throw new Exception(errMsg);
         }
 
-        return null;
-    }
-
-    public UploadFileRet postObject(File file, int channelId, long userId) {
-        try {
-            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);
-
-            String api = String.format("%s/", endpoint);
-            WebResponse webResponse = webRequest.upload(api, uploadParam);
-            int statusCode = webResponse.getStatusCode();
-            String body = webResponse.getBody();
-            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();
-        } catch (Exception e) {
-            e.printStackTrace();
+        Type type = new TypeToken<WebResult<UploadFileRet>>(){}.getType();
+        WebResult<UploadFileRet> webResult = JsonConverter.jsonToObject(body, type);
+        if (webResult.getCode() != 0) {
+            throw new Exception(webResult.getMsg());
         }
-        return null;
+
+        return webResult.getData();
     }
 
-    private UploadFileRet getResult(HttpResponse<String> httpResponse) {
+    private UploadFileRet getResult(HttpResponse<String> httpResponse) throws Exception {
         int statusCode = httpResponse.statusCode();
         String body = httpResponse.body();
         if (statusCode != 200) {
-            log.error("{} -> {}", statusCode, body);
-            return null;
+            String errMsg = String.format("%s -> %s", statusCode, body);
+            throw new Exception(errMsg);
         }
 
         Type type = new TypeToken<WebResult<UploadFileRet>>(){}.getType();
         WebResult<UploadFileRet> webResult = JsonConverter.jsonToObject(body, type);
         if (webResult.getCode() != 0) {
-            log.error("{}", webResult.getMsg());
-            return null;
+            String errMsg = String.format("%s - %s", webResult.getCode(), webResult.getMsg());
+            throw new Exception(errMsg);
         }
 
         return webResult.getData();

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

@@ -15,7 +15,7 @@ public class ObjectTest {
     static final String endpoint = "https://oss.reghao.cn";
     static ObjectUploadService objectUploadService = new ObjectUploadService(endpoint);
 
-    static void upload(File file) {
+    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);
@@ -35,10 +35,9 @@ public class ObjectTest {
         //multipartUploadService.get();
     }
 
-    public static void main(String[] args) {
+    public static void main(String[] args) throws Exception {
         String filePath = "/home/reghao/Downloads/Screenshot_20230828_171528.jpeg";
         File file = new File(filePath);
-        UploadFileRet uploadFileRet = objectUploadService.postObject1(file, UploadChannel.image.getCode(), 10000);
-        System.out.println();
+        objectUploadService.postObjectWithJdkHttp(file, UploadChannel.image.getCode(), 10000);
     }
 }