From 78ade5f37566bf1287f53796a34576f894e5fb1c Mon Sep 17 00:00:00 2001 From: anthonywj Date: Sat, 5 Aug 2023 16:40:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eminio=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 731 +++++++++--------- .../java/com/glxp/api/config/MinioConfig.java | 51 ++ .../java/com/glxp/api/util/MinioUtil.java | 156 ++++ src/main/resources/application-dev.yml | 4 + src/main/resources/application-test.yml | 6 +- 5 files changed, 583 insertions(+), 365 deletions(-) create mode 100644 src/main/java/com/glxp/api/config/MinioConfig.java create mode 100644 src/main/java/com/glxp/api/util/MinioUtil.java diff --git a/pom.xml b/pom.xml index 6ce2afe..9b2d23a 100644 --- a/pom.xml +++ b/pom.xml @@ -22,378 +22,381 @@ 1.8 - - - - - com.alibaba - fastjson - 2.0.4 - - - - - org.apache.commons - commons-lang3 - 3.9 - - - - org.projectlombok - lombok - true - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-tomcat - - - - - javax.servlet - javax.servlet-api - 3.1.0 - provided - - - - org.springframework.boot - spring-boot-configuration-processor - true - + + + + + com.alibaba + fastjson + 2.0.4 + + + + + org.apache.commons + commons-lang3 + 3.9 + + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + + org.springframework.boot + spring-boot-configuration-processor + true + - - org.springframework.boot - spring-boot-starter-aop - + + org.springframework.boot + spring-boot-starter-aop + - - - org.springframework.boot - spring-boot-starter-data-redis - + + + org.springframework.boot + spring-boot-starter-data-redis + - - - org.springframework.boot - spring-boot-devtools - true - + + + org.springframework.boot + spring-boot-devtools + true + - - - - org.springframework.boot - spring-boot-starter-jdbc - - - mysql - mysql-connector-java - 8.0.21 - runtime - - - - com.oracle - ojdbc14 - 10.2.0.4.0 - pom - - - - - com.baomidou - mybatis-plus-boot-starter - 3.5.2 - - - - - com.github.pagehelper - pagehelper-spring-boot-starter - 1.4.2 - - - - org.mybatis - mybatis - - - org.mybatis - mybatis-spring - - - - - - - p6spy - p6spy - 3.9.1 - - - - - org.projectlombok - lombok - true - - - - - io.jsonwebtoken - jjwt - 0.9.1 - - - - org.springframework.boot - spring-boot-starter-test - test - + + + + org.springframework.boot + spring-boot-starter-jdbc + + + mysql + mysql-connector-java + 8.0.21 + runtime + + + + com.oracle + ojdbc14 + 10.2.0.4.0 + pom + + + + + com.baomidou + mybatis-plus-boot-starter + 3.5.2 + + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.4.2 + + + + org.mybatis + mybatis + + + org.mybatis + mybatis-spring + + + + + + + p6spy + p6spy + 3.9.1 + + + + + org.projectlombok + lombok + true + + + + + io.jsonwebtoken + jjwt + 0.9.1 + + + + org.springframework.boot + spring-boot-starter-test + test + - - org.springframework.boot - spring-boot-starter-tomcat - provided - - - javax.xml.bind - jaxb-api - 2.3.0 - - - com.fasterxml.jackson.core - jackson-databind - 2.9.2 - - - com.github.pagehelper - pagehelper-spring-boot-autoconfigure - 1.4.5 - - - org.springframework.boot - spring-boot-starter-websocket - - - - - - org.apache.poi - poi - 4.0.0 - - - - org.apache.poi - poi-ooxml - 4.0.0 - - - - - com.glxp - tochina - 1.0 - - - com.itextpdf - itextpdf - 5.5.13.1 - - - com.itextpdf - itext-asian - 5.2.0 - - - - - cn.hutool - hutool-all - 5.8.18 - - - com.belerweb - pinyin4j - 2.5.0 - - - - com.google.zxing - core - 3.3.3 - - - com.google.zxing - javase - 3.3.3 - - - org.olap4j - olap4j - 1.2.0 - - - - - - - - net.sf.barcode4j - barcode4j - 2.1 - - - org.olap4je - olap4j - - - - - - net.sf.jasperreports - jasperreports - 6.5.1 - - - com.lowagie - itext - - - - - - net.sf.jasperreports - jasperreports-fonts - 6.0.0 - - - - - com.itextpdf - itext-pdfa - 5.5.0 - - - org.codehaus.groovy - groovy - 3.0.7 - - - cn.lesper - iTextAsian - 3.0 - - - - com.alibaba - easyexcel - 2.2.10 - - - - - hu.blackbelt.bundles.swagger-parser - io.swagger.parser - 1.0.47_1 - - - - - - - - org.apache.xmlgraphics - batik-bridge - 1.11 - - - - org.apache.xmlgraphics - batik-dom - 1.11 - - - - org.eclipse.birt.runtime.3_7_1 - com.lowagie.text - 2.1.7 - - - com.squareup.okhttp3 - okhttp - 3.10.0 - - - - - - - cn.dev33 - sa-token-spring-boot-starter - 1.30.0 - - - org.apache.commons - commons-text - 1.1 - - - - - org.springframework.plugin - spring-plugin-core - 2.0.0.RELEASE - - - org.springframework.plugin - spring-plugin-metadata - 2.0.0.RELEASE - - - com.github.xiaoymin - knife4j-spring-boot-starter - 2.0.9 - - - - org.bouncycastle - bcprov-jdk15to18 - 1.68 - - - org.springframework.boot - spring-boot-starter-amqp - - - - org.springframework.boot - spring-boot-starter-amqp - + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + javax.xml.bind + jaxb-api + 2.3.0 + + + com.fasterxml.jackson.core + jackson-databind + 2.9.2 + + + com.github.pagehelper + pagehelper-spring-boot-autoconfigure + 1.4.5 + + + org.springframework.boot + spring-boot-starter-websocket + + + + + + org.apache.poi + poi + 4.0.0 + + + + org.apache.poi + poi-ooxml + 4.0.0 + + + + + com.glxp + tochina + 1.0 + + + com.itextpdf + itextpdf + 5.5.13.1 + + + com.itextpdf + itext-asian + 5.2.0 + + + + + cn.hutool + hutool-all + 5.8.18 + + + com.belerweb + pinyin4j + 2.5.0 + + + + com.google.zxing + core + 3.3.3 + + + com.google.zxing + javase + 3.3.3 + + + org.olap4j + olap4j + 1.2.0 + + + + + + + + net.sf.barcode4j + barcode4j + 2.1 + + + org.olap4je + olap4j + + + + + + net.sf.jasperreports + jasperreports + 6.5.1 + + + com.lowagie + itext + + + + + + net.sf.jasperreports + jasperreports-fonts + 6.0.0 + + + + + com.itextpdf + itext-pdfa + 5.5.0 + + + org.codehaus.groovy + groovy + 3.0.7 + + + cn.lesper + iTextAsian + 3.0 + + + + com.alibaba + easyexcel + 2.2.10 + + + + + hu.blackbelt.bundles.swagger-parser + io.swagger.parser + 1.0.47_1 + + + + + + org.apache.xmlgraphics + batik-bridge + 1.11 + + + + org.apache.xmlgraphics + batik-dom + 1.11 + + + + org.eclipse.birt.runtime.3_7_1 + com.lowagie.text + 2.1.7 + + + com.squareup.okhttp3 + okhttp + 3.10.0 + + + + + + + cn.dev33 + sa-token-spring-boot-starter + 1.30.0 + + + org.apache.commons + commons-text + 1.1 + + + + + org.springframework.plugin + spring-plugin-core + 2.0.0.RELEASE + + + org.springframework.plugin + spring-plugin-metadata + 2.0.0.RELEASE + + + com.github.xiaoymin + knife4j-spring-boot-starter + 2.0.9 + + + + org.bouncycastle + bcprov-jdk15to18 + 1.68 + + + org.springframework.boot + spring-boot-starter-amqp + + + + org.springframework.boot + spring-boot-starter-amqp + - - - org.springframework.boot - spring-boot-starter-mail - - - + + + org.springframework.boot + spring-boot-starter-mail + + + org.springframework.boot spring-boot-starter-freemarker - - - - + + + + io.minio + minio + 3.0.10 + + + UDI_CPT_SERVER diff --git a/src/main/java/com/glxp/api/config/MinioConfig.java b/src/main/java/com/glxp/api/config/MinioConfig.java new file mode 100644 index 0000000..39f1405 --- /dev/null +++ b/src/main/java/com/glxp/api/config/MinioConfig.java @@ -0,0 +1,51 @@ +package com.glxp.api.config; + +import io.minio.MinioClient; +import io.minio.errors.InvalidEndpointException; +import io.minio.errors.InvalidPortException; +import lombok.Data; +import lombok.RequiredArgsConstructor; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@RequiredArgsConstructor +public class MinioConfig { + + @Data + @ConfigurationProperties("minio") + @Configuration + public class MinioProperties { + /** + * 服务地址 + */ + private String endpoint; + + + /** + * 用户名 + */ + private String accessKey; + + /** + * 密码 + */ + private String secretKey; + } + + + /** + * 初始化客户端 + * + * @return 客户端 + */ + @Bean + public MinioClient minioClient(MinioProperties minioProperties) { + try { + return new MinioClient(minioProperties.getEndpoint(), minioProperties.getAccessKey(), minioProperties.getSecretKey()); + } catch (InvalidEndpointException | InvalidPortException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/com/glxp/api/util/MinioUtil.java b/src/main/java/com/glxp/api/util/MinioUtil.java new file mode 100644 index 0000000..4b82148 --- /dev/null +++ b/src/main/java/com/glxp/api/util/MinioUtil.java @@ -0,0 +1,156 @@ +package com.glxp.api.util; + +import cn.hutool.core.io.FileUtil; +import io.minio.MinioClient; +import io.minio.errors.*; +import io.minio.http.Method; +import io.minio.policy.PolicyType; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; +import org.xmlpull.v1.XmlPullParserException; + +import java.io.IOException; +import java.io.InputStream; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +/** + * minio工具类 + */ +@Slf4j +@Component +public class MinioUtil { + + private static MinioClient minioClient; + + @Autowired + public void setMinioClient(MinioClient minioClient) { + MinioUtil.minioClient = minioClient; + } + + /** + * 如不存在则创建存储bucket + * + * @return Boolean + */ + private static void makeBucket(String bucketName) { + try { + + boolean exists = minioClient.bucketExists(bucketName); + if (!exists) { + minioClient.makeBucket(bucketName); + minioClient.setBucketPolicy(bucketName, "*", PolicyType.READ_ONLY); + } + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + + /** + * 上传文件 + * + * @param fileFullName 文件全地址 + * @param file 文件 + */ + public static void uploadFile(String fileFullName, MultipartFile file) { + try { + //从fileName中获取bucketName和objectName + String reg = "/(\\S*?)(/\\S*)"; + String bucketName = fileFullName.replaceFirst(reg, "$1"); + String objectName = fileFullName.replaceFirst(reg, "$2"); + makeBucket(bucketName); + minioClient.putObject(bucketName, objectName, file.getInputStream(), FileUtil.getMimeType(fileFullName)); + } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException | + InvalidKeyException | ErrorResponseException | InternalException | InvalidArgumentException | + XmlPullParserException | NoResponseException e) { + throw new RuntimeException(e); + } + } + + /** + * 上传文件 内置关闭流 + * + * @param fileFullName 文件全地址 + * @param file 文件 + * @param fileContentType 文件contentType + * @throws Exception + */ + public static void uploadFile(String fileFullName, InputStream file, String fileContentType) throws InvalidArgumentException, XmlPullParserException, NoResponseException { + try { + //从fileName中获取bucketName和objectName + String reg = "/(\\S*?)(/\\S*)"; + String bucketName = fileFullName.replaceFirst(reg, "$1"); + String objectName = fileFullName.replaceFirst(reg, "$2"); + makeBucket(bucketName); + minioClient.putObject(bucketName, objectName, file, fileContentType); + } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException | + InvalidKeyException | ErrorResponseException | + InternalException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } finally { + try { + file.close(); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + } + } + + /** + * 获取文件地址带有效期 + * + * @param fileFullName 文件全名称带路径 + * @param expires 过期时间 + * @return 完整访问地址 + */ + public static String getPresignedObjectUrl(String fileFullName, Integer expires) { + String reg = "/(\\S*?)(/\\S*)"; + String bucketName = fileFullName.replaceFirst(reg, "$1"); + String objectName = fileFullName.replaceFirst(reg, "$2"); + try { + return minioClient.getPresignedObjectUrl(Method.GET, bucketName, objectName, expires, null); + } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException | + InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException | + InternalException | InvalidExpiresRangeException e) { + throw new RuntimeException(e); + } + } + + + /** + * 获取文件地址带有效期 + * + * @param fileFullName 文件全名称带路径 + * @return inputStream + */ + public static InputStream getFileInputStream(String fileFullName) { + String reg = "/(\\S*?)(/\\S*)"; + String bucketName = fileFullName.replaceFirst(reg, "$1"); + String objectName = fileFullName.replaceFirst(reg, "$2"); + try { + return minioClient.getObject(bucketName, objectName); + } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException | + InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException | + InternalException | InvalidArgumentException e) { + throw new RuntimeException(e); + } + } + + public static void deleteFile(String fileFullName) { + String reg = "/(\\S*?)(/\\S*)"; + String bucketName = fileFullName.replaceFirst(reg, "$1"); + String objectName = fileFullName.replaceFirst(reg, "$2"); + try { + minioClient.removeObject(bucketName, objectName); + } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException | + InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException | + InternalException e) { +// throw new RuntimeException(e); + } + } + +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 2946190..0a4d863 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -69,6 +69,10 @@ ok: max-idle-connections: 200 keep-alive-duration: 300 +minio: + endpoint: http://139.9.219.60:9000 + access-key: minioadmin + secret-key: minioadmin logging: level: diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index f101988..e3c35ee 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -46,7 +46,11 @@ ok: max-idle-connections: 200 keep-alive-duration: 300 - +minio: + endpoint: http://139.9.219.60:9000 + access-key: minioadmin + secret-key: minioadmin + logging: level: com.glxp.api.dao: debug