|
|
@@ -1,118 +0,0 @@
|
|
|
-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;
|
|
|
-import java.net.http.HttpRequest;
|
|
|
-import java.net.http.HttpResponse;
|
|
|
-import java.nio.file.Path;
|
|
|
-import java.nio.file.Paths;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.function.Supplier;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author reghao
|
|
|
- * @date 2022-11-21 17:19:04
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-public class ObjectBasicService {
|
|
|
- private final String endpoint;
|
|
|
- private final HttpClient httpClient = HttpClient.newBuilder().build();
|
|
|
-
|
|
|
- public ObjectBasicService(String endpoint) {
|
|
|
- this.endpoint = endpoint;
|
|
|
- }
|
|
|
-
|
|
|
- public void putObject(String key, File file, Map<String, String> headers)
|
|
|
- throws URISyntaxException, IOException, InterruptedException {
|
|
|
- String api = String.format("%s/%s", endpoint, key);
|
|
|
- HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
|
|
|
- headers.forEach(builder::header);
|
|
|
-
|
|
|
- HttpRequest httpRequest = builder.PUT(HttpRequest.BodyPublishers.ofFile(Path.of(file.getAbsolutePath()))).build();
|
|
|
- HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
- int statusCode = httpResponse.statusCode();
|
|
|
- String body = httpResponse.body();
|
|
|
-
|
|
|
- Type type = new TypeToken<WebResult<String>>(){}.getType();
|
|
|
- WebResult<String> webResult = JsonConverter.jsonToObject(body, type);
|
|
|
- if (webResult.getCode() == 0) {
|
|
|
- String data = webResult.getData();
|
|
|
- log.info("{}", data);
|
|
|
- } else {
|
|
|
- log.error("{}", webResult.getMsg());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void putObject(String key, InputStream inputStream, Map<String, String> headers)
|
|
|
- throws URISyntaxException, IOException, InterruptedException {
|
|
|
- String api = String.format("%s/%s", endpoint, key);
|
|
|
- HttpRequest.Builder builder = HttpRequest.newBuilder(new URI(api)).version(HttpClient.Version.HTTP_1_1);
|
|
|
- headers.forEach(builder::header);
|
|
|
-
|
|
|
- BufferedInputStream bis = new BufferedInputStream(inputStream);
|
|
|
- Supplier<? extends InputStream> streamSupplier = new Supplier<BufferedInputStream>() {
|
|
|
- @Override
|
|
|
- public BufferedInputStream get() {
|
|
|
- return bis;
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- HttpRequest httpRequest = builder.PUT(HttpRequest.BodyPublishers.ofInputStream(streamSupplier)).build();
|
|
|
- HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
- }
|
|
|
-
|
|
|
- public void postObject(File file, int channelId)
|
|
|
- throws URISyntaxException, IOException, InterruptedException {
|
|
|
- MultiPartBodyPublisher publisher = new MultiPartBodyPublisher()
|
|
|
- .addPart("file", Paths.get(file.getAbsolutePath()))
|
|
|
- .addPart("channelId", channelId+"");
|
|
|
-
|
|
|
- String api = String.format("%s/", endpoint);
|
|
|
- HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
|
- .version(HttpClient.Version.HTTP_1_1)
|
|
|
- .header("Authorization", "5c34161755e3d8b44e5f165ce1b91ef9")
|
|
|
- .header("Content-Type", "multipart/form-data; boundary=" + publisher.getBoundary())
|
|
|
- .POST(publisher.build())
|
|
|
- .build();
|
|
|
-
|
|
|
- HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
- System.out.println();
|
|
|
- }
|
|
|
-
|
|
|
- public void headObject(String objectName) throws IOException, URISyntaxException, InterruptedException {
|
|
|
- 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());
|
|
|
- }
|
|
|
-
|
|
|
- public void getObject(String objectName) throws IOException, InterruptedException, URISyntaxException {
|
|
|
- String api = String.format("%s/%s", endpoint, objectName);
|
|
|
- HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
|
- .version(HttpClient.Version.HTTP_1_1)
|
|
|
- .GET()
|
|
|
- .build();
|
|
|
-
|
|
|
- HttpResponse<InputStream> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofInputStream());
|
|
|
- }
|
|
|
-
|
|
|
- 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(new File(filePath), 1);
|
|
|
- }
|
|
|
-}
|