|
|
@@ -1,15 +1,20 @@
|
|
|
package cn.reghao.oss.sdk;
|
|
|
|
|
|
-import cn.reghao.jutil.jdk.converter.ByteHex;
|
|
|
-import cn.reghao.jutil.jdk.security.Base64Util;
|
|
|
-import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
-import com.amazonaws.services.s3.AmazonS3;
|
|
|
-import com.amazonaws.services.s3.model.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
|
|
|
-import java.io.*;
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+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
|
|
|
@@ -17,100 +22,108 @@ import java.security.NoSuchAlgorithmException;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
public class ObjectBasicService {
|
|
|
- private final String bucketName;
|
|
|
- private final AmazonS3 s3Client;
|
|
|
+ private String endpoint;
|
|
|
+ private String bucketName;
|
|
|
+ private String accessKeyId;
|
|
|
+ private String accessKeySecret;
|
|
|
+ private HttpClient httpClient = HttpClient.newBuilder().build();
|
|
|
|
|
|
public ObjectBasicService(String endpoint, String bucketName) {
|
|
|
+ this.endpoint = endpoint;
|
|
|
this.bucketName = bucketName;
|
|
|
- S3Client s3Client = new S3Client(endpoint, bucketName);
|
|
|
- this.s3Client = s3Client.getS3Client();
|
|
|
+ this.accessKeyId = "LTAI5t9juYR3sSP3t7fstqyt";
|
|
|
+ this.accessKeySecret = "Tn54Dliyk6ET3nYevum9KGRxrM8Ytt";
|
|
|
}
|
|
|
|
|
|
- public void putObject(String key, File file) throws NoSuchAlgorithmException {
|
|
|
- ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
- objectMetadata.setContentLength(file.length());
|
|
|
-
|
|
|
- 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 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, long length) throws NoSuchAlgorithmException {
|
|
|
- ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
- objectMetadata.setContentLength(length);
|
|
|
-
|
|
|
- PutObjectResult putObjectResult = s3Client.putObject(bucketName, key, inputStream, objectMetadata);
|
|
|
- String eTag = putObjectResult.getETag();
|
|
|
- byte[] bytes = ByteHex.hex2Bytes(eTag);
|
|
|
- String md5Base64 = Base64Util.encode(DigestUtil.md5sum(bytes));
|
|
|
+ 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 getObject(String key, String savedAbsolutePath) throws IOException {
|
|
|
- S3Object s3Object = s3Client.getObject(bucketName, key);
|
|
|
- S3ObjectInputStream inputStream = s3Object.getObjectContent();
|
|
|
- saveFile(inputStream, savedAbsolutePath);
|
|
|
+ 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())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
+ System.out.println();
|
|
|
}
|
|
|
|
|
|
- public void getObject(String key) throws IOException {
|
|
|
- S3Object s3Object = s3Client.getObject(bucketName, key);
|
|
|
- S3ObjectInputStream inputStream = s3Object.getObjectContent();
|
|
|
- String absolutePath = "/home/reghao/Downloads/0/" + key;
|
|
|
- saveFile(inputStream, absolutePath);
|
|
|
+ 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();
|
|
|
}
|
|
|
|
|
|
- private void saveFile(InputStream inputStream, String absolutePath) throws IOException {
|
|
|
- File file = new File(absolutePath);
|
|
|
- if (file.exists()) {
|
|
|
- inputStream.readAllBytes();
|
|
|
- inputStream.close();
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- File parentDir = file.getParentFile();
|
|
|
- if (!parentDir.exists()) {
|
|
|
- FileUtils.forceMkdir(parentDir);
|
|
|
- }
|
|
|
-
|
|
|
- FileOutputStream fos = new FileOutputStream(file);
|
|
|
- // 1MiB
|
|
|
- int len = 1024*1024;
|
|
|
- byte[] buf = new byte[len];
|
|
|
- int readLen;
|
|
|
- while ((readLen = inputStream.read(buf, 0, len)) != -1) {
|
|
|
- fos.write(buf, 0, readLen);
|
|
|
- }
|
|
|
- fos.close();
|
|
|
- inputStream.close();
|
|
|
+ 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 boolean objectExist(String key) {
|
|
|
- boolean exist = s3Client.doesObjectExist(bucketName, key);
|
|
|
- return exist;
|
|
|
+ public void deleteObject() {
|
|
|
}
|
|
|
|
|
|
- public ObjectMetadata headObject(String key) {
|
|
|
- ObjectMetadata objectMetadata = s3Client.getObjectMetadata(bucketName, key);
|
|
|
- return objectMetadata;
|
|
|
+ public void deleteMultipleObjects() {
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
|
|
|
- String endpoint = "http://oss.reghao.cn";
|
|
|
- String bucketName = "tnb";
|
|
|
- ObjectBasicService objectBasicService = new ObjectBasicService(endpoint, bucketName);
|
|
|
-
|
|
|
- String filePath = "/home/reghao/Downloads/centos7";
|
|
|
- filePath = "/home/reghao/Downloads/SecurityWhitepaper.pdf";
|
|
|
- //filePath = "/home/reghao/Downloads/USMC-120509-M-PH073-074.jpg";
|
|
|
- //filePath = "/home/reghao/Downloads/public.sql";
|
|
|
- File file = new File(filePath);
|
|
|
- String key = String.format("aa/bb/cc/%s", file.getName());
|
|
|
- key = "video/playback/NbnoDb1qqN";
|
|
|
- key = "video/playback/7c75c5868ee878672a083434bb465321";
|
|
|
- objectBasicService.putObject(key, file);
|
|
|
- //objectBasicService.getObject(key, "/home/reghao/Downloads/11111");
|
|
|
- //objectBasicService.headObject(key);
|
|
|
- //objectBasicService.objectExist(key);
|
|
|
- log.info("{}/{}", endpoint, key);
|
|
|
+ public void putObjectCopy() {
|
|
|
}
|
|
|
}
|