dashan
12 months ago
9 changed files with 291 additions and 0 deletions
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
<parent> |
||||
<groupId>com.ds</groupId> |
||||
<artifactId>ds-config</artifactId> |
||||
<version>0.0.1-SNAPSHOT</version> |
||||
</parent> |
||||
|
||||
<groupId>com.ds.common.oss</groupId> |
||||
<artifactId>ds-oss-config</artifactId> |
||||
|
||||
<properties> |
||||
<maven.compiler.source>17</maven.compiler.source> |
||||
<maven.compiler.target>17</maven.compiler.target> |
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||
</properties> |
||||
|
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.springframework</groupId> |
||||
<artifactId>spring-context</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>io.minio</groupId> |
||||
<artifactId>minio</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.projectlombok</groupId> |
||||
<artifactId>lombok</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-autoconfigure</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework</groupId> |
||||
<artifactId>spring-web</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.apache.tomcat.embed</groupId> |
||||
<artifactId>tomcat-embed-core</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>jakarta.validation</groupId> |
||||
<artifactId>jakarta.validation-api</artifactId> |
||||
</dependency> |
||||
</dependencies> |
||||
|
||||
</project> |
@ -0,0 +1,32 @@
@@ -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(); |
||||
} |
||||
} |
@ -0,0 +1,49 @@
@@ -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为空"); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,99 @@
@@ -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<Bucket> 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<String> fileNames); |
||||
|
||||
/** |
||||
* 下载文件 |
||||
* @param response res |
||||
* @param fileName 文件名称 |
||||
* @param filePath 文件路径 |
||||
*/ |
||||
void downloadFile(HttpServletResponse response, String fileName, String filePath); |
||||
|
||||
} |
@ -0,0 +1,46 @@
@@ -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; |
||||
} |
Loading…
Reference in new issue