From 5fe725761549dacf614966b3037ddd2d8509d71d Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Wed, 17 Feb 2021 22:19:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eoss=E7=9A=84starter=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E6=94=AF=E6=8C=81=E4=BA=91oss=E3=80=81minio=E4=B8=8Ef?= =?UTF-8?q?astdfs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 + zlt-business/file-center/pom.xml | 24 +-- .../main/java/com/central/FileCenterApp.java | 3 - .../file/config/AliyunOSSAutoConfigure.java | 61 -------- .../file/config/FastdfsAutoConfigure.java | 53 ------- .../file/config/QiniuOSSAutoConfigure.java | 101 ------------ .../file/properties/FileServerProperties.java | 33 ---- .../file/properties/OssProperties.java | 32 ---- .../central/file/service/IFileService.java | 3 + .../service/impl/AbstractIFileService.java | 17 +- .../file/service/impl/FastdfsService.java | 44 ++++++ .../central/file/service/impl/S3Service.java | 75 +++++++++ .../src/main/resources/application.yml | 26 ++-- zlt-commons/pom.xml | 1 + .../zlt-oss-spring-boot-starter/pom.xml | 38 +++++ .../central/oss/config/OssAutoConfigure.java | 20 +++ .../com/central/oss/model/ObjectInfo.java | 24 +++ .../oss}/properties/FdfsProperties.java | 5 +- .../oss/properties/FileServerProperties.java | 38 +++++ .../central/oss/properties/S3Properties.java | 42 +++++ .../com/central/oss/service/IOssService.java | 42 +++++ .../central/oss/template/FdfsTemplate.java | 83 ++++++++++ .../com/central/oss/template/S3Template.java | 147 ++++++++++++++++++ .../main/resources/META-INF/spring.factories | 2 + .../main/resources/application-dev.properties | 6 + .../resources/application-prod.properties | 6 + .../resources/application-test.properties | 6 + 27 files changed, 621 insertions(+), 316 deletions(-) delete mode 100644 zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSAutoConfigure.java delete mode 100644 zlt-business/file-center/src/main/java/com/central/file/config/FastdfsAutoConfigure.java delete mode 100644 zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSSAutoConfigure.java delete mode 100644 zlt-business/file-center/src/main/java/com/central/file/properties/FileServerProperties.java delete mode 100644 zlt-business/file-center/src/main/java/com/central/file/properties/OssProperties.java create mode 100644 zlt-business/file-center/src/main/java/com/central/file/service/impl/FastdfsService.java create mode 100644 zlt-business/file-center/src/main/java/com/central/file/service/impl/S3Service.java create mode 100644 zlt-commons/zlt-oss-spring-boot-starter/pom.xml create mode 100644 zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/config/OssAutoConfigure.java create mode 100644 zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/model/ObjectInfo.java rename {zlt-business/file-center/src/main/java/com/central/file => zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss}/properties/FdfsProperties.java (78%) create mode 100644 zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/FileServerProperties.java create mode 100644 zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/S3Properties.java create mode 100644 zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/service/IOssService.java create mode 100644 zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/template/FdfsTemplate.java create mode 100644 zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/template/S3Template.java create mode 100644 zlt-commons/zlt-oss-spring-boot-starter/src/main/resources/META-INF/spring.factories diff --git a/pom.xml b/pom.xml index 8b1692ea..c4e7f1c6 100644 --- a/pom.xml +++ b/pom.xml @@ -350,6 +350,11 @@ dubbo ${dubbo.version} + + com.zlt + zlt-oss-spring-boot-starter + ${project.version} + org.springframework.boot diff --git a/zlt-business/file-center/pom.xml b/zlt-business/file-center/pom.xml index 2a270962..32269d70 100644 --- a/zlt-business/file-center/pom.xml +++ b/zlt-business/file-center/pom.xml @@ -30,6 +30,10 @@ com.zlt zlt-sentinel-spring-boot-starter + + com.zlt + zlt-oss-spring-boot-starter + com.alibaba.cloud @@ -66,21 +70,21 @@ micrometer-registry-prometheus - - - com.aliyun.oss - aliyun-sdk-oss - - - - com.qiniu - qiniu-java-sdk - com.github.tobato fastdfs-client + + + + + org.hibernate + hibernate-validator + diff --git a/zlt-business/file-center/src/main/java/com/central/FileCenterApp.java b/zlt-business/file-center/src/main/java/com/central/FileCenterApp.java index 487e94e5..1b83196d 100644 --- a/zlt-business/file-center/src/main/java/com/central/FileCenterApp.java +++ b/zlt-business/file-center/src/main/java/com/central/FileCenterApp.java @@ -1,10 +1,8 @@ package com.central; import com.central.common.ribbon.annotation.EnableFeignInterceptor; -import com.central.file.properties.FileServerProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @@ -13,7 +11,6 @@ import org.springframework.cloud.openfeign.EnableFeignClients; * @author 作者 owen E-mail: 624191343@qq.com */ @EnableDiscoveryClient -@EnableConfigurationProperties(FileServerProperties.class) @EnableFeignClients @EnableFeignInterceptor @SpringBootApplication diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSAutoConfigure.java b/zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSAutoConfigure.java deleted file mode 100644 index 9531e08a..00000000 --- a/zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSAutoConfigure.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.central.file.config; - -import com.aliyun.oss.common.auth.DefaultCredentialProvider; -import com.central.file.model.FileInfo; -import com.central.file.properties.FileServerProperties; -import com.central.file.service.impl.AbstractIFileService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import com.aliyun.oss.OSSClient; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - -/** - * 阿里云配置 - * - * @author 作者 owen E-mail: 624191343@qq.com - */ -@Configuration -@ConditionalOnProperty(name = "zlt.file-server.type", havingValue = "aliyun") -public class AliyunOSSAutoConfigure { - @Autowired - private FileServerProperties fileProperties; - - /** - * 阿里云文件存储client - * 只有配置了aliyun.oss.access-key才可以使用 - */ - @Bean - public OSSClient ossClient() { - OSSClient ossClient = new OSSClient(fileProperties.getOss().getEndpoint() - , new DefaultCredentialProvider(fileProperties.getOss().getAccessKey(), fileProperties.getOss().getAccessKeySecret()) - , null); - return ossClient; - } - - @Service - public class AliyunOssServiceImpl extends AbstractIFileService { - @Autowired - private OSSClient ossClient; - - @Override - protected String fileType() { - return fileProperties.getType(); - } - - @Override - protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception { - ossClient.putObject(fileProperties.getOss().getBucketName(), fileInfo.getName(), file.getInputStream()); - fileInfo.setUrl(fileProperties.getOss().getDomain() + "/" + fileInfo.getName()); - } - - @Override - protected boolean deleteFile(FileInfo fileInfo) { - ossClient.deleteObject(fileProperties.getOss().getBucketName(), fileInfo.getName()); - return true; - } - } -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/FastdfsAutoConfigure.java b/zlt-business/file-center/src/main/java/com/central/file/config/FastdfsAutoConfigure.java deleted file mode 100644 index e845d6f5..00000000 --- a/zlt-business/file-center/src/main/java/com/central/file/config/FastdfsAutoConfigure.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.central.file.config; - -import cn.hutool.core.util.StrUtil; -import com.central.file.model.FileInfo; -import com.central.file.properties.FileServerProperties; -import com.central.file.service.impl.AbstractIFileService; -import com.github.tobato.fastdfs.domain.fdfs.StorePath; -import com.github.tobato.fastdfs.service.FastFileStorageClient; -import org.apache.commons.io.FilenameUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Configuration; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - -/** - * FastDFS配置 - * - * @author zlt - */ -@Configuration -@ConditionalOnProperty(name = "zlt.file-server.type", havingValue = "fastdfs") -public class FastdfsAutoConfigure { - @Autowired - private FileServerProperties fileProperties; - - @Service - public class FastdfsServiceImpl extends AbstractIFileService { - @Autowired - private FastFileStorageClient storageClient; - - @Override - protected String fileType() { - return fileProperties.getType(); - } - - @Override - protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception { - StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null); - fileInfo.setUrl("http://" + fileProperties.getFdfs().getWebUrl() + "/" + storePath.getFullPath()); - fileInfo.setPath(storePath.getFullPath()); - } - - @Override - protected boolean deleteFile(FileInfo fileInfo) { - if (fileInfo != null && StrUtil.isNotEmpty(fileInfo.getPath())) { - StorePath storePath = StorePath.parseFromUrl(fileInfo.getPath()); - storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); - } - return true; - } - } -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSSAutoConfigure.java b/zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSSAutoConfigure.java deleted file mode 100644 index 849f928a..00000000 --- a/zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSSAutoConfigure.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.central.file.config; - -import com.central.file.model.FileInfo; -import com.central.file.properties.FileServerProperties; -import com.central.file.service.impl.AbstractIFileService; -import com.qiniu.common.QiniuException; -import com.qiniu.http.Response; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import com.qiniu.common.Zone; -import com.qiniu.storage.BucketManager; -import com.qiniu.storage.UploadManager; -import com.qiniu.util.Auth; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - -/** - * 七牛云配置 - * - * @author 作者 owen E-mail: 624191343@qq.com - */ -@Configuration -@ConditionalOnProperty(name = "zlt.file-server.type", havingValue = "qiniu") -public class QiniuOSSAutoConfigure { - @Autowired - private FileServerProperties fileProperties; - - /** - * 华东机房 - */ - @Bean - public com.qiniu.storage.Configuration qiniuConfig() { - return new com.qiniu.storage.Configuration(Zone.zone2()); - } - - /** - * 构建一个七牛上传工具实例 - */ - @Bean - public UploadManager uploadManager() { - return new UploadManager(qiniuConfig()); - } - - /** - * 认证信息实例 - * - * @return - */ - @Bean - public Auth auth() { - return Auth.create(fileProperties.getOss().getAccessKey(), fileProperties.getOss().getAccessKeySecret()); - } - - /** - * 构建七牛空间管理实例 - */ - @Bean - public BucketManager bucketManager() { - return new BucketManager(auth(), qiniuConfig()); - } - - @Service - public class QiniuOssServiceImpl extends AbstractIFileService { - @Autowired - private UploadManager uploadManager; - @Autowired - private BucketManager bucketManager; - @Autowired - private Auth auth; - - @Override - protected String fileType() { - return fileProperties.getType(); - } - - @Override - protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception { - // 调用put方法上传 - uploadManager.put(file.getBytes(), fileInfo.getName(), auth.uploadToken(fileProperties.getOss().getBucketName())); - fileInfo.setUrl(fileProperties.getOss().getEndpoint() + "/" + fileInfo.getName()); - fileInfo.setPath(fileProperties.getOss().getEndpoint() + "/" + fileInfo.getName()); - } - - @Override - protected boolean deleteFile(FileInfo fileInfo) { - try { - Response response = bucketManager.delete(fileProperties.getOss().getBucketName(), fileInfo.getPath()); - int retry = 0; - while (response.needRetry() && retry++ < 3) { - response = bucketManager.delete(fileProperties.getOss().getBucketName(), fileInfo.getPath()); - } - } catch (QiniuException e) { - return false; - } - return true; - } - } -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/properties/FileServerProperties.java b/zlt-business/file-center/src/main/java/com/central/file/properties/FileServerProperties.java deleted file mode 100644 index dca0c508..00000000 --- a/zlt-business/file-center/src/main/java/com/central/file/properties/FileServerProperties.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.central.file.properties; - -import lombok.Getter; -import lombok.Setter; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.cloud.context.config.annotation.RefreshScope; - -/** - * @author zlt - */ -@Setter -@Getter -@ConfigurationProperties(prefix = "zlt.file-server") -@RefreshScope -public class FileServerProperties { - /** - * 为以下3个值,指定不同的自动化配置 - * qiniu:七牛oss - * aliyun:阿里云oss - * fastdfs:本地部署的fastDFS - */ - private String type; - - /** - * oss配置 - */ - OssProperties oss = new OssProperties(); - - /** - * fastDFS配置 - */ - FdfsProperties fdfs = new FdfsProperties(); -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/properties/OssProperties.java b/zlt-business/file-center/src/main/java/com/central/file/properties/OssProperties.java deleted file mode 100644 index f3300b3b..00000000 --- a/zlt-business/file-center/src/main/java/com/central/file/properties/OssProperties.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.central.file.properties; - -import lombok.Getter; -import lombok.Setter; - -/** - * @author zlt - */ -@Setter -@Getter -public class OssProperties { - /** - * 密钥key - */ - private String accessKey; - /** - * 密钥密码 - */ - private String accessKeySecret; - /** - * 端点 - */ - private String endpoint; - /** - * bucket名称 - */ - private String bucketName; - /** - * 说明 - */ - private String domain; -} diff --git a/zlt-business/file-center/src/main/java/com/central/file/service/IFileService.java b/zlt-business/file-center/src/main/java/com/central/file/service/IFileService.java index 6f97a861..2e44595f 100644 --- a/zlt-business/file-center/src/main/java/com/central/file/service/IFileService.java +++ b/zlt-business/file-center/src/main/java/com/central/file/service/IFileService.java @@ -1,5 +1,6 @@ package com.central.file.service; +import java.io.OutputStream; import java.util.Map; import com.baomidou.mybatisplus.extension.service.IService; @@ -19,4 +20,6 @@ public interface IFileService extends IService { PageResult findList(Map params); void delete(String id); + + void out(String id, OutputStream os); } diff --git a/zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java b/zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java index ed1228be..93e38e1e 100644 --- a/zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java +++ b/zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java @@ -5,6 +5,7 @@ import java.util.Map; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.central.oss.model.ObjectInfo; import org.apache.commons.collections4.MapUtils; import org.springframework.web.multipart.MultipartFile; @@ -27,12 +28,14 @@ public abstract class AbstractIFileService extends ServiceImpl findList(Map params) { diff --git a/zlt-business/file-center/src/main/java/com/central/file/service/impl/FastdfsService.java b/zlt-business/file-center/src/main/java/com/central/file/service/impl/FastdfsService.java new file mode 100644 index 00000000..fe54278e --- /dev/null +++ b/zlt-business/file-center/src/main/java/com/central/file/service/impl/FastdfsService.java @@ -0,0 +1,44 @@ +package com.central.file.service.impl; + +import com.central.oss.model.ObjectInfo; +import com.central.oss.properties.FileServerProperties; +import com.central.oss.template.FdfsTemplate; +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_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 @@ zlt-sentinel-spring-boot-starter zlt-common-core zlt-elasticsearch-spring-boot-starter + zlt-oss-spring-boot-starter \ No newline at end of file diff --git a/zlt-commons/zlt-oss-spring-boot-starter/pom.xml b/zlt-commons/zlt-oss-spring-boot-starter/pom.xml new file mode 100644 index 00000000..16a2ed7a --- /dev/null +++ b/zlt-commons/zlt-oss-spring-boot-starter/pom.xml @@ -0,0 +1,38 @@ + + + + com.zlt + zlt-commons + 4.3.0 + + 4.0.0 + jar + zlt-oss-spring-boot-starter + 对象存储通用组件 + + + + com.github.tobato + fastdfs-client + true + + + + com.amazonaws + aws-java-sdk-s3 + true + + + org.springframework + spring-web + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + diff --git a/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/config/OssAutoConfigure.java b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/config/OssAutoConfigure.java new file mode 100644 index 00000000..725bc196 --- /dev/null +++ b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/config/OssAutoConfigure.java @@ -0,0 +1,20 @@ +package com.central.oss.config; + +import com.central.oss.properties.FileServerProperties; +import com.central.oss.template.FdfsTemplate; +import com.central.oss.template.S3Template; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Import; + +/** + * @author zlt + * @date 2021/2/13 + *

+ * 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 T download(String objectPath, DownloadCallback callback) { + if (!StringUtils.isEmpty(objectPath)) { + StorePath storePath = StorePath.parseFromUrl(objectPath); + return storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), callback); + } + return null; + } +} diff --git a/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/template/S3Template.java b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/template/S3Template.java new file mode 100644 index 00000000..eebf5433 --- /dev/null +++ b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/template/S3Template.java @@ -0,0 +1,147 @@ +package com.central.oss.template; + +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.amazonaws.services.s3.model.ObjectMetadata; +import com.amazonaws.services.s3.model.PutObjectRequest; +import com.amazonaws.services.s3.model.S3Object; +import com.amazonaws.services.s3.model.S3ObjectInputStream; +import com.amazonaws.util.IOUtils; +import com.central.oss.model.ObjectInfo; +import com.central.oss.properties.FileServerProperties; +import lombok.SneakyThrows; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; +import java.util.Calendar; + +/** + * aws s3配置 + * + * @author zlt + * @date 2021/2/11 + *

+ * 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