24282 4 mēneši atpakaļ
vecāks
revīzija
bfed113e4b

+ 0 - 2
jy-framework/src/main/java/com/jy/framework/config/ObsConfig.java

@@ -17,8 +17,6 @@ public class ObsConfig {
 
     private String endPoint;
 
-    private String url;
-
     private String bucketName;
 
 }

+ 3 - 4
jy-starter/src/main/resources/application.yml

@@ -17,10 +17,9 @@ jy:
     - /open/**          # 开放接口
     - /test/**          # 测试接口
   obs:
-    ak: 9HNBVBHO7F3GLUCGTK5C
-    sk: ZowLEoMJrICA9tOyln0yWVm0xGSiupe0gnbsZimk
-    endPoint: obs.cn-south-1.myhuaweicloud.com
-    url: https://os.winfaster.cn/
+    ak: LTAI5tFR5gnBJd2B44VPdBP6
+    sk: ECIBUwSuuWc8kssyLHXxErPGuYhJNA
+    endPoint: oss-cn-fuzhou.aliyuncs.com
     bucketName: winfaster
 
 spring:

+ 35 - 0
jy-starter/src/test/java/MySpringBootTest.java

@@ -1,18 +1,53 @@
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
 import com.jy.JyApplication;
+import com.jy.business.payment.dao.PaymentRequestsDao;
+import com.jy.business.payment.model.entity.PaymentRequests;
+import com.jy.framework.config.ObsConfig;
+import jakarta.annotation.Resource;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 import org.springframework.test.context.web.WebAppConfiguration;
 
+import java.util.List;
+
 @RunWith(SpringRunner.class)
 @SpringBootTest(classes = JyApplication.class)
 @WebAppConfiguration
 public class MySpringBootTest {
 
+    @Resource
+    private PaymentRequestsDao paymentRequestsDao;
+
+    @Resource
+    private ObsConfig obsConfig;
+
     @Test
     public void test() {
+        List<PaymentRequests> list = paymentRequestsDao.list(q -> q.isNotNull(PaymentRequests::getAtts));
+
+        list.forEach(item -> {
+            String atts = item.getAtts();
+            List<JSONObject> javaList = JSONArray.parseArray(atts).toJavaList(JSONObject.class);
+
+            javaList.forEach(i -> {
+                String string = i.getString("url");
+                String replace = string.replace("https://os.winfaster.cn/", getUrl());
+                i.put("url", replace);
+            });
+
+            item.setAtts(JSONArray.toJSONString(javaList));
+        });
+
+        paymentRequestsDao.updateBatchById(list);
+
+        System.out.println(list);
+    }
 
+    private String getUrl() {
+        return "https://" + obsConfig.getBucketName() + "." + obsConfig.getEndPoint() + "/";
     }
 
 }

+ 2 - 2
jy-system/pom.xml

@@ -25,8 +25,8 @@
         </dependency>
 
         <dependency>
-            <groupId>com.huaweicloud</groupId>
-            <artifactId>esdk-obs-java</artifactId>
+            <groupId>com.aliyun.oss</groupId>
+            <artifactId>aliyun-sdk-oss</artifactId>
         </dependency>
 
     </dependencies>

+ 22 - 7
jy-system/src/main/java/com/jy/system/controller/FileConfig.java

@@ -4,11 +4,13 @@ import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.ObjectUtil;
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
+import com.aliyun.oss.model.PutObjectRequest;
 import com.jy.framework.config.JyConfig;
 import com.jy.framework.config.ObsConfig;
 import com.jy.framework.exception.ServiceException;
 import com.jy.framework.utils.AssertUtil;
-import com.obs.services.ObsClient;
 import jakarta.annotation.Resource;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -38,11 +40,11 @@ public class FileConfig {
     public Map<String, String> uploadFile(@RequestParam("file") MultipartFile file) {
         AssertUtil.notEmpty(file, "文件不存在");
 
-        String oriName = file.getOriginalFilename();
-        AssertUtil.notBlank(oriName, "文件名为空");
+        String fileName = file.getOriginalFilename();
+        AssertUtil.notBlank(fileName, "文件名为空");
 
         // 文件后缀名
-        String suffix = FileUtil.getSuffix(oriName);
+        String suffix = FileUtil.getSuffix(fileName);
 
         // 文件路径
         String objectKey = new StringJoiner("/")
@@ -52,15 +54,28 @@ public class FileConfig {
                 .add(IdUtil.fastSimpleUUID() + (ObjectUtil.isEmpty(suffix) ? "" : "." + suffix))
                 .toString();
 
+        OSS ossClient = getOssClient();
+
         // 上传文件
-        try (ObsClient obsClient = new ObsClient(obsConfig.getAk(), obsConfig.getSk(), obsConfig.getEndPoint())) {
-            obsClient.putObject(obsConfig.getBucketName(), objectKey, file.getInputStream());
+        try {
+            PutObjectRequest putObjectRequest = new PutObjectRequest(obsConfig.getBucketName(), objectKey, file.getInputStream());
+            ossClient.putObject(putObjectRequest);
         } catch (Exception e) {
             throw new ServiceException("上传失败");
+        } finally {
+            ossClient.shutdown();
         }
 
         // 返回文件路径
-        return Map.of("name", oriName, "url", obsConfig.getUrl() + objectKey);
+        return Map.of("name", fileName, "url", getUrl() + objectKey);
+    }
+
+    private OSS getOssClient() {
+        return new OSSClientBuilder().build(obsConfig.getEndPoint(), obsConfig.getAk(), obsConfig.getSk());
+    }
+
+    private String getUrl() {
+        return "https://" + obsConfig.getBucketName() + "." + obsConfig.getEndPoint() + "/";
     }
 
 }

+ 4 - 4
pom.xml

@@ -28,8 +28,8 @@
         <nashorn-core.version>15.4</nashorn-core.version>
         <beetl.version>3.17.0.RELEASE</beetl.version>
         <warm-flow.version>1.3.7</warm-flow.version>
-        <esdk-obs-java.version>3.24.9</esdk-obs-java.version>
         <easyexcel.version>4.0.3</easyexcel.version>
+        <oss-java.version>3.18.1</oss-java.version>
     </properties>
 
     <modules>
@@ -127,9 +127,9 @@
             </dependency>
 
             <dependency>
-                <groupId>com.huaweicloud</groupId>
-                <artifactId>esdk-obs-java</artifactId>
-                <version>${esdk-obs-java.version}</version>
+                <groupId>com.aliyun.oss</groupId>
+                <artifactId>aliyun-sdk-oss</artifactId>
+                <version>${oss-java.version}</version>
             </dependency>
 
             <dependency>