Kaynağa Gözat

更新 oss-sdk 中的 getObject 方法

reghao 2 yıl önce
ebeveyn
işleme
6cc2526a24

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

@@ -5,7 +5,10 @@ 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;
@@ -21,6 +24,7 @@ import java.net.http.HttpResponse;
 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;
@@ -42,13 +46,21 @@ public class ObjectGetService {
 
     public void getObject(String objectName) {
         try {
-            String api = String.format("%s/%s", endpoint, objectName);
+            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();
         }
@@ -71,4 +83,38 @@ public class ObjectGetService {
 
         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 = "https://oss.reghao.cn";
+        ObjectGetService objectGetService = new ObjectGetService(endpoint);
+
+        String objectName = "image/p/ff4a21bd93f94d6c834330a6f139d9f8.webp";
+        objectGetService.getObject(objectName);
+    }
 }