Pārlūkot izejas kodu

修改本地命令上传逻辑

tianboguang 3 gadi atpakaļ
vecāks
revīzija
bb154f018f

+ 19 - 6
4dkankan-utils-fyun-local/src/main/java/com/fdkankan/fyun/local/LocalFileService.java

@@ -2,6 +2,7 @@ package com.fdkankan.fyun.local;
 
 import cn.hutool.core.io.FileUtil;
 import com.fdkankan.fyun.constant.FYunConstants;
+import com.fdkankan.fyun.constant.FYunTypeEnum;
 import com.fdkankan.fyun.face.AbstractFYunFileService;
 import com.fdkankan.fyun.local.constant.LocalConstants;
 import org.slf4j.Logger;
@@ -45,16 +46,28 @@ public class LocalFileService extends AbstractFYunFileService {
 
     @Override
     public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
-        String ossPath = bucket + "/" + remoteFilePath;
         try {
-            String command = String.format(FYunConstants.UPLOAD_SH, ossPath, filePath);
-            log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath);
+            String optType = new File(filePath).isDirectory() ? "folder" : "file";
+            String command = String.format(FYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType);
+            log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
             callshell(command);
-            return null;
         } catch (Exception e) {
-            log.error("上传文件失败, ossPath:{}, srcPath:{}", ossPath, filePath);
+            log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    @Override
+    public void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) {
+        try {
+            String optType = new File(filePath).isDirectory() ? "folder" : "file";
+            String command = String.format(FYunConstants.DOWNLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType);
+            log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
+            callshell(command);
+        } catch (Exception e) {
+            log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
             e.printStackTrace();
-            return null;
         }
     }
 

+ 19 - 5
4dkankan-utils-fyun-oss/src/main/java/com/fdkankan/fyun/oss/OssFileService.java

@@ -2,8 +2,9 @@ package com.fdkankan.fyun.oss;
 
 import com.aliyun.oss.OSS;
 import com.aliyun.oss.model.*;
+import com.fdkankan.fyun.constant.FYunConstants;
+import com.fdkankan.fyun.constant.FYunTypeEnum;
 import com.fdkankan.fyun.face.AbstractFYunFileService;
-import com.fdkankan.fyun.oss.constant.OssConstants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -80,19 +81,32 @@ public class OssFileService extends AbstractFYunFileService {
 
     @Override
     public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
-        String ossPath = bucket + "/" + remoteFilePath;
         try {
-            String command = String.format(OssConstants.UPLOAD_SH, ossPath, filePath);
-            log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath);
+            String optType = new File(filePath).isDirectory() ? "folder" : "file";
+            String command = String.format(FYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.OSS.code(), optType);
+            log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
             callshell(command);
         } catch (Exception e) {
-            log.error("上传文件失败, ossPath:{}, srcPath:{}", ossPath, filePath);
+            log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
             e.printStackTrace();
         }
         return null;
     }
 
     @Override
+    public void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) {
+        try {
+            String optType = new File(filePath).isDirectory() ? "folder" : "file";
+            String command = String.format(FYunConstants.DOWNLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType);
+            log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
+            callshell(command);
+        } catch (Exception e) {
+            log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
+            e.printStackTrace();
+        }
+    }
+
+    @Override
     public void deleteFile(String bucket, String remoteFilePath) throws IOException {
         try {
             ossClient.deleteObject(bucket, remoteFilePath);

+ 17 - 4
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/constant/FYunConstants.java

@@ -1,10 +1,23 @@
 package com.fdkankan.fyun.constant;
 
+import org.springframework.beans.factory.annotation.Value;
+
 public class FYunConstants {
+
+
+    public static String shellPath;
+
     /**
-     * oss文件上传命令
-     * 第一个参数是oss路径,要包含bucket名称
-     * 第二个参数是本地文件路径
+     * oss文件上传命令 bash /opt/ossutil/fyun-upload.sh {bucket} {srcPath} {destPath} {fyunType} {opType}
+     * opType: file or folder
+     * 参考:http://showdoc.4dage.com/web/#/45?page_id=784
+     * 例:下载目录:bash /opt/ossutil/fyun-download.sh 4dkankan /home/XXX /mnt/XXX oss folder
      */
-    public static final String UPLOAD_SH = "bash /opt/ossutil/upload.sh %s %s";
+    public static final String UPLOAD_SH = "bash ".concat(shellPath).concat("/fyun-upload.sh %s %s /%s %s %s");
+    public static final String DOWNLOAD_SH = "bash ".concat(shellPath).concat("/fyun-upload.sh %s /%s %s %s %s");
+
+    @Value("${fyun.shell.path:/opt/ossutil}")
+    public void setShellPath(String path){
+        FYunConstants.shellPath = path;
+    }
 }

+ 5 - 0
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/face/AbstractFYunFileService.java

@@ -48,6 +48,11 @@ public abstract class AbstractFYunFileService implements FYunFileServiceInterfac
     }
 
     @Override
+    public void downloadFileByCommand(String filePath, String remoteFilePath) {
+        downloadFileByCommand(fYunFileConfig.getBucket(),filePath,remoteFilePath);
+    }
+
+    @Override
     public void deleteFile(String remoteFilePath) throws IOException {
         deleteFile(fYunFileConfig.getBucket(), remoteFilePath);
     }

+ 18 - 0
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/face/FYunFileServiceInterface.java

@@ -81,6 +81,24 @@ public interface FYunFileServiceInterface {
     String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) ;
 
     /**
+     * 通过本地脚本上传
+     *
+     * @param bucket
+     * @param filePath
+     * @param remoteFilePath
+     */
+    void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) ;
+
+    /**
+     * 通过本地脚本上传
+     *
+     * @param bucket
+     * @param filePath
+     * @param remoteFilePath
+     */
+    void downloadFileByCommand(String filePath, String remoteFilePath) ;
+
+    /**
      * 通过本地脚本上传至系统默认bucket
      *
      * @param filePath

+ 18 - 4
4dkankan-utils-fyun-s3/src/main/java/com/fdkankan/fyun/s3/S3FileService.java

@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
 import com.amazonaws.services.s3.AmazonS3;
 import com.amazonaws.services.s3.model.*;
 import com.fdkankan.fyun.constant.FYunConstants;
+import com.fdkankan.fyun.constant.FYunTypeEnum;
 import com.fdkankan.fyun.face.AbstractFYunFileService;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
@@ -90,19 +91,32 @@ public class S3FileService extends AbstractFYunFileService {
 
     @Override
     public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath){
-        String ossPath = bucket + "/" + remoteFilePath;
         try {
-            String command = String.format(FYunConstants.UPLOAD_SH, ossPath, filePath);
-            log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath);
+            String optType = new File(filePath).isDirectory() ? "folder" : "file";
+            String command = String.format(FYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.AWS.code(), optType);
+            log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
             callshell(command);
         } catch (Exception e) {
-            log.error("上传文件失败, ossPath:{}, srcPath:{}", ossPath, filePath);
+            log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
             e.printStackTrace();
         }
         return null;
     }
 
     @Override
+    public void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) {
+        try {
+            String optType = new File(filePath).isDirectory() ? "folder" : "file";
+            String command = String.format(FYunConstants.DOWNLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.LOCAL.code(), optType);
+            log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
+            callshell(command);
+        } catch (Exception e) {
+            log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
+            e.printStackTrace();
+        }
+    }
+
+    @Override
     public void deleteFile(String bucket, String remoteFilePath){
         if (remoteFilePath.startsWith("/")) {
             remoteFilePath = remoteFilePath.substring(1);