新增oss的starter同时支持云oss、minio与fastdfs
This commit is contained in:
+20
@@ -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
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@EnableConfigurationProperties(FileServerProperties.class)
|
||||
@Import({FdfsTemplate.class, S3Template.class})
|
||||
public class OssAutoConfigure {
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.central.oss.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2021/2/11
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public class ObjectInfo {
|
||||
/**
|
||||
* 对象查看路径
|
||||
*/
|
||||
private String objectUrl;
|
||||
/**
|
||||
* 对象保存路径
|
||||
*/
|
||||
private String objectPath;
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.central.oss.properties;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* fastdfs配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2021/2/11
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public class FdfsProperties {
|
||||
/**
|
||||
* fastdfs的http访问地址
|
||||
*/
|
||||
private String webUrl;
|
||||
}
|
||||
+38
@@ -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
|
||||
* <p>
|
||||
* 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();
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.central.oss.properties;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* aws s3协议配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2021/2/11
|
||||
* <p>
|
||||
* 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;
|
||||
}
|
||||
+42
@@ -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
|
||||
* <p>
|
||||
* 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);
|
||||
}
|
||||
+83
@@ -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
|
||||
* <p>
|
||||
* 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> T download(String objectPath, DownloadCallback<T> callback) {
|
||||
if (!StringUtils.isEmpty(objectPath)) {
|
||||
StorePath storePath = StorePath.parseFromUrl(objectPath);
|
||||
return storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), callback);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+147
@@ -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
|
||||
* <p>
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user