feat:新增s3配置
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,10 +4,10 @@ server:
|
|||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
name: file-center
|
name: file-center
|
||||||
cloud:
|
# cloud:
|
||||||
nacos:
|
# nacos:
|
||||||
discovery:
|
# discovery:
|
||||||
ip: 119.45.169.13
|
# ip: 119.45.169.13
|
||||||
servlet:
|
servlet:
|
||||||
multipart:
|
multipart:
|
||||||
max-file-size: 20MB
|
max-file-size: 20MB
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ server:
|
|||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
name: order-center
|
name: order-center
|
||||||
cloud:
|
# cloud:
|
||||||
nacos:
|
# nacos:
|
||||||
discovery:
|
# discovery:
|
||||||
ip: 81.70.164.178
|
# ip: 81.70.164.178
|
||||||
# cloud:
|
# cloud:
|
||||||
# nacos:
|
# nacos:
|
||||||
# discovery:
|
# discovery:
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ server:
|
|||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
name: user-center
|
name: user-center
|
||||||
cloud:
|
# cloud:
|
||||||
nacos:
|
# nacos:
|
||||||
discovery:
|
# discovery:
|
||||||
ip: 124.222.128.40
|
# ip: 124.222.128.40
|
||||||
# cloud:
|
# cloud:
|
||||||
# nacos:
|
# nacos:
|
||||||
# discovery:
|
# discovery:
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ server:
|
|||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
name: uaa-server
|
name: uaa-server
|
||||||
cloud:
|
# cloud:
|
||||||
nacos:
|
# nacos:
|
||||||
discovery:
|
# discovery:
|
||||||
ip: 124.222.128.40
|
# ip: 124.222.128.40
|
||||||
|
|
||||||
encrypt:
|
encrypt:
|
||||||
key-store:
|
key-store:
|
||||||
|
|||||||
Reference in New Issue
Block a user