|
|
@@ -1,11 +1,15 @@
|
|
|
package cn.reghao.oss.sdk;
|
|
|
|
|
|
+import cn.reghao.jutil.jdk.result.WebResult;
|
|
|
+import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
+import com.google.gson.reflect.TypeToken;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
+import java.lang.reflect.Type;
|
|
|
import java.net.URI;
|
|
|
import java.net.URISyntaxException;
|
|
|
import java.net.http.HttpClient;
|
|
|
@@ -13,6 +17,7 @@ import java.net.http.HttpRequest;
|
|
|
import java.net.http.HttpResponse;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
@@ -22,17 +27,11 @@ import java.util.function.Supplier;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
public class ObjectBasicService {
|
|
|
- private String endpoint;
|
|
|
- private String bucketName;
|
|
|
- private String accessKeyId;
|
|
|
- private String accessKeySecret;
|
|
|
- private HttpClient httpClient = HttpClient.newBuilder().build();
|
|
|
+ private final String endpoint;
|
|
|
+ private final HttpClient httpClient = HttpClient.newBuilder().build();
|
|
|
|
|
|
- public ObjectBasicService(String endpoint, String bucketName) {
|
|
|
+ public ObjectBasicService(String endpoint) {
|
|
|
this.endpoint = endpoint;
|
|
|
- this.bucketName = bucketName;
|
|
|
- this.accessKeyId = "LTAI5t9juYR3sSP3t7fstqyt";
|
|
|
- this.accessKeySecret = "Tn54Dliyk6ET3nYevum9KGRxrM8Ytt";
|
|
|
}
|
|
|
|
|
|
public void putObject(String key, File file, Map<String, String> headers)
|
|
|
@@ -46,15 +45,14 @@ public class ObjectBasicService {
|
|
|
int statusCode = httpResponse.statusCode();
|
|
|
String body = httpResponse.body();
|
|
|
|
|
|
- /*Type type = new TypeToken<WebResult<RsaPubkey>>(){}.getType();
|
|
|
- WebResult<RsaPubkey> webResult = JsonConverter.jsonToObject(body, type);
|
|
|
+ Type type = new TypeToken<WebResult<String>>(){}.getType();
|
|
|
+ WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
|
|
|
if (webResult.getCode() == 0) {
|
|
|
- RsaPubkey rsaPubkey = webResult.getData();
|
|
|
- log.info("{} - {}", rsaPubkey.getPubkey(), rsaPubkey.getR());
|
|
|
+ String data = webResult.getData();
|
|
|
+ log.info("{}", data);
|
|
|
} else {
|
|
|
log.error("{}", webResult.getMsg());
|
|
|
- }*/
|
|
|
- System.out.println();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public void putObject(String key, InputStream inputStream, Map<String, String> headers)
|
|
|
@@ -78,16 +76,14 @@ public class ObjectBasicService {
|
|
|
public void postObject(String key, File file, String contentType)
|
|
|
throws URISyntaxException, IOException, InterruptedException {
|
|
|
MultiPartBodyPublisher publisher = new MultiPartBodyPublisher()
|
|
|
- .addPart("key", key)
|
|
|
.addPart("file", Paths.get(file.getAbsolutePath()))
|
|
|
- .addPart("contentType", contentType);
|
|
|
+ .addPart("channelId", 1+"");
|
|
|
+ //.addPart("contentType", contentType);
|
|
|
|
|
|
String api = String.format("%s/", endpoint);
|
|
|
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
|
.version(HttpClient.Version.HTTP_1_1)
|
|
|
- //.header("Host", "tnb.oss-cn-chengdu.reghao.xyz")
|
|
|
.header("Authorization", "5c34161755e3d8b44e5f165ce1b91ef9")
|
|
|
- //.header("Content-Length", String.valueOf(file.length()))
|
|
|
.header("Content-Type", "multipart/form-data; boundary=" + publisher.getBoundary())
|
|
|
.POST(publisher.build())
|
|
|
.build();
|
|
|
@@ -104,7 +100,6 @@ public class ObjectBasicService {
|
|
|
.build();
|
|
|
|
|
|
HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
- System.out.println();
|
|
|
}
|
|
|
|
|
|
public void getObject(String objectName) throws IOException, InterruptedException, URISyntaxException {
|
|
|
@@ -114,16 +109,12 @@ public class ObjectBasicService {
|
|
|
.GET()
|
|
|
.build();
|
|
|
|
|
|
- HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
- System.out.println();
|
|
|
- }
|
|
|
-
|
|
|
- public void deleteObject() {
|
|
|
- }
|
|
|
-
|
|
|
- public void deleteMultipleObjects() {
|
|
|
+ HttpResponse<InputStream> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofInputStream());
|
|
|
}
|
|
|
|
|
|
- public void putObjectCopy() {
|
|
|
+ public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
|
|
|
+ String filePath = "/home/reghao/Downloads/video.mp4";
|
|
|
+ ObjectBasicService objectBasicService = new ObjectBasicService("http://localhost:8010");
|
|
|
+ objectBasicService.postObject("a/b/c", new File(filePath), "");
|
|
|
}
|
|
|
}
|