소스 검색

图片服务切换成miniIO

codingliang 1 년 전
부모
커밋
6ebc5cee3b

+ 17 - 6
pom.xml

@@ -41,7 +41,7 @@
         <fastjson.version>1.2.83</fastjson.version>
         <hutool.version>4.1.1</hutool.version>
         <lombok.version>1.18.4</lombok.version>
-
+        <okhttp3.version>4.11.0</okhttp3.version>
 
         <!--wagon plugin 配置-->
         <!--<service-path>/work/sz</service-path>
@@ -416,13 +416,24 @@
             <version>2.1.6</version>
         </dependency>
 
-        <!--<dependency>-->
-        <!--<groupId>com.baidu.aip</groupId>-->
-        <!--            <artifactId>java-sdk</artifactId>-->
-        <!--            <version>4.11.3</version>-->
-        <!--</dependency>-->
+        <!-- minio -->
+        <dependency>
+            <groupId>io.minio</groupId>
+            <artifactId>minio</artifactId>
+            <version>8.5.9</version>
+        </dependency>
     </dependencies>
 
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>com.squareup.okhttp3</groupId>
+                <artifactId>okhttp</artifactId>
+                <version>${okhttp3.version}</version>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
     <build>
         <finalName>${project.artifactId}</finalName>
         <extensions>

+ 26 - 0
src/main/java/com/sqx/config/MinioClientConfig.java

@@ -0,0 +1,26 @@
+package com.sqx.config;
+
+import io.minio.MinioClient;
+import lombok.RequiredArgsConstructor;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * minio客户端自动注入
+ *
+ * @author : codingliang
+ * @date : 2024-09-29 18:35
+ */
+@Configuration
+@RequiredArgsConstructor
+public class MinioClientConfig {
+    private final MinioConfig minioConfig;
+
+    @Bean
+    public MinioClient minioClient() {
+        return MinioClient.builder()
+                .endpoint(minioConfig.getEndpoint())
+                .credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey())
+                .build();
+    }
+}

+ 38 - 0
src/main/java/com/sqx/config/MinioConfig.java

@@ -0,0 +1,38 @@
+package com.sqx.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+/**
+ * minio配置
+ *
+ * @author : codingliang
+ * @date : 2024-09-29 18:32
+ */
+@Data
+@Component
+@ConfigurationProperties(prefix = "minio")
+public class MinioConfig {
+
+    /**
+     * endpoint
+     */
+    private String endpoint;
+
+    /**
+     * key
+     */
+    private String accessKey;
+
+    /**
+     * secret
+     */
+    private String secretKey;
+
+    /**
+     * bucket名称
+     */
+    private String bucket;
+
+}

+ 34 - 11
src/main/java/com/sqx/modules/file/AliFileUploadController.java

@@ -1,19 +1,28 @@
 package com.sqx.modules.file;
 
+import cn.hutool.core.util.StrUtil;
 import com.aliyun.oss.OSS;
 import com.aliyun.oss.OSSClientBuilder;
+import com.sqx.common.exception.SqxException;
 import com.sqx.common.utils.Result;
+import com.sqx.config.MinioConfig;
 import com.sqx.modules.common.service.CommonInfoService;
 import com.sqx.modules.file.utils.FileUploadUtils;
 import icu.xuyijie.secureapi.annotation.DecryptParam;
+import io.minio.MinioClient;
+import io.minio.ObjectWriteResponse;
+import io.minio.PutObjectArgs;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.jaudiotagger.audio.AudioFileIO;
 import org.jaudiotagger.audio.mp3.MP3AudioHeader;
 import org.jaudiotagger.audio.mp3.MP3File;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.ByteArrayInputStream;
@@ -34,19 +43,16 @@ import java.util.UUID;
  * @author fang
  * @date 2020/7/13
  */
-@RestController
+@Slf4j
 @Api(value = "阿里云文件上传", tags = {"阿里云文件上传"})
+@RestController
 @RequestMapping(value = "/alioss")
-@Slf4j
+@RequiredArgsConstructor
 public class AliFileUploadController {
 
-
     private final CommonInfoService commonRepository;
-
-    @Autowired
-    public AliFileUploadController(CommonInfoService commonRepository) {
-        this.commonRepository = commonRepository;
-    }
+    private final MinioClient minioClient;
+    private final MinioConfig minioConfig;
 
     @RequestMapping(value = "/upload", method = RequestMethod.POST)
     @ApiOperation("文件上传")
@@ -71,7 +77,24 @@ public class AliFileUploadController {
             String src = commonRepository.findOne(72).getValue()+"/"+completePath;
             // String src = commonRepository.findOne(19).getValue()+"/img/"+completePath;
             return Result.success().put("data",src);
-        }else{
+        } else if (StrUtil.equals(value, "3")) {
+            // Minio上传
+            String suffix = file.getOriginalFilename().substring(Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf("."));
+            String completePath = getPath(suffix);
+            try {
+                minioClient.putObject(PutObjectArgs.builder()
+                        .bucket(minioConfig.getBucket())
+                        .object(completePath)
+                        .stream(file.getInputStream(), file.getSize(), -1)
+                        .build());
+
+                String src = commonRepository.findOne(72).getValue() + "/" + completePath;
+                return Result.success().put("data", src);
+            } catch (Exception e) {
+                throw new SqxException("文件上传失败!");
+            }
+
+        } else{
             try
             {
                 String http = commonRepository.findOne(19).getValue();

+ 7 - 0
src/main/resources/application-dev.yml

@@ -108,3 +108,10 @@ secure-api:
 
 mp:
     temp: true
+
+# minio配置
+minio:
+    endpoint: http://172.16.20.105:9000
+    accessKey: YTDm6Wtjf1q0LbGi2SKK
+    secretKey: MU1KozgxyknGDPwcKuvCEuhZwL37wbryHOFRW5Xt
+    bucket: wmfile

+ 7 - 0
src/main/resources/application-prod.yml

@@ -109,3 +109,10 @@ secure-api:
 
 mp:
     temp: false
+
+# minio配置
+minio:
+    endpoint: http://172.16.20.105:9000
+    accessKey: YTDm6Wtjf1q0LbGi2SKK
+    secretKey: MU1KozgxyknGDPwcKuvCEuhZwL37wbryHOFRW5Xt
+    bucket: wmfile