+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +@Service +@ConditionalOnProperty(prefix = com.central.oss.properties.FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_FDFS) +public class FastdfsService extends AbstractIFileService { + @Resource + private FdfsTemplate fdfsTemplate; + + @Override + protected String fileType() { + return FileServerProperties.TYPE_FDFS; + } + + @Override + protected ObjectInfo uploadFile(MultipartFile file) { + return fdfsTemplate.upload(file); + } + + @Override + protected void deleteFile(String objectPath) { + fdfsTemplate.delete(objectPath); + } + + @Override + public void out(String id, OutputStream os) { + } +} diff --git a/zlt-business/file-center/src/main/java/com/central/file/service/impl/S3Service.java b/zlt-business/file-center/src/main/java/com/central/file/service/impl/S3Service.java new file mode 100644 index 00000000..4067e98b --- /dev/null +++ b/zlt-business/file-center/src/main/java/com/central/file/service/impl/S3Service.java @@ -0,0 +1,75 @@ +package com.central.file.service.impl; + +import cn.hutool.core.util.StrUtil; +import com.central.file.model.FileInfo; +import com.central.oss.model.ObjectInfo; +import com.central.oss.properties.FileServerProperties; +import com.central.oss.template.S3Template; +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import java.io.OutputStream; + +/** + * @author zlt + * @date 2021/2/13 + *
+ * Blog: https://zlt2000.gitee.io
+ * Github: https://github.com/zlt2000
+ */
+@Service
+@ConditionalOnProperty(prefix = com.central.oss.properties.FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_S3)
+public class S3Service extends AbstractIFileService {
+ private static final String PATH_SPLIT = "/";
+
+ @Resource
+ private S3Template s3Template;
+
+ @Override
+ protected String fileType() {
+ return FileServerProperties.TYPE_S3;
+ }
+
+ @Override
+ protected ObjectInfo uploadFile(MultipartFile file) {
+ return s3Template.upload(file);
+ }
+
+ @Override
+ protected void deleteFile(String objectPath) {
+ S3Object s3Object = parsePath(objectPath);
+ s3Template.delete(s3Object.bucketName, s3Object.objectName);
+ }
+
+ @Override
+ public void out(String id, OutputStream os) {
+ FileInfo fileInfo = baseMapper.selectById(id);
+ if (fileInfo != null) {
+ S3Object s3Object = parsePath(fileInfo.getPath());
+ s3Template.out(s3Object.bucketName, s3Object.objectName, os);
+ }
+ }
+
+ @Setter
+ @Getter
+ private class S3Object {
+ private String bucketName;
+ private String objectName;
+ }
+
+ private S3Object parsePath(String path) {
+ S3Object s3Object = new S3Object();
+ if (StrUtil.isNotEmpty(path)) {
+ int splitIndex = path.lastIndexOf(PATH_SPLIT);
+ if (splitIndex != -1) {
+ s3Object.bucketName = path.substring(0, splitIndex);
+ s3Object.objectName = path.substring(splitIndex + 1);
+ }
+ }
+ return s3Object;
+ }
+}
diff --git a/zlt-business/file-center/src/main/resources/application.yml b/zlt-business/file-center/src/main/resources/application.yml
index e4c7e8c3..839effbe 100644
--- a/zlt-business/file-center/src/main/resources/application.yml
+++ b/zlt-business/file-center/src/main/resources/application.yml
@@ -15,16 +15,14 @@ mybatis-plus:
zlt:
file-server:
- type: fastdfs
- fdfs:
- web-url: ${zlt.fdfs.web-url}
- #oss配置
- #oss:
- # access-key: tpi8mObnfzZi4ggBX8Bw7zydjoTQ0WeML3RkPKsX
- # accessKeySecret: HZBXmSyUTy-haYp0KbBTtsil-GoKjVS2kDKT8Yow
- # endpoint: http://pkqtmn0p1.bkt.clouddn.com
- # bucketName: public-oss
- # domain:
+ type: s3
+ #fdfs:
+ # web-url: ${zlt.fdfs.web-url}
+ s3:
+ access-key: ${zlt.s3.access-key}
+ accessKeySecret: ${zlt.s3.accessKeySecret}
+ endpoint: ${zlt.s3.endpoint}
+ bucketName: ${zlt.s3.bucketName}
swagger:
enabled: true
title: 文件中心
@@ -36,7 +34,7 @@ zlt:
enable: true
#fastDFS配置
-fdfs:
- soTimeout: 1500
- connectTimeout: 600
- trackerList: ${zlt.fdfs.trackerList}
\ No newline at end of file
+#fdfs:
+# soTimeout: 1500
+# connectTimeout: 600
+# trackerList: ${zlt.fdfs.trackerList}
\ No newline at end of file
diff --git a/zlt-commons/pom.xml b/zlt-commons/pom.xml
index bd776989..4ca65a78 100644
--- a/zlt-commons/pom.xml
+++ b/zlt-commons/pom.xml
@@ -21,5 +21,6 @@
+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +@EnableConfigurationProperties(FileServerProperties.class) +@Import({FdfsTemplate.class, S3Template.class}) +public class OssAutoConfigure { + +} diff --git a/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/model/ObjectInfo.java b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/model/ObjectInfo.java new file mode 100644 index 00000000..15883a23 --- /dev/null +++ b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/model/ObjectInfo.java @@ -0,0 +1,24 @@ +package com.central.oss.model; + +import lombok.Getter; +import lombok.Setter; + +/** + * @author zlt + * @date 2021/2/11 + *
+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +@Setter +@Getter +public class ObjectInfo { + /** + * 对象查看路径 + */ + private String objectUrl; + /** + * 对象保存路径 + */ + private String objectPath; +} diff --git a/zlt-business/file-center/src/main/java/com/central/file/properties/FdfsProperties.java b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/FdfsProperties.java similarity index 78% rename from zlt-business/file-center/src/main/java/com/central/file/properties/FdfsProperties.java rename to zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/FdfsProperties.java index 4e00e943..94a27ce1 100644 --- a/zlt-business/file-center/src/main/java/com/central/file/properties/FdfsProperties.java +++ b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/FdfsProperties.java @@ -1,10 +1,13 @@ -package com.central.file.properties; +package com.central.oss.properties; import lombok.Getter; import lombok.Setter; /** + * fastdfs配置 + * * @author zlt + * @date 2021/2/11 *
* Blog: https://zlt2000.gitee.io * Github: https://github.com/zlt2000 diff --git a/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/FileServerProperties.java b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/FileServerProperties.java new file mode 100644 index 00000000..ac5a786d --- /dev/null +++ b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/FileServerProperties.java @@ -0,0 +1,38 @@ +package com.central.oss.properties; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * @author zlt + * @date 2021/2/11 + *
+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +@Setter +@Getter +@ConfigurationProperties(prefix = FileServerProperties.PREFIX) +public class FileServerProperties { + public static final String PREFIX = "zlt.file-server"; + public static final String TYPE_FDFS = "fastdfs"; + public static final String TYPE_S3 = "s3"; + + /** + * 为以下2个值,指定不同的自动化配置 + * s3:aws s3协议的存储(七牛oss、阿里云oss、minio等) + * fastdfs:本地部署的fastDFS + */ + private String type; + + /** + * aws s3配置 + */ + S3Properties s3 = new S3Properties(); + + /** + * fastDFS配置 + */ + FdfsProperties fdfs = new FdfsProperties(); +} diff --git a/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/S3Properties.java b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/S3Properties.java new file mode 100644 index 00000000..6cf0f25d --- /dev/null +++ b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/S3Properties.java @@ -0,0 +1,42 @@ +package com.central.oss.properties; + +import lombok.Getter; +import lombok.Setter; + +/** + * aws s3协议配置 + * + * @author zlt + * @date 2021/2/11 + *
+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +@Setter +@Getter +public class S3Properties { + /** + * 用户名 + */ + private String accessKey; + /** + * 密码 + */ + private String accessKeySecret; + /** + * 访问端点 + */ + private String endpoint; + /** + * bucket名称 + */ + private String bucketName; + /** + * 区域 + */ + private String region; + /** + * path-style + */ + private Boolean pathStyleAccessEnabled = true; +} diff --git a/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/service/IOssService.java b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/service/IOssService.java new file mode 100644 index 00000000..c4fe6a58 --- /dev/null +++ b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/service/IOssService.java @@ -0,0 +1,42 @@ +package com.central.oss.service; + +import com.central.oss.model.ObjectInfo; +import org.springframework.web.multipart.MultipartFile; + +import java.io.InputStream; +import java.io.OutputStream; + +/** + * @author zlt + * @date 2021/2/9 + *
+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +public interface IOssService { + /** + * 上传对象 + * @param objectName 对象名 + * @param is 对象流 + */ + ObjectInfo upload(String objectName, InputStream is); + + /** + * 上传对象 + * @param file 对象 + */ + ObjectInfo upload(MultipartFile file); + + /** + * 删除对象 + * @param objectKey 对象标识 + */ + void delete(String objectKey); + + /** + * 查看文件 + * @param objectPath 对象路径 + * @param os 输出流 + */ + void view(String objectPath, OutputStream os); +} diff --git a/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/template/FdfsTemplate.java b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/template/FdfsTemplate.java new file mode 100644 index 00000000..8390a563 --- /dev/null +++ b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/template/FdfsTemplate.java @@ -0,0 +1,83 @@ +package com.central.oss.template; + +import com.central.oss.model.ObjectInfo; +import com.central.oss.properties.FileServerProperties; +import com.github.tobato.fastdfs.domain.fdfs.StorePath; +import com.github.tobato.fastdfs.domain.proto.storage.DownloadCallback; +import com.github.tobato.fastdfs.service.FastFileStorageClient; +import lombok.SneakyThrows; +import org.apache.commons.io.FilenameUtils; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import java.io.InputStream; + +/** + * FastDFS配置 + * + * @author zlt + * @date 2021/2/11 + *
+ * Blog: https://zlt2000.gitee.io
+ * Github: https://github.com/zlt2000
+ */
+@ConditionalOnClass(FastFileStorageClient.class)
+@ConditionalOnProperty(prefix = FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_FDFS)
+public class FdfsTemplate {
+ @Resource
+ private FileServerProperties fileProperties;
+
+ @Resource
+ private FastFileStorageClient storageClient;
+
+ @SneakyThrows
+ public ObjectInfo upload(String objectName, InputStream is) {
+ return upload(objectName, is, is.available());
+ }
+
+ @SneakyThrows
+ public ObjectInfo upload(MultipartFile file) {
+ return upload(file.getOriginalFilename(), file.getInputStream(), file.getSize());
+ }
+
+ /**
+ * 上传对象
+ * @param objectName 对象名
+ * @param is 对象流
+ * @param size 大小
+ */
+ private ObjectInfo upload(String objectName, InputStream is, long size) {
+ StorePath storePath = storageClient.uploadFile(is, size, FilenameUtils.getExtension(objectName), null);
+ ObjectInfo obj = new ObjectInfo();
+ obj.setObjectPath(storePath.getFullPath());
+ obj.setObjectUrl("http://" + fileProperties.getFdfs().getWebUrl() + "/" + storePath.getFullPath());
+ return obj;
+ }
+
+ /**
+ * 删除对象
+ * @param objectPath 对象路径
+ */
+ public void delete(String objectPath) {
+ if (!StringUtils.isEmpty(objectPath)) {
+ StorePath storePath = StorePath.parseFromUrl(objectPath);
+ storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
+ }
+ }
+
+ /**
+ * 下载对象
+ * @param objectPath 对象路径
+ * @param callback 回调
+ */
+ public
+ * Blog: https://zlt2000.gitee.io
+ * Github: https://github.com/zlt2000
+ */
+@ConditionalOnClass(AmazonS3.class)
+@ConditionalOnProperty(prefix = FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_S3)
+public class S3Template implements InitializingBean {
+ private static final String DEF_CONTEXT_TYPE = "application/octet-stream";
+ private static final String PATH_SPLIT = "/";
+
+ @Autowired
+ private FileServerProperties fileProperties;
+
+ private AmazonS3 amazonS3;
+
+ @Override
+ public void afterPropertiesSet() {
+ ClientConfiguration config = new ClientConfiguration();
+ AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration(fileProperties.getS3().getEndpoint(), fileProperties.getS3().getRegion());
+ AWSCredentials credentials = new BasicAWSCredentials(fileProperties.getS3().getAccessKey(), fileProperties.getS3().getAccessKeySecret());
+ AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(credentials);
+ this.amazonS3 = AmazonS3Client.builder()
+ .withEndpointConfiguration(endpoint)
+ .withClientConfiguration(config)
+ .withCredentials(awsCredentialsProvider)
+ .withPathStyleAccessEnabled(fileProperties.getS3().getPathStyleAccessEnabled())
+ .disableChunkedEncoding()
+ .build();
+ }
+
+ @SneakyThrows
+ public ObjectInfo upload(String fileName, InputStream is) {
+ return upload(fileProperties.getS3().getBucketName(), fileName, is, is.available(), DEF_CONTEXT_TYPE);
+ }
+
+ @SneakyThrows
+ public ObjectInfo upload(MultipartFile file) {
+ return upload(fileProperties.getS3().getBucketName(), file.getOriginalFilename(), file.getInputStream()
+ , ((Long)file.getSize()).intValue(), file.getContentType());
+ }
+
+ @SneakyThrows
+ public ObjectInfo upload(String bucketName, String fileName, InputStream is) {
+ return upload(bucketName, fileName, is, is.available(), DEF_CONTEXT_TYPE);
+ }
+
+ /**
+ * 上传对象
+ * @param bucketName bucket名称
+ * @param objectName 对象名
+ * @param is 对象流
+ * @param size 大小
+ * @param contentType 类型
+ */
+ private ObjectInfo upload(String bucketName, String objectName, InputStream is, int size, String contentType) {
+ ObjectMetadata objectMetadata = new ObjectMetadata();
+ objectMetadata.setContentLength(size);
+ objectMetadata.setContentType(contentType);
+ PutObjectRequest putObjectRequest = new PutObjectRequest(
+ bucketName, objectName, is, objectMetadata);
+ putObjectRequest.getRequestClientOptions().setReadLimit(size + 1);
+ amazonS3.putObject(putObjectRequest);
+
+ ObjectInfo obj = new ObjectInfo();
+ obj.setObjectPath(bucketName + PATH_SPLIT + objectName);
+ obj.setObjectUrl(fileProperties.getS3().getEndpoint() + PATH_SPLIT + obj.getObjectPath());
+ return obj;
+ }
+
+ public void delete(String objectName) {
+ delete(fileProperties.getS3().getBucketName(), objectName);
+ }
+
+ public void delete(String bucketName, String objectName) {
+ amazonS3.deleteObject(bucketName, objectName);
+ }
+
+ /**
+ * 获取预览地址
+ * @param bucketName bucket名称
+ * @param objectName 对象名
+ * @param expires 有效时间(分钟),最大7天有效
+ * @return
+ */
+ public String getViewUrl(String bucketName, String objectName, int expires) {
+ Calendar cal = Calendar.getInstance();
+ cal.add(Calendar.MINUTE, expires);
+ URL url = amazonS3.generatePresignedUrl(bucketName, objectName, cal.getTime());
+ return url.toString();
+ }
+
+ public void out(String objectName, OutputStream os) {
+ out(fileProperties.getS3().getBucketName(), objectName, os);
+ }
+
+ /**
+ * 输出对象
+ * @param bucketName bucket名称
+ * @param objectName 对象名
+ * @param os 输出流
+ */
+ @SneakyThrows
+ public void out(String bucketName, String objectName, OutputStream os) {
+ S3Object s3Object = amazonS3.getObject(bucketName, objectName);
+ try (
+ S3ObjectInputStream s3is = s3Object.getObjectContent();
+ ) {
+ IOUtils.copy(s3is, os);
+ }
+ }
+}
diff --git a/zlt-commons/zlt-oss-spring-boot-starter/src/main/resources/META-INF/spring.factories b/zlt-commons/zlt-oss-spring-boot-starter/src/main/resources/META-INF/spring.factories
new file mode 100644
index 00000000..43d799a7
--- /dev/null
+++ b/zlt-commons/zlt-oss-spring-boot-starter/src/main/resources/META-INF/spring.factories
@@ -0,0 +1,2 @@
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+com.central.oss.config.OssAutoConfigure
\ No newline at end of file
diff --git a/zlt-config/src/main/resources/application-dev.properties b/zlt-config/src/main/resources/application-dev.properties
index a8c21924..091ab6e3 100644
--- a/zlt-config/src/main/resources/application-dev.properties
+++ b/zlt-config/src/main/resources/application-dev.properties
@@ -21,6 +21,12 @@ zlt.sentinel.dashboard=192.168.28.130:6999
zlt.fdfs.web-url=192.168.28.130
zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
+##### s3配置
+zlt.s3.access-key=minioadmin
+zlt.s3.accessKeySecret=minioadmin
+zlt.s3.endpoint=http://192.168.28.130:9000
+zlt.s3.bucketName=test
+
##### 日志链路追踪
zlt.trace.enable=true
diff --git a/zlt-config/src/main/resources/application-prod.properties b/zlt-config/src/main/resources/application-prod.properties
index 933e9b66..5fe2ca3d 100644
--- a/zlt-config/src/main/resources/application-prod.properties
+++ b/zlt-config/src/main/resources/application-prod.properties
@@ -21,5 +21,11 @@ zlt.sentinel.dashboard=192.168.28.130:6999
zlt.fdfs.web-url=192.168.28.130
zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
+##### s3配置
+zlt.s3.access-key=minioadmin
+zlt.s3.accessKeySecret=minioadmin
+zlt.s3.endpoint=http://192.168.28.130:9000
+zlt.s3.bucketName=test
+
##### 日志链路追踪
zlt.trace.enable=true
\ No newline at end of file
diff --git a/zlt-config/src/main/resources/application-test.properties b/zlt-config/src/main/resources/application-test.properties
index b46b08ee..ddf04395 100644
--- a/zlt-config/src/main/resources/application-test.properties
+++ b/zlt-config/src/main/resources/application-test.properties
@@ -21,5 +21,11 @@ zlt.sentinel.dashboard=192.168.28.130:6999
zlt.fdfs.web-url=192.168.28.130
zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
+##### s3配置
+zlt.s3.access-key=minioadmin
+zlt.s3.accessKeySecret=minioadmin
+zlt.s3.endpoint=http://192.168.28.130:9000
+zlt.s3.bucketName=test
+
##### 日志链路追踪
zlt.trace.enable=true
\ No newline at end of file