com.admin4j.oss.impl.SimpleOSSUploadFileService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oss-spring-boot-starter Show documentation
Show all versions of oss-spring-boot-starter Show documentation
封装基于Amazon S3的OSS对象存储服务。在SpringBoot 中通过封装,简单地方式将文件存储到
MinIO、阿里云OSS、华为云OBS、七牛云Kodo、腾讯云COS等支持
S3 协议的平台
package com.admin4j.oss.impl;
import com.admin4j.oss.OssProperties;
import com.admin4j.oss.OssTemplate;
import com.admin4j.oss.UploadFileService;
import com.admin4j.oss.entity.vo.UploadFileVO;
import com.amazonaws.util.Md5Utils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;
/**
* @author andanyang
* @since 2023/4/14 9:27
*/
@RequiredArgsConstructor
public class SimpleOSSUploadFileService implements UploadFileService {
protected final static DateTimeFormatter FILEPATH_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
protected final OssTemplate ossTemplate;
protected final OssProperties ossProperties;
/**
* 上传文件
*
* @param file 文件
* @param path 路径,可以是文件类型
*/
@Override
public UploadFileVO upload(String path, MultipartFile file) throws IOException {
UploadFileVO uploadFileVO = new UploadFileVO();
uploadFileVO.setOriginalFilename(file.getOriginalFilename());
uploadFileVO.setSize(file.getSize());
uploadFileVO.setContentType(file.getContentType());
uploadFileVO.setCreateTime(LocalDateTime.now());
uploadFileVO.setBucket(defaultBucketName());
//计算文件md5
String md5 = Md5Utils.md5AsBase64(file.getBytes());
uploadFileVO.setMd5(md5);
//生成文件存储路径
if (StringUtils.isNotBlank(path)) {
uploadFileVO.setPrefix(path);
}
path = generateFilePath(uploadFileVO);
uploadFileVO.setKey(path);
uploadFileVO.setPreviewUrl(getPreviewUrl(path));
UploadFileVO beforeUploadFileVO = beforeUpload(uploadFileVO);
if (beforeUploadFileVO != null) {
return beforeUploadFileVO;
}
ossTemplate.putObject(defaultBucketName(), path, file.getInputStream());
afterUpload(uploadFileVO);
return uploadFileVO;
}
/**
* 文件预览路径
*
* @param key oss key
* @return 文件阅览路径
*/
@Override
public String getPreviewUrl(String key) {
if (StringUtils.isNotBlank(ossProperties.getPreviewUrl())) {
return ossProperties.getPreviewUrl() + key;
}
return getOssPreviewUrl(key);
}
/**
* 通过OSS直接查看文件预览路径
*
* @param key oss key
* @return 文件阅览路径
*/
@Override
public String getOssPreviewUrl(String key) {
return ossTemplate.getObjectURL(defaultBucketName(), key, 3, TimeUnit.HOURS);
}
/**
* 文件内网阅览路径
*
* @param key oss key
* @return 文件阅览路径
*/
@Override
public String getPreviewIntranetUrl(String key) {
if (StringUtils.isNotBlank(ossProperties.getIntranetUrl())) {
return ossProperties.getIntranetUrl() + key;
}
return getPreviewUrl(key);
}
/**
* 删除文件
*
* @param key 文件可以
*/
@Override
public void delete(String key) {
ossTemplate.removeObject(defaultBucketName(), key);
}
/**
* 生成文件路径
*
* @param uploadFileVO
* @return 生成oss key
*/
protected String generateFilePath(UploadFileVO uploadFileVO) {
//后缀
String postfix = null;
if (StringUtils.isNotBlank(uploadFileVO.getOriginalFilename()) && StringUtils.contains(uploadFileVO.getOriginalFilename(), ".")) {
postfix = StringUtils.substringAfterLast(uploadFileVO.getOriginalFilename(), ".");
}
StringBuilder filePathBuilder = new StringBuilder();
//文件前缀
if (StringUtils.isNotBlank(uploadFileVO.getPrefix())) {
filePathBuilder.append(uploadFileVO.getPrefix()).append('/');
}
filePathBuilder.append(uploadFileVO.getCreateTime().format(FILEPATH_DATETIME_FORMATTER))
.append('/').append(uploadFileVO.getMd5());
if (postfix != null) {
filePathBuilder.append(".").append(postfix);
}
return filePathBuilder.toString();
}
/**
* @return 默认的桶名称
*/
protected String defaultBucketName() {
return ossProperties.getBucket();
}
/**
* 上传前钩子
*
* @param uploadFileVO 上传文件信息
* @return true 可以上传 false 不用上传(比如根据md5/path 检查文件已存在)
*/
protected UploadFileVO beforeUpload(UploadFileVO uploadFileVO) {
return null;
}
/**
* 上传完成钩子,可以保存上传记录等
*
* @param uploadFileVO
*/
protected void afterUpload(UploadFileVO uploadFileVO) {
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy