Jelajahi Sumber

添加OSS复制文件和移动文件接口

xuexiaobo 5 tahun lalu
induk
melakukan
395ed605ac

+ 5 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/ctrl/TestController.java

@@ -247,4 +247,9 @@ public class TestController {
         response.setHeader("P3P","CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");
         return "sessionId: " + sessionId;
     }
+
+    @RequestMapping(value = "/api/common/video/move", method = RequestMethod.GET)
+    public String moveVideo(@RequestParam("source") String sourceFile, @RequestParam("target") String targetFile){
+        return ossLogic.copyFileInOss("mooctest-course",sourceFile, targetFile);
+    }
 }

+ 6 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/logic/OSSLogic.java

@@ -50,6 +50,12 @@ public interface OSSLogic {
 
     boolean upload2DevBySigUrl(String signedUrl, File localFilePath);
 
+    boolean deleteByUrl(String bucketName, String ossFileNameWithPath);
+
+    String copyFileInOss(String bucketName, String sourceFileNameWithPath, String targetFileNameWithPath);
+
+    String moveFileInOss(String bucketName, String sourceFileNameWithPath, String targetFileNameWithPath);
+
     List<String> listObjectsByPrefix(String prefix);
 
     String saveFile(MultipartFile file, String path) throws IOException;

+ 28 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/logic/impl/OSSLogicImpl.java

@@ -346,6 +346,34 @@ public class OSSLogicImpl implements OSSLogic {
     }
 
     @Override
+    public boolean deleteByUrl(String bucketName, String ossFileNameWithPath) {
+        try{
+            ossClient.deleteObject(bucketName, ossFileNameWithPath);
+            return true;
+        } catch (Exception e){
+            log.error("删除OSS存储对象出错", e);
+            return false;
+        }
+
+    }
+
+    @Override
+    public String copyFileInOss(String bucketName, String sourceFileNameWithPath, String targetFileNameWithPath) {
+        ossClient.copyObject(bucketName, sourceFileNameWithPath, bucketName, targetFileNameWithPath);
+        StringBuilder sb = new StringBuilder("http://");
+        String nohttpEndPoint = endPoint.replace("http://","");
+        sb.append(bucketName).append(".").append(nohttpEndPoint).append("/").append(targetFileNameWithPath);
+        return sb.toString();
+    }
+
+    @Override
+    public String moveFileInOss(String bucketName, String sourceFileNameWithPath, String targetFileNameWithPath) {
+        String targetUrl = this.copyFileInOss(bucketName, sourceFileNameWithPath, targetFileNameWithPath);
+        this.deleteByUrl(bucketName, sourceFileNameWithPath);
+        return targetUrl;
+    }
+
+    @Override
     public List<String> listObjectsByPrefix(String prefix) {
         ObjectListing objectListing = ossClient.listObjects(bucketName,prefix);
         List<OSSObjectSummary> sums = objectListing.getObjectSummaries();