Browse Source

完善oss文件操作功能

tianboguang 3 năm trước cách đây
mục cha
commit
776de850f2

+ 354 - 0
4dkankan-utils-fyun-oss/src/main/java/com/fdkankan/fyun/oss/OssFileService.java

@@ -0,0 +1,354 @@
+package com.fdkankan.fyun.oss;
+
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.model.*;
+import com.fdkankan.common.util.CreateObjUtil;
+import com.fdkankan.fyun.config.FYunFileConfig;
+import com.fdkankan.fyun.face.FYunFileService;
+import com.fdkankan.fyun.oss.constant.OssConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
+
+import java.io.*;
+import java.net.FileNameMap;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+@Component
+public class OssFileService implements FYunFileService {
+
+    private Logger log = LoggerFactory.getLogger(this.getClass().getName());
+
+    @Autowired
+    private OSS ossClient;
+
+    @Autowired
+    private FYunFileConfig fYunFileConfig;
+
+    @Override
+    public void uploadFile(String bucket, byte[] data, String remoteFilePath) throws Exception {
+        try {
+            ossClient.putObject(bucket, remoteFilePath, new ByteArrayInputStream(data));
+        } catch (Exception e) {
+            log.error("oss上传文件失败", e);
+            e.printStackTrace();
+        } finally {
+            if (!ObjectUtils.isEmpty(ossClient)) {
+                ossClient.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void uploadFile(byte[] data, String remoteFilePath) throws Exception {
+        uploadFile(fYunFileConfig.getBucket(), data, remoteFilePath);
+    }
+
+    @Override
+    public void uploadFile(String bucket, String filePath, String remoteFilePath) throws Exception {
+        uploadFile(bucket, filePath, remoteFilePath, true);
+    }
+
+    @Override
+    public void uploadFile(String filePath, String remoteFilePath) throws Exception {
+        uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath, true);
+    }
+
+    private void uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown) throws Exception {
+        try {
+            File file = new File(filePath);
+            if (!file.exists()) {
+                log.error("要上传的文件不存在:" + filePath);
+                return;
+            }
+            ObjectMetadata metadata = new ObjectMetadata();
+            if (filePath.contains(".jpg")) {
+                metadata.setContentType("image/jpeg");
+            }
+            if (filePath.contains(".mp4")) {
+                metadata.setContentType("video/mp4");
+            }
+            if (filePath.contains(".mp3")) {
+                metadata.setContentType("audio/mp3");
+            }
+            ossClient.putObject(bucket, remoteFilePath, new File(filePath), metadata);
+        } catch (Exception e) {
+            log.error("oss上传文件失败", e);
+            e.printStackTrace();
+        } finally {
+            if ((ObjectUtils.isEmpty(shutdown) || shutdown) && !ObjectUtils.isEmpty(ossClient)) {
+                ossClient.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void uploadFileByCommand(String bucket, String filePath, String remoteFilePath) throws Exception {
+        String ossPath = bucket + "/" + remoteFilePath;
+        try {
+            String command = String.format(OssConstants.UPLOAD_SH, ossPath, filePath);
+            log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath);
+            CreateObjUtil.callshell(command);
+        } catch (Exception e) {
+            log.error("上传文件失败, ossPath:{}, srcPath:{}", ossPath, filePath);
+            e.printStackTrace();
+        }
+    }
+
+    @Override
+    public void uploadFileByCommand(String filePath, String remoteFilePath) throws Exception {
+        uploadFileByCommand(fYunFileConfig.getBucket(), filePath, remoteFilePath);
+    }
+
+    @Override
+    public void deleteFile(String bucket, String remoteFilePath) throws IOException {
+        try {
+            ossClient.deleteObject(bucket, remoteFilePath);
+        } catch (Exception e) {
+            log.error("OSS删除文件失败,key=" + remoteFilePath);
+            e.printStackTrace();
+        } finally {
+            if (!ObjectUtils.isEmpty(ossClient)) {
+                ossClient.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void deleteFile(String remoteFilePath) throws IOException {
+        deleteFile(fYunFileConfig.getBucket(), remoteFilePath);
+    }
+
+    @Override
+    public void deleteFolder(String bucket, String remoteFolderPath) throws Exception {
+        try {
+            List<String> remoteFiles = listRemoteFiles(bucket, remoteFolderPath, false);
+            if(CollectionUtils.isEmpty(remoteFiles)){
+                return;
+            }
+            DeleteObjectsRequest request = new DeleteObjectsRequest(bucket);
+            request.setKeys(remoteFiles);
+            ossClient.deleteObjects(request);
+        } catch (Exception e) {
+            log.error("OSS删除文件失败,key=" + remoteFolderPath);
+            e.printStackTrace();
+        } finally {
+            if (!ObjectUtils.isEmpty(ossClient)) {
+                ossClient.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void deleteFolder(String remoteFolderPath) throws Exception {
+        deleteFolder(fYunFileConfig.getBucket(), remoteFolderPath);
+    }
+
+    @Override
+    public void uploadMulFiles(String bucket, Map<String, String> filepaths) throws Exception {
+        try {
+            for (Map.Entry<String, String> entry : filepaths.entrySet()) {
+                uploadFile(bucket, entry.getKey(), entry.getValue(), false);
+            }
+        } catch (Exception e) {
+            log.error("OSS批量上传文件失败!");
+        } finally {
+            if (!ObjectUtils.isEmpty(ossClient)) {
+                ossClient.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void uploadMulFiles(Map<String, String> filepaths) throws Exception {
+        uploadMulFiles(fYunFileConfig.getBucket(), filepaths);
+    }
+
+    @Override
+    public String getContentType(String remoteFolderPath) throws Exception {
+        FileNameMap fileNameMap = URLConnection.getFileNameMap();
+        String contentType = fileNameMap.getContentTypeFor(remoteFolderPath);
+        return contentType;
+    }
+
+    @Override
+    public List<String> listRemoteFiles(String bucket, String sourcePath) throws Exception {
+        return listRemoteFiles(bucket, sourcePath, true);
+    }
+
+    @Override
+    public List<String> listRemoteFiles(String sourcePath) throws Exception {
+        return listRemoteFiles(fYunFileConfig.getBucket(), sourcePath);
+    }
+
+    private List<String> listRemoteFiles(String bucket, String sourcePath, Boolean shutdown) throws Exception {
+        List<String> keyList = new ArrayList<>();
+        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<String> collect = objectListing.getObjectSummaries().parallelStream()
+                        .map(OSSObjectSummary::getKey).collect(Collectors.toList());
+                if (!CollectionUtils.isEmpty(collect)) {
+                    keyList.addAll(collect);
+                }
+                nextMaker = objectListing.getNextMarker();
+                //全部执行完后,为false
+                flag = objectListing.isTruncated();
+            } while (flag);
+        } catch (Exception e) {
+            log.error("获取文件列表失败,path=" + sourcePath, e);
+            e.printStackTrace();
+        } finally {
+            if ((ObjectUtils.isEmpty(shutdown) || shutdown) && !ObjectUtils.isEmpty(ossClient)) {
+                ossClient.shutdown();
+            }
+        }
+        return keyList;
+    }
+
+    @Override
+    public void copyFileBetweenFyun(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) throws Exception {
+        copyFileBetweenFyun(sourceBucketName,sourcePath,targetBucketName,targetPath,true);
+    }
+
+    private void copyFileBetweenFyun(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath,Boolean shutdown) throws Exception {
+        try {
+            List<String> files = listRemoteFiles(sourceBucketName, sourcePath, false);
+            if (ObjectUtils.isEmpty(files)) {
+                log.error("源文件夹为空:{}", sourcePath);
+            }
+            files.stream().forEach(file -> {
+                ossClient.copyObject(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
+            });
+        } catch (Exception e) {
+            log.error("列举文件目录失败,key=" + sourcePath);
+        } finally {
+            if ((ObjectUtils.isEmpty(shutdown) || shutdown) && !ObjectUtils.isEmpty(ossClient)) {
+                ossClient.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void copyFileBetweenFyun(String sourcePath, String targetBucketName, String targetPath) throws Exception {
+        copyFileBetweenFyun(fYunFileConfig.getBucket(),sourcePath, targetBucketName, targetPath);
+    }
+
+    @Override
+    public void copyFilesBetweenFyun(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) throws Exception {
+        if (ObjectUtils.isEmpty(pathMap)) {
+            return;
+        }
+        try {
+            for (Map.Entry<String, String> entry : pathMap.entrySet()) {
+                copyFileBetweenFyun(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue(), false);
+            }
+        } catch (Exception e) {
+            log.error("批量复制文件失败!");
+        } finally {
+            if (!ObjectUtils.isEmpty(ossClient)) {
+                ossClient.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void copyFilesBetweenFyun(String targetBucketName, Map<String, String> pathMap) throws Exception {
+        copyFilesBetweenFyun(fYunFileConfig.getBucket(),targetBucketName,pathMap);
+    }
+
+    @Override
+    public String getFileContent(String bucketName, String remoteFilePath) throws Exception {
+        try {
+            OSSObject ossObject = ossClient.getObject(bucketName, remoteFilePath);
+            InputStream objectContent = ossObject.getObjectContent();
+            StringBuilder contentJson = new StringBuilder();
+            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);
+            }
+            return contentJson.toString();
+        } catch (Exception e) {
+            log.error("获取文件内容失败:{}", remoteFilePath);
+            return null;
+        } finally {
+            if (!ObjectUtils.isEmpty(ossClient)) {
+                ossClient.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public String getFileContent(String remoteFilePath) throws Exception {
+        return getFileContent(fYunFileConfig.getBucket(), remoteFilePath);
+    }
+
+    @Override
+    public boolean fileExist(String bucket, String objectName) throws Exception {
+        try {
+            return ossClient.doesObjectExist(bucket, objectName);
+        } catch (Exception e) {
+            log.error("判断文件是否存在失败:{}", objectName);
+            return false;
+        } finally {
+            if (!ObjectUtils.isEmpty(ossClient)) {
+                ossClient.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public boolean fileExist(String objectName) throws Exception {
+        return fileExist(fYunFileConfig.getBucket(), objectName);
+    }
+
+    @Override
+    public void downloadFile(String bucket, String remoteFilePath, String localPath) throws Exception {
+        try {
+            File localFile = new File(localPath);
+            if(!localFile.getParentFile().exists()){
+                localFile.getParentFile().mkdirs();
+            }
+            DownloadFileRequest request = new DownloadFileRequest(bucket, remoteFilePath);
+            request.setDownloadFile(localPath);
+            // 默认5个任务并发下载
+            request.setTaskNum(5);
+            // 启动断点续传
+            request.setEnableCheckpoint(true);
+            ossClient.downloadFile(request);
+        } catch (Throwable throwable) {
+            log.error("文件下载失败:{}", remoteFilePath);
+            throwable.printStackTrace();
+        }finally {
+            if (!ObjectUtils.isEmpty(ossClient)) {
+                ossClient.shutdown();
+            }
+        }
+    }
+
+    @Override
+    public void downloadFile(String remoteFilePath, String localPath) throws Exception {
+        downloadFile(fYunFileConfig.getBucket(),remoteFilePath,localPath);
+    }
+}