From cfa6fc7d565f09d2ea1f7fece7e102ea0fca8406 Mon Sep 17 00:00:00 2001 From: dashan Date: Mon, 27 Nov 2023 23:03:03 +0800 Subject: [PATCH] =?UTF-8?q?feat(oss):=E6=96=B0=E5=A2=9Eoss=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E5=AD=98=E5=82=A8=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 2 + .idea/encodings.xml | 1 + ds-commons/ds-config/ds-oss-config/pom.xml | 56 +++++++++++ .../common/oss/bean/MinioConfiguration.java | 32 ++++++ .../com/ds/common/oss/bean/OssProperties.java | 49 +++++++++ .../com/ds/common/oss/core/OssTemplate.java | 99 +++++++++++++++++++ .../java/com/ds/common/oss/pojo/OssFile.java | 46 +++++++++ ds-commons/ds-config/pom.xml | 1 + pom.xml | 5 + 9 files changed, 291 insertions(+) create mode 100644 ds-commons/ds-config/ds-oss-config/pom.xml create mode 100644 ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/bean/MinioConfiguration.java create mode 100644 ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/bean/OssProperties.java create mode 100644 ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/core/OssTemplate.java create mode 100644 ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/pojo/OssFile.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 8b52174..2e8e550 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -14,6 +14,7 @@ + @@ -37,6 +38,7 @@ + diff --git a/.idea/encodings.xml b/.idea/encodings.xml index b29e007..2fd322d 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -4,6 +4,7 @@ + diff --git a/ds-commons/ds-config/ds-oss-config/pom.xml b/ds-commons/ds-config/ds-oss-config/pom.xml new file mode 100644 index 0000000..7e795cd --- /dev/null +++ b/ds-commons/ds-config/ds-oss-config/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + + com.ds + ds-config + 0.0.1-SNAPSHOT + + + com.ds.common.oss + ds-oss-config + + + 17 + 17 + UTF-8 + + + + + org.springframework + spring-context + + + io.minio + minio + + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot + + + org.springframework.boot + spring-boot-autoconfigure + + + org.springframework + spring-web + + + org.apache.tomcat.embed + tomcat-embed-core + + + jakarta.validation + jakarta.validation-api + + + + \ No newline at end of file diff --git a/ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/bean/MinioConfiguration.java b/ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/bean/MinioConfiguration.java new file mode 100644 index 0000000..bfdb2a1 --- /dev/null +++ b/ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/bean/MinioConfiguration.java @@ -0,0 +1,32 @@ +package com.ds.common.oss.bean; + +import io.minio.MinioClient; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author ds + * @since 2023/11/27 + */ + +@Configuration +@EnableConfigurationProperties(OssProperties.class) +@ConditionalOnProperty(value = "oss.name", havingValue = "minio") +public class MinioConfiguration { + + private final OssProperties ossProperties; + + public MinioConfiguration(OssProperties ossProperties){ + this.ossProperties=ossProperties; + } + + @Bean + public MinioClient minioClient() { + return MinioClient.builder() + .endpoint(ossProperties.getEndpoint()) + .credentials(ossProperties.getAccessKey(), ossProperties.getSecretKey()) + .build(); + } +} diff --git a/ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/bean/OssProperties.java b/ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/bean/OssProperties.java new file mode 100644 index 0000000..610b518 --- /dev/null +++ b/ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/bean/OssProperties.java @@ -0,0 +1,49 @@ +package com.ds.common.oss.bean; + +import lombok.Data; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.util.Assert; + +/** + * @author ds + * @since 2023/11/27 + */ +@Data +@ConfigurationProperties(prefix = OssProperties.PREFIX) +public class OssProperties implements InitializingBean { + + /** + * 配置前缀 + */ + public static final String PREFIX = "oss"; + + /** + * 对象存储名称 + */ + private String name; + + /** + * 对象存储服务的URL + */ + private String endpoint; + + /** + * Access key 账户ID + */ + private String accessKey; + + /** + * Secret key 密码 + */ + private String secretKey; + + + @Override + public void afterPropertiesSet() { + Assert.hasText(endpoint, "Minio endpoint 为空"); + Assert.hasText(accessKey, "Minio accessKey为空"); + Assert.hasText(secretKey, "Minio secretKey为空"); + } + +} diff --git a/ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/core/OssTemplate.java b/ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/core/OssTemplate.java new file mode 100644 index 0000000..6666497 --- /dev/null +++ b/ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/core/OssTemplate.java @@ -0,0 +1,99 @@ +package com.ds.common.oss.core; + +import com.ds.common.oss.pojo.OssFile; +import io.minio.messages.Bucket; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.validation.constraints.NotEmpty; +import org.springframework.web.multipart.MultipartFile; + +import java.io.InputStream; +import java.util.List; + +/** + * @author ds + * @since 2023/11/27 + */ +public interface OssTemplate { + + /** + * 存储桶是否存在 + * + * @param bucketName 存储桶名称 + * @return boolean + */ + boolean bucketExists(@NotEmpty(message = "桶名称不能为空") String bucketName); + + /** + * 创建桶 + * @param bucketName 存储桶名称 + * @return boolean + */ + boolean makeBucket(String bucketName); + + /** + * 删除桶 + * @param bucketName 存储桶名称 + * @return boolean + */ + boolean removeBucket(String bucketName); + + /** + * 获取全部桶 + * @return 全部桶列表 + */ + List getAllBuckets(); + + String preview(String fileName); + + /** + * 获取文件信息 + * + * @param fileName 存储桶文件名称 + * @return InputStream + */ + OssFile getOssInfo(@NotEmpty(message = "桶名称不能为空") String bucketName,@NotEmpty(message = "文件名称不能为空") String fileName); + + /** + * 上传文件 + * + * @param folderName 上传的文件夹名称 + * @param fileName 上传文件名 + * @param file 上传文件类 + * @return BladeFile + */ + OssFile uploadFile(String folderName, String fileName, MultipartFile file); + + /** + * 上传文件 + * + * @param folderName 上传的文件夹名称 + * @param fileName 存储桶对象名称 + * @param suffix 文件后缀名 + * @param stream 文件流 + * @return BladeFile + */ + OssFile uploadFile(String folderName, String fileName, String suffix, InputStream stream); + + /** + * 删除文件 + * + * @param fileName 存储桶对象名称 + */ + boolean removeFile(String fileName); + + /** + * 批量删除文件 + * + * @param fileNames 存储桶对象名称集合 + */ + boolean removeFiles(List fileNames); + + /** + * 下载文件 + * @param response res + * @param fileName 文件名称 + * @param filePath 文件路径 + */ + void downloadFile(HttpServletResponse response, String fileName, String filePath); + +} diff --git a/ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/pojo/OssFile.java b/ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/pojo/OssFile.java new file mode 100644 index 0000000..f536047 --- /dev/null +++ b/ds-commons/ds-config/ds-oss-config/src/main/java/com/ds/common/oss/pojo/OssFile.java @@ -0,0 +1,46 @@ +package com.ds.common.oss.pojo; + +import lombok.Data; + +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * @author ds + * @since 2023/11/27 + */ +@Data +public class OssFile implements Serializable { + /** + * 文件地址 + */ + private String filePath; + /** + * 域名地址 + */ + private String domain; + /** + * 文件名 + */ + private String name; + /** + * 原始文件名 + */ + private String originalName; + /** + * 文件hash值 + */ + public String hash; + /** + * 文件大小 + */ + private long size; + /** + * 文件上传时间 + */ + private LocalDateTime putTime; + /** + * 文件contentType + */ + private String contentType; +} diff --git a/ds-commons/ds-config/pom.xml b/ds-commons/ds-config/pom.xml index 0cb20b1..760ba98 100644 --- a/ds-commons/ds-config/pom.xml +++ b/ds-commons/ds-config/pom.xml @@ -15,6 +15,7 @@ ds-web-config ds-mybatisplus-config ds-mybatisplus-support + ds-oss-config diff --git a/pom.xml b/pom.xml index 00a7caf..7d94300 100644 --- a/pom.xml +++ b/pom.xml @@ -91,6 +91,11 @@ knife4j-openapi3-spring-boot-starter ${knife4j.version} + + io.minio + minio + 8.5.4 +