mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-09-24 07:19:46 +08:00
add 增加 OSS 对象存储模块 相关代码(未完成)
This commit is contained in:
@ -0,0 +1,69 @@
|
||||
package com.ruoyi.oss.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 云存储配置信息
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "cloud-storage")
|
||||
public class CloudStorageConfig implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 类型 1:七牛 2:阿里云 3:腾讯云 4: minio
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 七牛绑定的域名
|
||||
*/
|
||||
private String domain;
|
||||
|
||||
/**
|
||||
* 七牛路径前缀
|
||||
*/
|
||||
private String prefix;
|
||||
|
||||
/**
|
||||
* 七牛ACCESS_KEY
|
||||
*/
|
||||
private String accessKey;
|
||||
|
||||
/**
|
||||
* 七牛SECRET_KEY
|
||||
*/
|
||||
private String secretKey;
|
||||
|
||||
/**
|
||||
* 七牛存储空间名
|
||||
*/
|
||||
private String bucketName;
|
||||
|
||||
/**
|
||||
* 腾讯云AppId
|
||||
*/
|
||||
private Integer qcloudAppId;
|
||||
|
||||
/**
|
||||
* 腾讯云SecretId
|
||||
*/
|
||||
private String qcloudSecretId;
|
||||
|
||||
/**
|
||||
* 腾讯云SecretKey
|
||||
*/
|
||||
private String qcloudSecretKey;
|
||||
|
||||
/**
|
||||
* 腾讯云COS所属地区
|
||||
*/
|
||||
private String qcloudRegion;
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.ruoyi.oss.constant;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
|
||||
public class CloudConstant {
|
||||
|
||||
/**
|
||||
* 云存储配置KEY
|
||||
*/
|
||||
public final static String CLOUD_STORAGE_CONFIG_KEY = "sys.oss.cloud-storage";
|
||||
|
||||
/**
|
||||
* 云服务商
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum CloudService {
|
||||
|
||||
/**
|
||||
* 七牛云
|
||||
*/
|
||||
QINIU(1),
|
||||
|
||||
/**
|
||||
* 阿里云
|
||||
*/
|
||||
ALIYUN(2),
|
||||
|
||||
/**
|
||||
* 腾讯云
|
||||
*/
|
||||
QCLOUD(3),
|
||||
|
||||
/**
|
||||
* minio
|
||||
*/
|
||||
MINIO(4);
|
||||
|
||||
private final int value;
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.ruoyi.oss.exception;
|
||||
|
||||
/**
|
||||
* OSS异常类
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
public class OssException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public OssException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.ruoyi.oss.service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public interface ICloudStorageService {
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
*
|
||||
* @param prefix 前缀
|
||||
* @param suffix 后缀
|
||||
* @return 返回上传路径
|
||||
*/
|
||||
String getPath(String prefix, String suffix);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param data 文件字节数组
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
String upload(byte[] data, String path);
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
*
|
||||
* @param path 文件路径,包含文件名
|
||||
*/
|
||||
void delete(String path);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param data 文件字节数组
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
String uploadSuffix(byte[] data, String suffix);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param inputStream 字节流
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
String upload(InputStream inputStream, String path);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param inputStream 字节流
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
String uploadSuffix(InputStream inputStream, String suffix);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.ruoyi.oss.service.abstractd;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.service.ICloudStorageService;
|
||||
import com.ruoyi.oss.utils.DateUtils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 云存储(支持七牛、阿里云、腾讯云、minio)
|
||||
*/
|
||||
public abstract class AbstractCloudStorageService implements ICloudStorageService {
|
||||
|
||||
/**
|
||||
* 云存储配置信息
|
||||
*/
|
||||
protected CloudStorageConfig config;
|
||||
|
||||
public int getServiceType() {
|
||||
return config.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPath(String prefix, String suffix) {
|
||||
// 生成uuid
|
||||
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
// 文件路径
|
||||
String path = DateUtils.dateTime() + "/" + uuid;
|
||||
if (StrUtil.isNotBlank(prefix)) {
|
||||
path = prefix + "/" + path;
|
||||
}
|
||||
return path + suffix;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 阿里云存储
|
||||
*/
|
||||
public class AliyunCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
|
||||
private OSSClient client;
|
||||
|
||||
public AliyunCloudStorageServiceImpl(CloudStorageConfig config) {
|
||||
this.config = config;
|
||||
// 初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
client = new OSSClient(config.getDomain(), config.getAccessKey(), config.getSecretKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
return upload(new ByteArrayInputStream(data), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
client.putObject(config.getBucketName(), path, inputStream);
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检查配置信息");
|
||||
}
|
||||
return config.getDomain() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
path = path.replace(config.getDomain() + "/", "");
|
||||
try {
|
||||
client.deleteObject(config.getBucketName(), path);
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请检查配置信息");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
import io.minio.MinioClient;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* minio存储
|
||||
*/
|
||||
public class MinioCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
|
||||
private MinioClient minioClient;
|
||||
|
||||
public MinioCloudStorageServiceImpl(CloudStorageConfig config) {
|
||||
this.config = config;
|
||||
// 初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
minioClient = MinioClient.builder()
|
||||
.endpoint(config.getDomain())
|
||||
.credentials(config.getAccessKey(), config.getSecretKey())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
try {
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请核对Minio配置信息");
|
||||
}
|
||||
return config.getDomain() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
try {
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new OssException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 腾讯云存储
|
||||
*/
|
||||
public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
|
||||
private COSClient client;
|
||||
|
||||
public QcloudCloudStorageServiceImpl(CloudStorageConfig config) {
|
||||
this.config = config;
|
||||
// 初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
// Credentials credentials = new Credentials(config.getQcloudAppId(), config.getQcloudSecretId(),
|
||||
// config.getQcloudSecretKey());
|
||||
// 初始化客户端配置
|
||||
// ClientConfig clientConfig = new ClientConfig();
|
||||
// // 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
|
||||
// clientConfig.setRegion(config.getQcloudRegion());
|
||||
// client = new COSClient(clientConfig, credentials);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
// 腾讯云必需要以"/"开头
|
||||
if (!path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
// 上传到腾讯云
|
||||
// UploadFileRequest request = new UploadFileRequest(config.getQcloudBucketName(), path, data);
|
||||
// String response = client.uploadFile(request);
|
||||
// Map<String, Object> jsonObject = JsonUtils.parseMap(response);
|
||||
// if (Convert.toInt(jsonObject.get("code")) != 0) {
|
||||
// throw new OssException("文件上传失败," + Convert.toStr(jsonObject.get("message")));
|
||||
// }
|
||||
return config.getDomain() + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
// path = path.replace(config.getDomain(),"");
|
||||
// DelFileRequest request = new DelFileRequest(config.getBucketName(), path);
|
||||
// String response = client.delFile(request);
|
||||
// Map<String, Object> jsonObject = JsonUtils.parseMap(response);
|
||||
// if (Convert.toInt(jsonObject.get("code")) != 0) {
|
||||
// throw new OssException("文件删除失败," + Convert.toStr(jsonObject.get("message")));
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package com.ruoyi.oss.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.qiniu.http.Response;
|
||||
import com.qiniu.storage.BucketManager;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.Region;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.util.Auth;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 七牛云存储
|
||||
*/
|
||||
public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService {
|
||||
|
||||
private UploadManager uploadManager;
|
||||
private BucketManager bucketManager;
|
||||
private String token;
|
||||
|
||||
public QiniuCloudStorageServiceImpl(CloudStorageConfig config) {
|
||||
this.config = config;
|
||||
// 初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
// z0 z1 z2
|
||||
Configuration config = new Configuration(Region.autoRegion());
|
||||
// 默认不使用https
|
||||
config.useHttpsDomains = false;
|
||||
uploadManager = new UploadManager(config);
|
||||
Auth auth = Auth.create(this.config.getAccessKey(), this.config.getSecretKey());
|
||||
token = auth.uploadToken(this.config.getBucketName());
|
||||
bucketManager = new BucketManager(auth, config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
try {
|
||||
Response res = uploadManager.put(data, path, token);
|
||||
if (!res.isOK()) {
|
||||
throw new RuntimeException("上传七牛出错:" + res.toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OssException("上传文件失败,请核对七牛配置信息");
|
||||
}
|
||||
return config.getDomain() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
try {
|
||||
path = path.replace(config.getDomain() + "/", "");
|
||||
Response res = bucketManager.delete(config.getBucketName(), path);
|
||||
if (!res.isOK()) {
|
||||
throw new RuntimeException("删除七牛文件出错:" + res.toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OssException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
byte[] data = IoUtil.readBytes(inputStream);
|
||||
return this.upload(data, path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getPrefix(), suffix));
|
||||
}
|
||||
|
||||
}
|
135
ruoyi-oss/src/main/java/com/ruoyi/oss/utils/DateUtils.java
Normal file
135
ruoyi-oss/src/main/java/com/ruoyi/oss/utils/DateUtils.java
Normal file
@ -0,0 +1,135 @@
|
||||
package com.ruoyi.oss.utils;
|
||||
|
||||
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 时间工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
public static String YYYY = "yyyy";
|
||||
|
||||
public static String YYYY_MM = "yyyy-MM";
|
||||
|
||||
public static String YYYY_MM_DD = "yyyy-MM-dd";
|
||||
|
||||
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
||||
|
||||
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
private static String[] parsePatterns = {
|
||||
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
|
||||
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
|
||||
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
|
||||
|
||||
/**
|
||||
* 获取当前Date型日期
|
||||
*
|
||||
* @return Date() 当前日期
|
||||
*/
|
||||
public static Date getNowDate() {
|
||||
return new Date();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期, 默认格式为yyyy-MM-dd
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static String getDate() {
|
||||
return dateTimeNow(YYYY_MM_DD);
|
||||
}
|
||||
|
||||
public static final String getTime() {
|
||||
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
||||
}
|
||||
|
||||
public static final String dateTimeNow() {
|
||||
return dateTimeNow(YYYYMMDDHHMMSS);
|
||||
}
|
||||
|
||||
public static final String dateTimeNow(final String format) {
|
||||
return parseDateToStr(format, new Date());
|
||||
}
|
||||
|
||||
public static final String dateTime(final Date date) {
|
||||
return parseDateToStr(YYYY_MM_DD, date);
|
||||
}
|
||||
|
||||
public static final String parseDateToStr(final String format, final Date date) {
|
||||
return new SimpleDateFormat(format).format(date);
|
||||
}
|
||||
|
||||
public static final Date dateTime(final String format, final String ts) {
|
||||
try {
|
||||
return new SimpleDateFormat(format).parse(ts);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期路径 即年/月/日 如2018/08/08
|
||||
*/
|
||||
public static final String datePath() {
|
||||
Date now = new Date();
|
||||
return DateFormatUtils.format(now, "yyyy/MM/dd");
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期路径 即年/月/日 如20180808
|
||||
*/
|
||||
public static final String dateTime() {
|
||||
Date now = new Date();
|
||||
return DateFormatUtils.format(now, "yyyyMMdd");
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期型字符串转化为日期 格式
|
||||
*/
|
||||
public static Date parseDate(Object str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return parseDate(str.toString(), parsePatterns);
|
||||
} catch (ParseException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务器启动时间
|
||||
*/
|
||||
public static Date getServerStartDate() {
|
||||
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
|
||||
return new Date(time);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算两个时间差
|
||||
*/
|
||||
public static String getDatePoor(Date endDate, Date nowDate) {
|
||||
long nd = 1000 * 24 * 60 * 60;
|
||||
long nh = 1000 * 60 * 60;
|
||||
long nm = 1000 * 60;
|
||||
// long ns = 1000;
|
||||
// 获得两个时间的毫秒时间差异
|
||||
long diff = endDate.getTime() - nowDate.getTime();
|
||||
// 计算差多少天
|
||||
long day = diff / nd;
|
||||
// 计算差多少小时
|
||||
long hour = diff % nd / nh;
|
||||
// 计算差多少分钟
|
||||
long min = diff % nd % nh / nm;
|
||||
// 计算差多少秒//输出结果
|
||||
// long sec = diff % nd % nh % nm / ns;
|
||||
return day + "天" + hour + "小时" + min + "分钟";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user