|
@@ -1,377 +0,0 @@
|
|
|
-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.Autowired;
|
|
|
-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.*;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-@Slf4j
|
|
|
-@Component
|
|
|
-public class UploadToOssUtil {
|
|
|
-
|
|
|
-
|
|
|
- @Value("${oss.point}")
|
|
|
- private String point;
|
|
|
-
|
|
|
- @Value("${oss.key}")
|
|
|
- private String key;
|
|
|
-
|
|
|
- @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;
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取文件内容-阿里云
|
|
|
- * @param objectName
|
|
|
- * @return
|
|
|
- */
|
|
|
- public boolean existKey(String objectName){
|
|
|
- //创建oss客户端
|
|
|
- if("local".equals(type)){
|
|
|
- return localToOssUtil.existKey(objectName);
|
|
|
- }
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
- // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
|
- try{
|
|
|
- boolean exist = ossClient.doesObjectExist(bucket, objectName);
|
|
|
- return exist;
|
|
|
- }catch (Exception e){
|
|
|
- log.error("s4判断是否存在key异常,key=" + objectName, e);
|
|
|
- }finally {
|
|
|
- if(ossClient != null){
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 从阿里云oss下载文件到本地
|
|
|
- * @param objectName 云端文件k地址
|
|
|
- * @param localPath 本地文件地址
|
|
|
- * @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);
|
|
|
- File file = new File(localPath);
|
|
|
- if(!file.getParentFile().exists()){
|
|
|
- file.getParentFile().mkdirs();
|
|
|
- file = new File(localPath);
|
|
|
- }
|
|
|
- ossClient.getObject(request, file);
|
|
|
- return true;
|
|
|
- }catch (Exception e){
|
|
|
- log.error("阿里云oss文件下载失败,key=" + objectName, e);
|
|
|
- }finally {
|
|
|
- if(ossClient != null){
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- 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);
|
|
|
- File file = new File(filePath);
|
|
|
- if (!file.exists()) {
|
|
|
- log.info("upload-to-oss:file-path:{},oss-path:{},filePath不存在!",filePath,key1);
|
|
|
- return;
|
|
|
- }
|
|
|
- ObjectMetadata metadata = new ObjectMetadata();
|
|
|
- ossClient.putObject(bucket, key1, new File(filePath), metadata);
|
|
|
- } catch (Exception e) {
|
|
|
- log.info("upload-to-oss:error:file-path:{},key:{}",filePath,key1);
|
|
|
- } finally {
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- }
|
|
|
- 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);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("OSS删除文件失败,key=" + objectName);
|
|
|
- }finally {
|
|
|
- if(ossClient != null){
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获得文件列表-阿里云
|
|
|
- * @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 {
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取文件内容-阿里云
|
|
|
- * @param bucketName
|
|
|
- * @param objectName
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String getObjectContent(String bucketName, String objectName){
|
|
|
- //创建oss客户端
|
|
|
- OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
- InputStream objectContent = null;
|
|
|
- StringBuilder contentJson = new StringBuilder();
|
|
|
- try {
|
|
|
- // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
|
- OSSObject ossObject = ossClient.getObject(bucketName, objectName);
|
|
|
- objectContent = ossObject.getObjectContent();
|
|
|
- try(BufferedReader reader = new BufferedReader(new InputStreamReader(objectContent))){
|
|
|
- while (true) {
|
|
|
- String line = reader.readLine();
|
|
|
- if (line == null) break;
|
|
|
- contentJson.append(line);
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("读取scene.json文件流失败", e);
|
|
|
- }
|
|
|
- }catch (Exception e){
|
|
|
- log.error("s3获取文件内容失败,key="+objectName, e);
|
|
|
- }finally {
|
|
|
- if(ossClient != null){
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return contentJson.toString();
|
|
|
- }
|
|
|
-
|
|
|
- public void uploadFileOss(File file) {
|
|
|
- if("local".equals(type)){
|
|
|
- localToOssUtil.uploadFileOss(file);
|
|
|
- return ;
|
|
|
- }
|
|
|
- if(file.isFile()){
|
|
|
- String ossPath = file.getPath();
|
|
|
- ossPath = ossPath.replace("/mnt/","");
|
|
|
- ossPath = ossPath.replace("\\","/");
|
|
|
- this.uploadOss(file.getPath(),ossPath);
|
|
|
- }else {
|
|
|
- File[] files = file.listFiles();
|
|
|
- for (File file1 : files) {
|
|
|
- uploadFileOss(file1);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- 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 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){
|
|
|
- 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 Long getSize(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( 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 String getOssPath(String path) {
|
|
|
- return path.replace(queryPath,"");
|
|
|
- }
|
|
|
-}
|