zyp 1 week ago
parent
commit
63bf0fb791

+ 2 - 2
fs-service/src/main/java/com/fs/course/service/impl/TencentCloudCosService.java

@@ -51,10 +51,10 @@ public class TencentCloudCosService implements ITencentCloudCosService {
 
     private static final String YL_SECRET_ID = "AKIDiMq9lDf2EOM9lIfqqfKo7FNgM5meD0sT";
     private static final String YL_SECRET_KEY = "u5SuS80342xzx8FRBukza9lVNHKNMSaB";
-    private static final String YL_BUCKET = "myhk-1323137866";
+    private static final String YL_BUCKET = "jiuzhou-1323137866";
     private static final String YL_APP_ID = "1323137866";
     private static final String YL_REGION = "ap-chongqing";
-    private static final String YL_PROXY = "myhk";
+    private static final String YL_PROXY = "jiuzhou";
     @Override
     public R getTmpSecretKey() {
         TreeMap<String, Object> config = new TreeMap<String, Object>();

+ 6 - 2
fs-service/src/main/java/com/fs/system/oss/CloudConstant.java

@@ -23,7 +23,11 @@ public class CloudConstant
         /**
          * 腾讯云
          */
-        QCLOUD(3);
+        QCLOUD(3),
+        /**
+         * 华为云
+         */
+        HUAWEI(4);
         private int value;
 
         CloudService(int value)
@@ -36,4 +40,4 @@ public class CloudConstant
             return value;
         }
     }
-}
+}

+ 57 - 0
fs-service/src/main/java/com/fs/system/oss/CloudStorageConfig.java

@@ -2,6 +2,7 @@ package com.fs.system.oss;
 
 
 import com.fs.system.oss.valdator.AliyunGroup;
+import com.fs.system.oss.valdator.HuaweiGroup;
 import com.fs.system.oss.valdator.QcloudGroup;
 import com.fs.system.oss.valdator.QiniuGroup;
 import org.hibernate.validator.constraints.Range;
@@ -92,6 +93,62 @@ public class CloudStorageConfig implements Serializable
     @NotBlank(message = "所属地区不能为空", groups = QcloudGroup.class)
     private String qcloudRegion;
 
+    @NotBlank(message = "华为云BucketName不能为空", groups = HuaweiGroup.class)
+    private String huaweiBucketName;
+
+    @NotBlank(message = "华为云Endpoint不能为空", groups = HuaweiGroup.class)
+    private String huaweiEndpoint;
+
+    @NotBlank(message = "华为云AK不能为空", groups = HuaweiGroup.class)
+    private String huaweiAK;
+
+    @NotBlank(message = "华为云SK不能为空", groups = HuaweiGroup.class)
+    private String huaweiSK;
+
+    @NotBlank(message = "华为云绑定的域名不能为空", groups = HuaweiGroup.class)
+    @URL(message = "华为云绑定的域名格式不正确", groups = HuaweiGroup.class)
+    private String huaweiDomain;
+
+    public String getHuaweiBucketName() {
+        return huaweiBucketName;
+    }
+
+    public void setHuaweiBucketName( String huaweiBucketName) {
+        this.huaweiBucketName = huaweiBucketName;
+    }
+
+    public String getHuaweiEndpoint() {
+        return huaweiEndpoint;
+    }
+
+    public void setHuaweiEndpoint( String huaweiEndpoint) {
+        this.huaweiEndpoint = huaweiEndpoint;
+    }
+
+    public  String getHuaweiAK() {
+        return huaweiAK;
+    }
+
+    public void setHuaweiAK( String huaweiAK) {
+        this.huaweiAK = huaweiAK;
+    }
+
+    public  String getHuaweiSK() {
+        return huaweiSK;
+    }
+
+    public void setHuaweiSK( String huaweiSK) {
+        this.huaweiSK = huaweiSK;
+    }
+
+    public  String getHuaweiDomain() {
+        return huaweiDomain;
+    }
+
+    public void setHuaweiDomain( String huaweiDomain) {
+        this.huaweiDomain = huaweiDomain;
+    }
+
     public static long getSerialVersionUID() {
         return serialVersionUID;
     }

+ 78 - 0
fs-service/src/main/java/com/fs/system/oss/HuaweiCloudStorageService.java

@@ -0,0 +1,78 @@
+package com.fs.system.oss;
+
+import com.fs.common.exception.file.OssException;
+import com.obs.services.ObsClient;
+import com.obs.services.exception.ObsException;
+import com.obs.services.model.PutObjectRequest;
+import org.apache.commons.io.IOUtils;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import static com.fs.course.service.impl.HuaweiObsServiceImpl.generateUploadFileName;
+
+/**
+ * 腾讯云存储
+ */
+public class HuaweiCloudStorageService extends CloudStorageService
+{
+    private ObsClient client;
+
+    public HuaweiCloudStorageService(CloudStorageConfig config)
+    {
+        this.config = config;
+        // 初始化
+        init();
+    }
+
+    private void init()
+    {
+        client = new ObsClient(config.getHuaweiAK(), config.getHuaweiSK(), config.getHuaweiEndpoint());
+    }
+
+    @Override
+    public String upload(byte[] data, String path)
+    {
+        String objKey = generateUploadFileName(path);
+        try {
+            // 文件上传
+            PutObjectRequest request = new PutObjectRequest(config.getHuaweiBucketName(), objKey,new ByteArrayInputStream(data));
+            client.putObject(request);
+        } catch (ObsException e) {
+            throw new ObsException("文件上传失败," + e.getErrorMessage());
+        } catch (Exception e) {
+            System.out.println("上传失败");
+            // 其他异常信息打印
+            return "上传失败"+e.getMessage();
+        }
+        return config.getHuaweiDomain()+"/"+ objKey;
+
+    }
+
+    @Override
+    public String upload(InputStream inputStream, String path)
+    {
+        try
+        {
+            byte[] data = IOUtils.toByteArray(inputStream);
+            return this.upload(data, path);
+        }
+        catch (IOException e)
+        {
+            throw new OssException("上传文件失败");
+        }
+    }
+
+    @Override
+    public String uploadSuffix(byte[] data, String suffix)
+    {
+        return upload(data, getPath(config.getQcloudPrefix(), suffix));
+    }
+
+    @Override
+    public String uploadSuffix(InputStream inputStream, String suffix)
+    {
+        return upload(inputStream, getPath(config.getQcloudPrefix(), suffix));
+    }
+}

+ 4 - 0
fs-service/src/main/java/com/fs/system/oss/OSSFactory.java

@@ -33,6 +33,10 @@ public final class OSSFactory
         {
             return new QcloudCloudStorageService(config);
         }
+        else if (config.getType() == CloudConstant.CloudService.HUAWEI.getValue())
+        {
+            return new HuaweiCloudStorageService(config);
+        }
         return null;
     }
 }

+ 8 - 0
fs-service/src/main/java/com/fs/system/oss/valdator/HuaweiGroup.java

@@ -0,0 +1,8 @@
+package com.fs.system.oss.valdator;
+
+/**
+ * 华为
+ */
+public interface HuaweiGroup
+{
+}