|
@@ -5,7 +5,10 @@ import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
import cn.reghao.oss.api.rest.UploadFileRet;
|
|
import cn.reghao.oss.api.rest.UploadFileRet;
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
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.io.InputStream;
|
|
|
import java.lang.reflect.Type;
|
|
import java.lang.reflect.Type;
|
|
|
import java.net.URI;
|
|
import java.net.URI;
|
|
@@ -21,6 +24,7 @@ import java.net.http.HttpResponse;
|
|
|
public class ObjectGetService {
|
|
public class ObjectGetService {
|
|
|
private final String endpoint;
|
|
private final String endpoint;
|
|
|
private final HttpClient httpClient = HttpClient.newBuilder().build();
|
|
private final HttpClient httpClient = HttpClient.newBuilder().build();
|
|
|
|
|
+ private final String baseDir = "/opt/tmp/";
|
|
|
|
|
|
|
|
public ObjectGetService(String endpoint) {
|
|
public ObjectGetService(String endpoint) {
|
|
|
this.endpoint = endpoint;
|
|
this.endpoint = endpoint;
|
|
@@ -42,13 +46,21 @@ public class ObjectGetService {
|
|
|
|
|
|
|
|
public void getObject(String objectName) {
|
|
public void getObject(String objectName) {
|
|
|
try {
|
|
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))
|
|
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
|
.version(HttpClient.Version.HTTP_1_1)
|
|
.version(HttpClient.Version.HTTP_1_1)
|
|
|
.GET()
|
|
.GET()
|
|
|
.build();
|
|
.build();
|
|
|
|
|
|
|
|
HttpResponse<InputStream> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofInputStream());
|
|
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) {
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
@@ -71,4 +83,38 @@ public class ObjectGetService {
|
|
|
|
|
|
|
|
return webResult.getData();
|
|
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);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|