初始化仓库
This commit is contained in:
@@ -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