|
@@ -5,7 +5,6 @@ import cn.hutool.core.io.FileUtil;
|
|
import com.aliyun.oss.OSSClient;
|
|
import com.aliyun.oss.OSSClient;
|
|
import com.aliyun.oss.common.utils.BinaryUtil;
|
|
import com.aliyun.oss.common.utils.BinaryUtil;
|
|
import com.aliyun.oss.model.*;
|
|
import com.aliyun.oss.model.*;
|
|
-import com.fdkankan.fusion.config.LaserOssConfig;
|
|
|
|
import com.fdkankan.fusion.response.FileInfoVo;
|
|
import com.fdkankan.fusion.response.FileInfoVo;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
@@ -48,9 +47,6 @@ public class UploadToOssUtil {
|
|
@Autowired
|
|
@Autowired
|
|
LocalToOssUtil localToOssUtil;
|
|
LocalToOssUtil localToOssUtil;
|
|
|
|
|
|
- @Autowired
|
|
|
|
- LaserOssConfig laserOssConfig;
|
|
|
|
-
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取文件内容-阿里云
|
|
* 获取文件内容-阿里云
|
|
@@ -130,6 +126,16 @@ public class UploadToOssUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public void delete(String objectName){
|
|
public void delete(String objectName){
|
|
|
|
+ if(StringUtils.isBlank(objectName)){
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if(objectName.contains(queryPath)){
|
|
|
|
+ objectName = objectName.replace(queryPath,"");
|
|
|
|
+ }
|
|
|
|
+ if(!this.fileExist(objectName)){
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
if("local".equals(type)){
|
|
if("local".equals(type)){
|
|
localToOssUtil.delete(objectName);
|
|
localToOssUtil.delete(objectName);
|
|
return ;
|
|
return ;
|
|
@@ -151,50 +157,11 @@ public class UploadToOssUtil {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public List<String> listKeysFromAli(String sourcePath) {
|
|
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 {
|
|
|
|
- boolean flag = true;
|
|
|
|
- String nextMaker = null;
|
|
|
|
- ListObjectsRequest listObjectsRequest = new ListObjectsRequest(this.bucket);
|
|
|
|
- //指定下一级文件
|
|
|
|
- listObjectsRequest.setPrefix(sourcePath);
|
|
|
|
- //设置分页的页容量
|
|
|
|
- listObjectsRequest.setMaxKeys(200);
|
|
|
|
- do
|
|
|
|
- {
|
|
|
|
- //获取下一页的起始点,它的下一项
|
|
|
|
- listObjectsRequest.setMarker(nextMaker);
|
|
|
|
- ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
|
|
|
|
- List<OSSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
|
|
|
|
- List<String> collect = objectSummaries.stream().map(summary -> {
|
|
|
|
- return summary.getKey();
|
|
|
|
- }).collect(Collectors.toList());
|
|
|
|
- if(CollUtil.isNotEmpty(collect)){
|
|
|
|
- keyList.addAll(collect);
|
|
|
|
- }
|
|
|
|
- nextMaker = objectListing.getNextMarker();
|
|
|
|
- //全部执行完后,为false
|
|
|
|
- flag = objectListing.isTruncated();
|
|
|
|
- } while (flag);
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.error("获取文件列表失败,path="+sourcePath, e);
|
|
|
|
- }finally {
|
|
|
|
- if(ossClient != null){
|
|
|
|
- ossClient.shutdown();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- ossClient.shutdown();
|
|
|
|
|
|
|
|
- return keyList;
|
|
|
|
|
|
+ return listKeysFromAli(sourcePath,bucket);
|
|
}
|
|
}
|
|
|
|
|
|
- public List<String> listKeysFromAli(String bucket,String sourcePath) {
|
|
|
|
|
|
+ public List<String> listKeysFromAli(String sourcePath,String bucket) {
|
|
if("local".equals(type)){
|
|
if("local".equals(type)){
|
|
return localToOssUtil.listKeysFromAli(sourcePath);
|
|
return localToOssUtil.listKeysFromAli(sourcePath);
|
|
}
|
|
}
|
|
@@ -238,47 +205,6 @@ public class UploadToOssUtil {
|
|
return keyList;
|
|
return keyList;
|
|
}
|
|
}
|
|
|
|
|
|
- public static List<String> listKeysFromAli(String point,String key ,String secrey,String bucket,String sourcePath) {
|
|
|
|
- List<String> keyList = new ArrayList<>();
|
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
- try {
|
|
|
|
- boolean flag = true;
|
|
|
|
- String nextMaker = null;
|
|
|
|
- ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket);
|
|
|
|
- //指定下一级文件
|
|
|
|
- listObjectsRequest.setPrefix(sourcePath);
|
|
|
|
- //设置分页的页容量
|
|
|
|
- listObjectsRequest.setMaxKeys(200);
|
|
|
|
- do
|
|
|
|
- {
|
|
|
|
- //获取下一页的起始点,它的下一项
|
|
|
|
- listObjectsRequest.setMarker(nextMaker);
|
|
|
|
- ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
|
|
|
|
- List<OSSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
|
|
|
|
- List<String> collect = objectSummaries.stream().map(summary -> {
|
|
|
|
- return summary.getKey();
|
|
|
|
- }).collect(Collectors.toList());
|
|
|
|
- if(CollUtil.isNotEmpty(collect)){
|
|
|
|
- keyList.addAll(collect);
|
|
|
|
- }
|
|
|
|
- nextMaker = objectListing.getNextMarker();
|
|
|
|
- //全部执行完后,为false
|
|
|
|
- flag = objectListing.isTruncated();
|
|
|
|
- } while (flag);
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.error("获取文件列表失败,path="+sourcePath, e);
|
|
|
|
- }finally {
|
|
|
|
- if(ossClient != null){
|
|
|
|
- ossClient.shutdown();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- ossClient.shutdown();
|
|
|
|
-
|
|
|
|
- return keyList;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 获取文件内容-阿里云
|
|
* 获取文件内容-阿里云
|
|
* @param objectName
|
|
* @param objectName
|
|
@@ -408,7 +334,7 @@ public class UploadToOssUtil {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
- public FileInfoVo getFileInfo(String filePath){
|
|
|
|
|
|
+ public FileInfoVo getFileInfo(String filePath,String bucket){
|
|
if("local".equals(type)){
|
|
if("local".equals(type)){
|
|
return localToOssUtil.getFileInfo(filePath);
|
|
return localToOssUtil.getFileInfo(filePath);
|
|
}
|
|
}
|
|
@@ -432,38 +358,18 @@ public class UploadToOssUtil {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
- public FileInfoVo getFileInfo(String bucket,String filePath){
|
|
|
|
- if("local".equals(type)){
|
|
|
|
- return localToOssUtil.getFileInfo(filePath);
|
|
|
|
- }
|
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
- try {
|
|
|
|
- GetObjectRequest getObjectMetadataRequest = new GetObjectRequest(bucket, filePath);
|
|
|
|
- Date LastMo = ossClient.getObjectMetadata(getObjectMetadataRequest).getLastModified();
|
|
|
|
- String md5 = ossClient.getObjectMetadata(getObjectMetadataRequest).getETag();
|
|
|
|
- Long size = ossClient.getObjectMetadata(getObjectMetadataRequest).getContentLength();
|
|
|
|
-
|
|
|
|
- InputStream inputStream = ossClient.getObject(bucket, filePath).getObjectContent();
|
|
|
|
- String sha1 = MD5Checksum.getSHA1(inputStream);
|
|
|
|
- return new FileInfoVo(md5,sha1.toUpperCase(),LastMo.getTime(),size);
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.info("oss-getFileInfo-error:{}",filePath,e);
|
|
|
|
- }finally {
|
|
|
|
- if(ossClient != null){
|
|
|
|
- ossClient.shutdown();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return null;
|
|
|
|
|
|
+ public FileInfoVo getFileInfo(String filePath){
|
|
|
|
+ return getFileInfo(filePath,bucket);
|
|
}
|
|
}
|
|
|
|
|
|
- public Long getSize(String filePath){
|
|
|
|
|
|
+ public Long getSize(String filePath,String bucket){
|
|
if("local".equals(type)){
|
|
if("local".equals(type)){
|
|
return localToOssUtil.getSize(filePath);
|
|
return localToOssUtil.getSize(filePath);
|
|
}
|
|
}
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
Long total = 0L;
|
|
Long total = 0L;
|
|
try {
|
|
try {
|
|
- List<String> files = listKeysFromAli( filePath);
|
|
|
|
|
|
+ List<String> files = listKeysFromAli( filePath,bucket);
|
|
if (ObjectUtils.isEmpty(files)) {
|
|
if (ObjectUtils.isEmpty(files)) {
|
|
return 0L;
|
|
return 0L;
|
|
}
|
|
}
|
|
@@ -483,33 +389,11 @@ public class UploadToOssUtil {
|
|
return total;
|
|
return total;
|
|
}
|
|
}
|
|
|
|
|
|
- public Long getSize(String bucket,String filePath){
|
|
|
|
- if("local".equals(type)){
|
|
|
|
- return localToOssUtil.getSize(filePath);
|
|
|
|
- }
|
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
- Long total = 0L;
|
|
|
|
- try {
|
|
|
|
- List<String> files = listKeysFromAli( bucket,filePath);
|
|
|
|
- if (ObjectUtils.isEmpty(files)) {
|
|
|
|
- return 0L;
|
|
|
|
- }
|
|
|
|
- for (String file : files) {
|
|
|
|
- GetObjectRequest getObjectMetadataRequest = new GetObjectRequest(bucket, file);
|
|
|
|
- Long size = ossClient.getObjectMetadata(getObjectMetadataRequest).getContentLength();
|
|
|
|
- total += size;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.info("oss-getFileInfo-error:{}",e);
|
|
|
|
- }finally {
|
|
|
|
- if(ossClient != null){
|
|
|
|
- ossClient.shutdown();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return total;
|
|
|
|
|
|
+ public Long getSize(String filePath){
|
|
|
|
+ return getSize(filePath,bucket);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
public String getOssPath(String path) {
|
|
public String getOssPath(String path) {
|
|
return path.replace(queryPath,"");
|
|
return path.replace(queryPath,"");
|
|
}
|
|
}
|
|
@@ -517,35 +401,4 @@ public class UploadToOssUtil {
|
|
public boolean fileExist(String objKey) {
|
|
public boolean fileExist(String objKey) {
|
|
return this.existKey(objKey);
|
|
return this.existKey(objKey);
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
- public static void main(String[] args) {
|
|
|
|
- String bucket ="laser-data";
|
|
|
|
- String key ="LTAI5tJwboCj3r4vUNkSmbyX";
|
|
|
|
- String secrey ="meDy7VYAWbg8kZCKsoUZcIYQxigWOy";
|
|
|
|
- String point ="http://oss-cn-shenzhen.aliyuncs.com";
|
|
|
|
-
|
|
|
|
- String filePath = "testdata/SG-t-N4FjKEtJIjo/data/SG-t-N4FjKEtJIjo/webcloud";
|
|
|
|
-
|
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
- Long total = 0L;
|
|
|
|
- try {
|
|
|
|
- List<String> files = listKeysFromAli(point, key, secrey, bucket,filePath);
|
|
|
|
- if (ObjectUtils.isEmpty(files)) {
|
|
|
|
- }
|
|
|
|
- for (String file : files) {
|
|
|
|
- GetObjectRequest getObjectMetadataRequest = new GetObjectRequest(bucket, file);
|
|
|
|
- Long size = ossClient.getObjectMetadata(getObjectMetadataRequest).getContentLength();
|
|
|
|
- total += size;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.info("oss-getFileInfo-error:{}",e);
|
|
|
|
- }finally {
|
|
|
|
- if(ossClient != null){
|
|
|
|
- ossClient.shutdown();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- System.out.println(total);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|