|
@@ -1,20 +1,19 @@
|
|
|
package cn.reghao.oss.sdk;
|
|
package cn.reghao.oss.sdk;
|
|
|
|
|
|
|
|
-import cn.reghao.jutil.jdk.result.WebResult;
|
|
|
|
|
-import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
|
|
|
|
+import cn.reghao.jutil.jdk.converter.ByteHex;
|
|
|
|
|
+import cn.reghao.jutil.jdk.security.Base64Util;
|
|
|
|
|
+import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
|
|
+import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
|
|
|
|
+import com.amazonaws.auth.BasicAWSCredentials;
|
|
|
|
|
+import com.amazonaws.client.builder.AwsClientBuilder;
|
|
|
|
|
+import com.amazonaws.services.s3.AmazonS3;
|
|
|
|
|
+import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
|
|
|
|
+import com.amazonaws.services.s3.model.ObjectMetadata;
|
|
|
|
|
+import com.amazonaws.services.s3.model.PutObjectResult;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
-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;
|
|
|
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author reghao
|
|
* @author reghao
|
|
@@ -23,107 +22,37 @@ import java.util.function.Supplier;
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class ObjectBasicService {
|
|
public class ObjectBasicService {
|
|
|
private String endpoint;
|
|
private String endpoint;
|
|
|
- private String bucketName;
|
|
|
|
|
- private String accessKeyId;
|
|
|
|
|
- private String accessKeySecret;
|
|
|
|
|
- private HttpClient httpClient = HttpClient.newBuilder().build();
|
|
|
|
|
-
|
|
|
|
|
|
|
+ private String region;
|
|
|
|
|
+ private final String bucketName;
|
|
|
|
|
+ private String accessKey;
|
|
|
|
|
+ private String secretKey;
|
|
|
|
|
+ private final AmazonS3 s3Client;
|
|
|
|
|
+
|
|
|
public ObjectBasicService(String endpoint, String bucketName) {
|
|
public ObjectBasicService(String endpoint, String bucketName) {
|
|
|
this.endpoint = endpoint;
|
|
this.endpoint = endpoint;
|
|
|
|
|
+ this.region = "chengdu";
|
|
|
this.bucketName = bucketName;
|
|
this.bucketName = bucketName;
|
|
|
- this.accessKeyId = "LTAI5t9juYR3sSP3t7fstqyt";
|
|
|
|
|
- this.accessKeySecret = "Tn54Dliyk6ET3nYevum9KGRxrM8Ytt";
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- 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<RsaPubkey>>(){}.getType();
|
|
|
|
|
- WebResult<RsaPubkey> webResult = JsonConverter.jsonToObject(body, type);
|
|
|
|
|
- if (webResult.getCode() == 0) {
|
|
|
|
|
- RsaPubkey rsaPubkey = webResult.getData();
|
|
|
|
|
- log.info("{} - {}", rsaPubkey.getPubkey(), rsaPubkey.getR());
|
|
|
|
|
- } else {
|
|
|
|
|
- log.error("{}", webResult.getMsg());
|
|
|
|
|
- }*/
|
|
|
|
|
- System.out.println();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- 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(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);
|
|
|
|
|
-
|
|
|
|
|
- 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())
|
|
|
|
|
|
|
+ this.accessKey = "LTAI5t9juYR3sSP3t7fstqyt";
|
|
|
|
|
+ this.secretKey = "Tn54Dliyk6ET3nYevum9KGRxrM8Ytt";
|
|
|
|
|
+ this.s3Client = AmazonS3ClientBuilder.standard()
|
|
|
|
|
+ .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
|
|
|
|
|
+ .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region))
|
|
|
|
|
+ .withPathStyleAccessEnabled(false)
|
|
|
|
|
+ .withChunkedEncodingDisabled(true)
|
|
|
.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());
|
|
|
|
|
- System.out.println();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- 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<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
- System.out.println();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public void deleteObject() {
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void deleteMultipleObjects() {
|
|
|
|
|
|
|
+ public void putObject(String key, File file) throws NoSuchAlgorithmException {
|
|
|
|
|
+ PutObjectResult putObjectResult = s3Client.putObject(bucketName, key, file);
|
|
|
|
|
+ String eTag = putObjectResult.getETag();
|
|
|
|
|
+ byte[] bytes = ByteHex.hex2Bytes(eTag);
|
|
|
|
|
+ String md5Base64 = Base64Util.encode(DigestUtil.md5sum(bytes));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void putObjectCopy() {
|
|
|
|
|
|
|
+ public void putObject(String key, InputStream inputStream) throws NoSuchAlgorithmException {
|
|
|
|
|
+ PutObjectResult putObjectResult = s3Client.putObject(bucketName, key, inputStream, new ObjectMetadata());
|
|
|
|
|
+ String eTag = putObjectResult.getETag();
|
|
|
|
|
+ byte[] bytes = ByteHex.hex2Bytes(eTag);
|
|
|
|
|
+ String md5Base64 = Base64Util.encode(DigestUtil.md5sum(bytes));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|