新增oss的starter同时支持云oss、minio与fastdfs

This commit is contained in:
zlt2000
2021-02-17 22:19:30 +08:00
parent b0768b858c
commit 5fe7257615
27 changed files with 621 additions and 316 deletions
@@ -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
@@ -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;
}
}
}
@@ -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;
}
}
}
@@ -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;
}
}
}
@@ -1,19 +0,0 @@
package com.central.file.properties;
import lombok.Getter;
import lombok.Setter;
/**
* @author zlt
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Setter
@Getter
public class FdfsProperties {
/**
* fastdfs的http访问地址
*/
private String webUrl;
}
@@ -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();
}
@@ -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;
}
@@ -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<FileInfo> {
PageResult<FileInfo> findList(Map<String, Object> params);
void delete(String id);
void out(String id, OutputStream os);
}
@@ -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<FileMapper, FileI
private static final String FILE_SPLIT = ".";
@Override
public FileInfo upload(MultipartFile file) throws Exception {
public FileInfo upload(MultipartFile file) {
FileInfo fileInfo = FileUtil.getFileInfo(file);
if (!fileInfo.getName().contains(FILE_SPLIT)) {
throw new IllegalArgumentException("缺少后缀名");
}
uploadFile(file, fileInfo);
ObjectInfo objectInfo = uploadFile(file);
fileInfo.setPath(objectInfo.getObjectPath());
fileInfo.setUrl(objectInfo.getObjectUrl());
// 设置文件来源
fileInfo.setSource(fileType());
// 将文件信息保存到数据库
@@ -52,9 +55,8 @@ public abstract class AbstractIFileService extends ServiceImpl<FileMapper, FileI
* 上传文件
*
* @param file
* @param fileInfo
*/
protected abstract void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception;
protected abstract ObjectInfo uploadFile(MultipartFile file);
/**
* 删除文件
@@ -65,17 +67,16 @@ public abstract class AbstractIFileService extends ServiceImpl<FileMapper, FileI
FileInfo fileInfo = baseMapper.selectById(id);
if (fileInfo != null) {
baseMapper.deleteById(fileInfo.getId());
this.deleteFile(fileInfo);
this.deleteFile(fileInfo.getPath());
}
}
/**
* 删除文件资源
*
* @param fileInfo
* @return
* @param objectPath 文件路径
*/
protected abstract boolean deleteFile(FileInfo fileInfo);
protected abstract void deleteFile(String objectPath);
@Override
public PageResult<FileInfo> findList(Map<String, Object> params) {
@@ -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
* <p>
* 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) {
}
}
@@ -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
* <p>
* 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;
}
}