初始化仓库

This commit is contained in:
2026-04-21 15:08:07 +08:00
parent 444d984122
commit b5119afb9f
195 changed files with 11034 additions and 19 deletions

View 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>

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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");
}
}

View File

@@ -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);
}
}