|
@@ -3,21 +3,21 @@ 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 com.fdkankan.fusion.response.FileInfoVo;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
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
|
|
@@ -39,7 +39,6 @@ public class UploadToOssUtil {
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 获取文件内容-阿里云
|
|
|
* @param objectName
|
|
@@ -213,4 +212,122 @@ public class UploadToOssUtil {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void copyFile( String sourcePath, String targetPath) {
|
|
|
+ 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 String getMd5(String filePath){
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
+ String md5 = null;
|
|
|
+ try {
|
|
|
+ InputStream inputStream = ossClient.getObject(bucket, filePath).getObjectContent();
|
|
|
+ md5 = MD5Checksum.getMD5(inputStream);
|
|
|
+ log.info("Calculated MD5 for " + filePath + " is " + md5);
|
|
|
+ inputStream.close();
|
|
|
+ }catch (Exception e){
|
|
|
+ log.info("oss-getMd5-error:{}",e);
|
|
|
+ }finally {
|
|
|
+ if(ossClient != null){
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return md5;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getSHA1(String filePath){
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
+ String md5 = null;
|
|
|
+ try {
|
|
|
+ InputStream inputStream = ossClient.getObject(bucket, filePath).getObjectContent();
|
|
|
+ md5 = MD5Checksum.getSHA1(inputStream);
|
|
|
+ log.info("Calculated MD5 for " + filePath + " is " + md5);
|
|
|
+ inputStream.close();
|
|
|
+ }catch (Exception e){
|
|
|
+ log.info("oss-getMd5-error:{}",e);
|
|
|
+ }finally {
|
|
|
+ if(ossClient != null){
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return md5;
|
|
|
+ }
|
|
|
+ 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 FileInfoVo getFileInfo(String 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:{}",e);
|
|
|
+ }finally {
|
|
|
+ if(ossClient != null){
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Long getSize(String filePath){
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
+ Long total = 0L;
|
|
|
+ try {
|
|
|
+ List<String> files = listKeysFromAli( 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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|