初始化仓库
This commit is contained in:
21
xtools-boot-storage/pom.xml
Normal file
21
xtools-boot-storage/pom.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>org.xujun</groupId>
|
||||
<artifactId>xtools-boot</artifactId>
|
||||
<version>5.0.0</version>
|
||||
</parent>
|
||||
<packaging>pom</packaging>
|
||||
<artifactId>xtools-boot-storage</artifactId>
|
||||
|
||||
<!-- 子模块 -->
|
||||
<modules>
|
||||
<module>xtools-boot-storage-file</module>
|
||||
<module>xtools-boot-storage-s3</module>
|
||||
<module>xtools-boot-storage-base</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
30
xtools-boot-storage/xtools-boot-storage-base/pom.xml
Normal file
30
xtools-boot-storage/xtools-boot-storage-base/pom.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-storage</artifactId>
|
||||
<version>5.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xtools-boot-storage-base</artifactId>
|
||||
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<!-- xtools-boot begin -->
|
||||
<!-- xtools-boot-core -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-core</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools-boot end -->
|
||||
|
||||
<!-- spring-boot -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,28 @@
|
||||
package xtools.boot.storage.base;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
import xtools.boot.core.utils.ModuleLoadUtils;
|
||||
import xtools.boot.storage.base.selector.BootStorageBaseImportSelector;
|
||||
|
||||
/**
|
||||
* <p>Title : BootStorageBaseConfiguration</p>
|
||||
* <p>Description : BootStorageBaseConfiguration</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
@Import(BootStorageBaseImportSelector.class)
|
||||
public class BootStorageBaseConfiguration {
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public BootStorageBaseConfiguration() {
|
||||
ModuleLoadUtils.loadSuccess(BootStorageBaseConfiguration.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package xtools.boot.storage.base.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* <p>Title : StorageConfig</p>
|
||||
* <p>Description : StorageConfig</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/3/14 07:40
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "storage")
|
||||
public class StorageConfig {
|
||||
|
||||
/**
|
||||
* 存储类型[file|s3]
|
||||
*/
|
||||
private String type = "file";
|
||||
|
||||
/**
|
||||
* 文件类型
|
||||
*/
|
||||
private FileType file;
|
||||
|
||||
/**
|
||||
* S3类型
|
||||
*/
|
||||
private S3Type s3;
|
||||
|
||||
/**
|
||||
* 文件类型
|
||||
*/
|
||||
@Data
|
||||
public static class FileType {
|
||||
|
||||
/**
|
||||
* 文件存储路径
|
||||
*/
|
||||
private String path;
|
||||
}
|
||||
|
||||
/**
|
||||
* S3类型
|
||||
*/
|
||||
@Data
|
||||
public static class S3Type {
|
||||
|
||||
/**
|
||||
* 服务类型[rustfs]
|
||||
*/
|
||||
private String server = "default";
|
||||
|
||||
/**
|
||||
* 地址
|
||||
*/
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* 密钥ID
|
||||
*/
|
||||
private String accessKeyId;
|
||||
|
||||
/**
|
||||
* 密钥
|
||||
*/
|
||||
private String secretAccessKey;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package xtools.boot.storage.base.selector;
|
||||
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
/**
|
||||
* <p>Title : BootStorageBaseImportSelector</p>
|
||||
* <p>Description : BootStorageBaseImportSelector</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
public class BootStorageBaseImportSelector implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
/**
|
||||
* 根据给定的注释元数据,根据需要注册bean
|
||||
*
|
||||
* @param importingClassMetadata AnnotationMetadata
|
||||
* @param registry BeanDefinitionRegistry
|
||||
*/
|
||||
@Override
|
||||
public void registerBeanDefinitions(@NonNull AnnotationMetadata importingClassMetadata, @NonNull BeanDefinitionRegistry registry) {
|
||||
// 构建扫描对象
|
||||
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, true);
|
||||
// 扫描包下路径
|
||||
scanner.scan("xtools.boot.storage.base");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package xtools.boot.storage.base.service;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* <p>Title : StorageService</p>
|
||||
* <p>Description : StorageService</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/3/14 07:20
|
||||
*/
|
||||
public interface StorageService {
|
||||
|
||||
/**
|
||||
* 是否存在文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
* @return true or false
|
||||
*/
|
||||
boolean exists(
|
||||
@NotNull String bucket,
|
||||
@NotNull String fileName
|
||||
);
|
||||
|
||||
/**
|
||||
* 保存文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
* @param inputStream 输入流
|
||||
* @param contentLength 文件长度
|
||||
*/
|
||||
void save(
|
||||
@NotNull String bucket,
|
||||
@NotNull String fileName,
|
||||
@NotNull InputStream inputStream,
|
||||
long contentLength
|
||||
);
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
* @param outputStream 输出流
|
||||
*/
|
||||
void get(
|
||||
@NotNull String bucket,
|
||||
@NotNull String fileName,
|
||||
@NotNull OutputStream outputStream
|
||||
);
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
*/
|
||||
void del(
|
||||
@NotNull String bucket,
|
||||
@NotNull String fileName
|
||||
);
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
xtools.boot.storage.base.BootStorageBaseConfiguration
|
||||
@@ -0,0 +1,18 @@
|
||||
# 存储配置
|
||||
storage:
|
||||
# 存储类型[file|s3](并且需要引入对应的jar包)
|
||||
type: ${STORAGE_TYPE:file}
|
||||
# 文件类型
|
||||
file:
|
||||
# 文件存储路径
|
||||
path: ${STORAGE_FILE_PATH:/data/xtools/file}
|
||||
# S3类型
|
||||
s3:
|
||||
# 服务类型(可为空)[rustfs]
|
||||
server: ${STORAGE_S3_SERVER:rustfs}
|
||||
# 地址
|
||||
host: ${STORAGE_S3_HOST:http://127.0.0.1:9000}
|
||||
# 密钥ID
|
||||
accessKeyId: ${STORAGE_S3_ACCESS_KEY_ID:admin}
|
||||
# 密钥
|
||||
secretAccessKey: ${STORAGE_S3_SECRET_ACCESS_KEY:123456}
|
||||
41
xtools-boot-storage/xtools-boot-storage-file/pom.xml
Normal file
41
xtools-boot-storage/xtools-boot-storage-file/pom.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-storage</artifactId>
|
||||
<version>5.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xtools-boot-storage-file</artifactId>
|
||||
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<!-- xtools-boot begin -->
|
||||
<!-- xtools-boot-core -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-core</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools-boot-storage-base -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-storage-base</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools-boot end -->
|
||||
|
||||
<!-- spring-boot -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- jakarta -->
|
||||
<dependency>
|
||||
<groupId>jakarta.annotation</groupId>
|
||||
<artifactId>jakarta.annotation-api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,28 @@
|
||||
package xtools.boot.storage.file;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
import xtools.boot.core.utils.ModuleLoadUtils;
|
||||
import xtools.boot.storage.file.selector.BootStorageFileImportSelector;
|
||||
|
||||
/**
|
||||
* <p>Title : BootStorageFileConfiguration</p>
|
||||
* <p>Description : BootStorageFileConfiguration</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
@Import(BootStorageFileImportSelector.class)
|
||||
public class BootStorageFileConfiguration {
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public BootStorageFileConfiguration() {
|
||||
ModuleLoadUtils.loadSuccess(BootStorageFileConfiguration.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package xtools.boot.storage.file.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import xtools.boot.storage.base.service.StorageService;
|
||||
import xtools.boot.storage.file.service.impl.StorageServiceFileImpl;
|
||||
|
||||
/**
|
||||
* <p>Title : FileStorageConfig</p>
|
||||
* <p>Description : FileStorageConfig</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/3/14 07:53
|
||||
*/
|
||||
@Configuration
|
||||
public class FileStorageConfig {
|
||||
|
||||
/**
|
||||
* 创建文件存储服务
|
||||
*
|
||||
* @return StorageService
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "storage", name = "type", havingValue = "file")
|
||||
public StorageService fileStorage() {
|
||||
return new StorageServiceFileImpl();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package xtools.boot.storage.file.selector;
|
||||
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
/**
|
||||
* <p>Title : BootStorageFileImportSelector</p>
|
||||
* <p>Description : BootStorageFileImportSelector</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
public class BootStorageFileImportSelector implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
/**
|
||||
* 根据给定的注释元数据,根据需要注册bean
|
||||
*
|
||||
* @param importingClassMetadata AnnotationMetadata
|
||||
* @param registry BeanDefinitionRegistry
|
||||
*/
|
||||
@Override
|
||||
public void registerBeanDefinitions(@NonNull AnnotationMetadata importingClassMetadata, @NonNull BeanDefinitionRegistry registry) {
|
||||
// 构建扫描对象
|
||||
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, true);
|
||||
// 扫描包下路径
|
||||
scanner.scan("xtools.boot.storage.file");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package xtools.boot.storage.file.service.impl;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import xtools.base.config.BaseParams;
|
||||
import xtools.boot.api.exection.BizError;
|
||||
import xtools.boot.storage.base.config.StorageConfig;
|
||||
import xtools.boot.storage.base.service.StorageService;
|
||||
import xtools.core.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* <p>Title : StorageServiceS3Impl</p>
|
||||
* <p>Description : StorageServiceS3Impl</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/3/14 07:35
|
||||
*/
|
||||
@Slf4j
|
||||
public class StorageServiceFileImpl implements StorageService, BaseParams {
|
||||
|
||||
@Resource
|
||||
private StorageConfig storageConfig;
|
||||
|
||||
/**
|
||||
* 是否存在文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
* @return true or false
|
||||
*/
|
||||
@Override
|
||||
public boolean exists(
|
||||
@NotNull String bucket,
|
||||
@NotNull String fileName
|
||||
) {
|
||||
initBucket(bucket);
|
||||
File file = new File(getFilePath(bucket, fileName));
|
||||
return file.exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
* @param inputStream 输入流
|
||||
* @param contentLength 文件长度
|
||||
*/
|
||||
@Override
|
||||
public void save(
|
||||
@NotNull String bucket,
|
||||
@NotNull String fileName,
|
||||
@NotNull InputStream inputStream,
|
||||
long contentLength
|
||||
) {
|
||||
initBucket(bucket);
|
||||
File file = new File(getFilePath(bucket, fileName));
|
||||
try (OutputStream outputStream = new FileOutputStream(file)) {
|
||||
byte[] bytes = new byte[CP_NUM1024];
|
||||
int len;
|
||||
while ((len = inputStream.read(bytes)) != CP_NEGATIVE1) {
|
||||
outputStream.write(bytes, CP_NUM0, len);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("保存文件失败", e);
|
||||
throw new BizError("保存文件失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
* @param outputStream 输出流
|
||||
*/
|
||||
@Override
|
||||
public void get(
|
||||
@NotNull String bucket,
|
||||
@NotNull String fileName,
|
||||
@NotNull OutputStream outputStream
|
||||
) {
|
||||
initBucket(bucket);
|
||||
try (InputStream inputStream = new FileInputStream(getFilePath(bucket, fileName))) {
|
||||
byte[] bytes = new byte[CP_NUM1024];
|
||||
int len;
|
||||
while ((len = inputStream.read(bytes)) != CP_NEGATIVE1) {
|
||||
outputStream.write(bytes, CP_NUM0, len);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("获取文件失败", e);
|
||||
throw new BizError("获取文件失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
*/
|
||||
@Override
|
||||
public void del(
|
||||
@NotNull String bucket,
|
||||
@NotNull String fileName
|
||||
) {
|
||||
initBucket(bucket);
|
||||
FileUtils.del(getFilePath(bucket, fileName));
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化桶
|
||||
*
|
||||
* @param bucket 桶
|
||||
*/
|
||||
private void initBucket(String bucket) {
|
||||
String path = getPath(bucket);
|
||||
File file = new File(path);
|
||||
if (file.exists()) {
|
||||
return;
|
||||
}
|
||||
if (file.mkdirs()) {
|
||||
return;
|
||||
}
|
||||
throw new BizError("创建目录失败" + path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取路径
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @return 路径
|
||||
*/
|
||||
private String getPath(String bucket) {
|
||||
return storageConfig.getFile().getPath() + CP_SLASH + bucket;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件路径
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
* @return 文件路径
|
||||
*/
|
||||
private String getFilePath(String bucket, String fileName) {
|
||||
return getPath(bucket) + CP_SLASH + fileName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
xtools.boot.storage.file.BootStorageFileConfiguration
|
||||
55
xtools-boot-storage/xtools-boot-storage-s3/pom.xml
Normal file
55
xtools-boot-storage/xtools-boot-storage-s3/pom.xml
Normal file
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-storage</artifactId>
|
||||
<version>5.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xtools-boot-storage-s3</artifactId>
|
||||
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<!-- xtools begin -->
|
||||
<!-- xtools-extend begin -->
|
||||
<!-- 存储工具s3客户端 -->
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>s3</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-extend</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools-extend end -->
|
||||
<!-- xtools end -->
|
||||
|
||||
<!-- xtools-boot begin -->
|
||||
<!-- xtools-boot-core -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-core</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools-boot-storage-base -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-storage-base</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools-boot end -->
|
||||
|
||||
<!-- spring-boot -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- jakarta -->
|
||||
<dependency>
|
||||
<groupId>jakarta.annotation</groupId>
|
||||
<artifactId>jakarta.annotation-api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,28 @@
|
||||
package xtools.boot.storage.s3;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
import xtools.boot.core.utils.ModuleLoadUtils;
|
||||
import xtools.boot.storage.s3.selector.BootStorageS3ImportSelector;
|
||||
|
||||
/**
|
||||
* <p>Title : BootStorageS3Configuration</p>
|
||||
* <p>Description : BootStorageS3Configuration</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
@Import(BootStorageS3ImportSelector.class)
|
||||
public class BootStorageS3Configuration {
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public BootStorageS3Configuration() {
|
||||
ModuleLoadUtils.loadSuccess(BootStorageS3Configuration.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package xtools.boot.storage.s3.config;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import xtools.boot.storage.base.config.StorageConfig;
|
||||
import xtools.boot.storage.base.service.StorageService;
|
||||
import xtools.boot.storage.s3.service.impl.StorageServiceS3Impl;
|
||||
import xtools.extend.S3Utils;
|
||||
|
||||
/**
|
||||
* <p>Title : S3StorageConfig</p>
|
||||
* <p>Description : S3StorageConfig</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/3/14 07:53
|
||||
*/
|
||||
@Configuration
|
||||
public class S3StorageConfig {
|
||||
|
||||
@Resource
|
||||
private StorageConfig storageConfig;
|
||||
|
||||
/**
|
||||
* 创建S3默认客户端
|
||||
*
|
||||
* @return S3Client
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "storage", name = "type", havingValue = "s3")
|
||||
@ConditionalOnProperty(prefix = "storage.s3", name = "server", havingValue = "default")
|
||||
public S3Client defaultClient() {
|
||||
return S3Utils.client(
|
||||
storageConfig.getS3().getHost(),
|
||||
storageConfig.getS3().getAccessKeyId(),
|
||||
storageConfig.getS3().getSecretAccessKey(),
|
||||
Region.CN_NORTH_1,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建S3-rustfs客户端
|
||||
*
|
||||
* @return S3Client
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "storage", name = "type", havingValue = "s3")
|
||||
@ConditionalOnProperty(prefix = "storage.s3", name = "server", havingValue = "rustfs")
|
||||
public S3Client rustfsClient() {
|
||||
return S3Utils.rustfsClient(
|
||||
storageConfig.getS3().getHost(),
|
||||
storageConfig.getS3().getAccessKeyId(),
|
||||
storageConfig.getS3().getSecretAccessKey()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建S3存储服务
|
||||
*
|
||||
* @return StorageService
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "storage", name = "type", havingValue = "s3")
|
||||
public StorageService s3Storage() {
|
||||
return new StorageServiceS3Impl();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package xtools.boot.storage.s3.selector;
|
||||
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
/**
|
||||
* <p>Title : BootStorageS3ImportSelector</p>
|
||||
* <p>Description : BootStorageS3ImportSelector</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
public class BootStorageS3ImportSelector implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
/**
|
||||
* 根据给定的注释元数据,根据需要注册bean
|
||||
*
|
||||
* @param importingClassMetadata AnnotationMetadata
|
||||
* @param registry BeanDefinitionRegistry
|
||||
*/
|
||||
@Override
|
||||
public void registerBeanDefinitions(@NonNull AnnotationMetadata importingClassMetadata, @NonNull BeanDefinitionRegistry registry) {
|
||||
// 构建扫描对象
|
||||
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, true);
|
||||
// 扫描包下路径
|
||||
scanner.scan("xtools.boot.storage.s3");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package xtools.boot.storage.s3.service.impl;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import xtools.base.config.BaseParams;
|
||||
import xtools.boot.storage.base.service.StorageService;
|
||||
import xtools.extend.S3Utils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* <p>Title : StorageServiceS3Impl</p>
|
||||
* <p>Description : StorageServiceS3Impl</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/3/14 07:35
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class StorageServiceS3Impl implements StorageService, BaseParams {
|
||||
|
||||
/**
|
||||
* 桶集合
|
||||
*/
|
||||
private final static Set<String> BUCKET_SET = new HashSet<>();
|
||||
|
||||
@Resource
|
||||
private S3Client s3Client;
|
||||
|
||||
/**
|
||||
* 是否存在文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
* @return true or false
|
||||
*/
|
||||
@Override
|
||||
public boolean exists(
|
||||
@NotNull String bucket,
|
||||
@NotNull String fileName
|
||||
) {
|
||||
initBucket(bucket);
|
||||
return S3Utils.exists(s3Client, bucket, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
* @param inputStream 输入流
|
||||
* @param contentLength 文件长度
|
||||
*/
|
||||
@Override
|
||||
public void save(
|
||||
@NotNull String bucket,
|
||||
@NotNull String fileName,
|
||||
@NotNull InputStream inputStream,
|
||||
long contentLength
|
||||
) {
|
||||
initBucket(bucket);
|
||||
S3Utils.upload(s3Client, bucket, fileName, inputStream, contentLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
* @param outputStream 输出流
|
||||
*/
|
||||
@Override
|
||||
public void get(
|
||||
@NotNull String bucket,
|
||||
@NotNull String fileName,
|
||||
@NotNull OutputStream outputStream
|
||||
) {
|
||||
S3Utils.download(s3Client, bucket, fileName, outputStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param fileName 文件名
|
||||
*/
|
||||
@Override
|
||||
public void del(
|
||||
@NotNull String bucket,
|
||||
@NotNull String fileName
|
||||
) {
|
||||
S3Utils.delete(s3Client, bucket, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化桶
|
||||
*
|
||||
* @param bucket 桶
|
||||
*/
|
||||
private void initBucket(String bucket) {
|
||||
if (BUCKET_SET.contains(bucket)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
S3Utils.createBucket(s3Client, bucket);
|
||||
} catch (Exception e) {
|
||||
log.info("文件桶已存在", e);
|
||||
}
|
||||
BUCKET_SET.add(bucket);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
xtools.boot.storage.s3.BootStorageS3Configuration
|
||||
Reference in New Issue
Block a user