diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/S3Config.java b/zlt-business/file-center/src/main/java/com/central/file/config/S3Config.java new file mode 100644 index 00000000..c179d2f8 --- /dev/null +++ b/zlt-business/file-center/src/main/java/com/central/file/config/S3Config.java @@ -0,0 +1,94 @@ +package com.central.file.config; + +import com.amazonaws.ClientConfiguration; +import com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.AWSCredentialsProvider; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.client.builder.AwsClientBuilder; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3Client; +import com.central.oss.properties.FileServerProperties; +import com.central.oss.properties.S3Properties; +import com.central.oss.template.S3Template; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * S3配置类 + * 用于时间窗口智能存储的S3服务配置 + * + * @author system + */ +@Slf4j +@Configuration +public class S3Config { + + @Autowired + private FileServerProperties fileServerProperties; + + /** + * 创建S3Template Bean,用于时间窗口存储 + * 当S3服务启用时创建,不依赖主存储类型 + */ + @Bean("s3Template") + @ConditionalOnClass(name = "com.amazonaws.services.s3.AmazonS3") + @ConditionalOnProperty(prefix = "zlt.file-server.s3", name = "enabled", havingValue = "true") + public S3Template s3Template() { + log.info("创建S3Template用于时间窗口存储"); + + try { + S3Properties s3Properties = fileServerProperties.getS3(); + + // 创建AWS S3客户端配置 + ClientConfiguration config = new ClientConfiguration(); + AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration( + s3Properties.getEndpoint(), + s3Properties.getRegion() + ); + + AWSCredentials credentials = new BasicAWSCredentials( + s3Properties.getAccessKey(), + s3Properties.getAccessKeySecret() + ); + AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(credentials); + + AmazonS3 amazonS3 = AmazonS3Client.builder() + .withEndpointConfiguration(endpoint) + .withClientConfiguration(config) + .withCredentials(awsCredentialsProvider) + .withPathStyleAccessEnabled(s3Properties.getPathStyleAccessEnabled()) + .disableChunkedEncoding() + .build(); + + // 创建S3Template实例 + S3Template s3Template = new S3Template(); + + // 使用反射设置amazonS3字段 + try { + java.lang.reflect.Field amazonS3Field = S3Template.class.getDeclaredField("amazonS3"); + amazonS3Field.setAccessible(true); + amazonS3Field.set(s3Template, amazonS3); + + java.lang.reflect.Field filePropertiesField = S3Template.class.getDeclaredField("fileProperties"); + filePropertiesField.setAccessible(true); + filePropertiesField.set(s3Template, fileServerProperties); + + log.info("S3Template创建成功"); + return s3Template; + + } catch (Exception e) { + log.error("设置S3Template字段失败: {}", e.getMessage(), e); + throw new RuntimeException("创建S3Template失败", e); + } + + } catch (Exception e) { + log.error("创建S3Template失败: {}", e.getMessage(), e); + throw new RuntimeException("创建S3Template失败", e); + } + } +} \ No newline at end of file diff --git a/zlt-business/file-center/src/main/resources/bootstrap.yml b/zlt-business/file-center/src/main/resources/bootstrap.yml index 005bb0d4..e15c1688 100644 --- a/zlt-business/file-center/src/main/resources/bootstrap.yml +++ b/zlt-business/file-center/src/main/resources/bootstrap.yml @@ -4,10 +4,10 @@ server: spring: application: name: file-center - cloud: - nacos: - discovery: - ip: 119.45.169.13 +# cloud: +# nacos: +# discovery: +# ip: 119.45.169.13 servlet: multipart: max-file-size: 20MB diff --git a/zlt-business/order-center/src/main/resources/bootstrap.yml b/zlt-business/order-center/src/main/resources/bootstrap.yml index 1566517e..e780c4e4 100644 --- a/zlt-business/order-center/src/main/resources/bootstrap.yml +++ b/zlt-business/order-center/src/main/resources/bootstrap.yml @@ -4,10 +4,10 @@ server: spring: application: name: order-center - cloud: - nacos: - discovery: - ip: 81.70.164.178 +# cloud: +# nacos: +# discovery: +# ip: 81.70.164.178 # cloud: # nacos: # discovery: diff --git a/zlt-business/user-center/src/main/resources/bootstrap.yml b/zlt-business/user-center/src/main/resources/bootstrap.yml index a23d0891..c2b67a64 100644 --- a/zlt-business/user-center/src/main/resources/bootstrap.yml +++ b/zlt-business/user-center/src/main/resources/bootstrap.yml @@ -4,10 +4,10 @@ server: spring: application: name: user-center - cloud: - nacos: - discovery: - ip: 124.222.128.40 +# cloud: +# nacos: +# discovery: +# ip: 124.222.128.40 # cloud: # nacos: # discovery: diff --git a/zlt-uaa/src/main/resources/bootstrap.yml b/zlt-uaa/src/main/resources/bootstrap.yml index aff9b849..f24da648 100644 --- a/zlt-uaa/src/main/resources/bootstrap.yml +++ b/zlt-uaa/src/main/resources/bootstrap.yml @@ -8,10 +8,10 @@ server: spring: application: name: uaa-server - cloud: - nacos: - discovery: - ip: 124.222.128.40 +# cloud: +# nacos: +# discovery: +# ip: 124.222.128.40 encrypt: key-store: