|
|
@@ -13,6 +13,7 @@ import com.amazonaws.services.s3.model.PutObjectResult;
|
|
|
import com.amazonaws.services.s3.model.S3Object;
|
|
|
import com.amazonaws.services.s3.model.S3ObjectInputStream;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
@@ -45,26 +46,56 @@ public class ObjectBasicService {
|
|
|
}
|
|
|
|
|
|
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, InputStream inputStream) throws NoSuchAlgorithmException {
|
|
|
- PutObjectResult putObjectResult = s3Client.putObject(bucketName, key, inputStream, new ObjectMetadata());
|
|
|
+ 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 getObject(String key) {
|
|
|
+ public void getObject(String key) throws IOException {
|
|
|
S3Object s3Object = s3Client.getObject(bucketName, key);
|
|
|
S3ObjectInputStream inputStream = s3Object.getObjectContent();
|
|
|
+
|
|
|
+ File file = new File("/home/reghao/Downloads/0/" + key);
|
|
|
+ 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 boolean objectExist(String key) {
|
|
|
- return s3Client.doesObjectExist(bucketName, key);
|
|
|
+ boolean exist = s3Client.doesObjectExist(bucketName, key);
|
|
|
+ return exist;
|
|
|
}
|
|
|
|
|
|
public ObjectMetadata headObject(String key) {
|
|
|
@@ -72,17 +103,21 @@ public class ObjectBasicService {
|
|
|
return objectMetadata;
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) throws NoSuchAlgorithmException {
|
|
|
+ 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 key = "";
|
|
|
- boolean exist = objectBasicService.objectExist(key);*/
|
|
|
-
|
|
|
- String filePath = "/home/reghao/Downloads/1445972277.jpg";
|
|
|
+ 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("a/b/c/d/%s", file.getName());
|
|
|
+ String key = String.format("a/c/d/%s", file.getName());
|
|
|
objectBasicService.putObject(key, file);
|
|
|
+ //objectBasicService.getObject(key);
|
|
|
+ //objectBasicService.headObject(key);
|
|
|
+ //objectBasicService.objectExist(key);
|
|
|
+ log.info("{}/{}", endpoint, key);
|
|
|
}
|
|
|
}
|