|
@@ -3,10 +3,12 @@ package com.fdkankan.fusion.common.util;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.common.utils.BinaryUtil;
|
|
|
import com.aliyun.oss.model.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
@@ -15,10 +17,7 @@ import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.net.*;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
@@ -26,19 +25,26 @@ import java.util.stream.Collectors;
|
|
|
public class UploadToOssUtil {
|
|
|
|
|
|
|
|
|
- @Value("${oss.point:http://oss-cn-shenzhen-internal.aliyuncs.com}")
|
|
|
+ @Value("${oss.point}")
|
|
|
private String point;
|
|
|
|
|
|
- @Value("${oss.key:LTAIUrvuHqj8pvry}")
|
|
|
+ @Value("${oss.key}")
|
|
|
private String key;
|
|
|
|
|
|
- @Value("${oss.secrey:JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4}")
|
|
|
+ @Value("${oss.secrey}")
|
|
|
private String secrey;
|
|
|
|
|
|
@Value("${oss.bucket:4dkankan}")
|
|
|
private String bucket;
|
|
|
|
|
|
+ @Value("${upload.type:oss}")
|
|
|
+ private String type;
|
|
|
|
|
|
+ @Value("${upload.query-path}")
|
|
|
+ private String queryPath;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ LocalToOssUtil localToOssUtil;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -48,6 +54,9 @@ public class UploadToOssUtil {
|
|
|
*/
|
|
|
public boolean existKey(String objectName){
|
|
|
//创建oss客户端
|
|
|
+ if("local".equals(type)){
|
|
|
+ return localToOssUtil.existKey(objectName);
|
|
|
+ }
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
|
try{
|
|
@@ -70,6 +79,9 @@ public class UploadToOssUtil {
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean downFormAli(String objectName, String localPath){
|
|
|
+ if("local".equals(type)){
|
|
|
+ return localToOssUtil.downFormAli(objectName,localPath);
|
|
|
+ }
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
|
com.aliyun.oss.model.GetObjectRequest request = new com.aliyun.oss.model.GetObjectRequest(bucket,objectName);
|
|
@@ -92,6 +104,10 @@ public class UploadToOssUtil {
|
|
|
|
|
|
|
|
|
public void uploadOss(String filePath, String key1){
|
|
|
+ if("local".equals(type)){
|
|
|
+ localToOssUtil.uploadOss(filePath,key1);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
|
log.info("upload-to-oss:file-path:{},oss-path:{}",filePath,key1);
|
|
@@ -109,6 +125,10 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
}
|
|
|
public void delete(String objectName){
|
|
|
+ if("local".equals(type)){
|
|
|
+ localToOssUtil.delete(objectName);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
|
ossClient.deleteObject(bucket, objectName);
|
|
@@ -126,6 +146,9 @@ public class UploadToOssUtil {
|
|
|
* @return
|
|
|
*/
|
|
|
public List<String> listKeysFromAli(String sourcePath) {
|
|
|
+ if("local".equals(type)){
|
|
|
+ return localToOssUtil.listKeysFromAli(sourcePath);
|
|
|
+ }
|
|
|
List<String> keyList = new ArrayList<>();
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
try {
|
|
@@ -202,6 +225,10 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
|
|
|
public void uploadFileOss(File file) {
|
|
|
+ if("local".equals(type)){
|
|
|
+ localToOssUtil.uploadFileOss(file);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
if(file.isFile()){
|
|
|
String ossPath = file.getPath();
|
|
|
ossPath = ossPath.replace("/mnt/","");
|
|
@@ -214,7 +241,54 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void copyFile( String sourcePath, String targetPath) {
|
|
|
+ if("local".equals(type)){
|
|
|
+ localToOssUtil.copyFile(sourcePath,targetPath);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
+
|
|
|
+ try {
|
|
|
+ List<String> files = listKeysFromAli( sourcePath);
|
|
|
+ if (ObjectUtils.isEmpty(files)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ files.stream().forEach(file -> {
|
|
|
+ log.info("oss-copy-file---sourcePath:{},targetPath:{}",sourcePath,targetPath);
|
|
|
+ ossClient.copyObject(this.bucket, file, this.bucket, file.replace(sourcePath, targetPath));
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("列举文件目录失败,key:" + sourcePath, e);
|
|
|
+ }finally {
|
|
|
+ if(ossClient != null){
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long getLastModified(String filePath){
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
+ try {
|
|
|
+ GetObjectRequest getObjectMetadataRequest = new GetObjectRequest(bucket, filePath);
|
|
|
+ Date LastMo = ossClient.getObjectMetadata(getObjectMetadataRequest).getLastModified();
|
|
|
+ return LastMo.getTime();
|
|
|
+ }catch (Exception e){
|
|
|
+ log.info("oss-getMd5-error:{}",e);
|
|
|
+ }finally {
|
|
|
+ if(ossClient != null){
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
public Long getSize(String filePath){
|
|
|
+ if("local".equals(type)){
|
|
|
+ return localToOssUtil.getSize(filePath);
|
|
|
+ }
|
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
Long total = 0L;
|
|
|
try {
|
|
@@ -237,4 +311,8 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
return total;
|
|
|
}
|
|
|
+
|
|
|
+ public String getOssPath(String path) {
|
|
|
+ return path.replace(queryPath,"");
|
|
|
+ }
|
|
|
}
|