minio文件上传至不同路径
This commit is contained in:
parent
f920c36edc
commit
fb118431ac
@ -13,10 +13,10 @@ import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 用户基本信息 实体类
|
||||
* 用户基本信息实体类
|
||||
*
|
||||
* @author: yanglei
|
||||
* @since: 2025-10-15 09:21
|
||||
* @author : yanglei
|
||||
* @since : 2025-10-15 09:21
|
||||
*/
|
||||
|
||||
@Data
|
||||
@ -42,40 +42,35 @@ public class EbikeUser implements Serializable {
|
||||
*/
|
||||
private String mobile;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 邮箱
|
||||
*/
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String nickname;
|
||||
|
||||
/**
|
||||
* 性别 1-男 2-女 0-未知
|
||||
*/
|
||||
private Integer gender;
|
||||
|
||||
/**
|
||||
* 生日
|
||||
*/
|
||||
private LocalDateTime birthdate;
|
||||
|
||||
/**
|
||||
* 头像
|
||||
*/
|
||||
private String avatar;
|
||||
|
||||
/**
|
||||
* 角色
|
||||
* 性别 1-男 2-女 0-未知
|
||||
*/
|
||||
private String role;
|
||||
private Integer gender;
|
||||
|
||||
/**
|
||||
* 国家
|
||||
*/
|
||||
private String country;
|
||||
|
||||
/**
|
||||
* 省
|
||||
*/
|
||||
private String province;
|
||||
|
||||
/**
|
||||
* 市
|
||||
*/
|
||||
private String city;
|
||||
|
||||
/**
|
||||
* 地址
|
||||
@ -92,11 +87,6 @@ public class EbikeUser implements Serializable {
|
||||
*/
|
||||
private Integer userStatus;
|
||||
|
||||
/**
|
||||
* 用户是否通过实名认证 1-是 2-否
|
||||
*/
|
||||
private Integer userRealNameVerified;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
|
||||
@ -0,0 +1,27 @@
|
||||
package com.cdzy.user.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 文件上传至minio不同路径(同一桶中)
|
||||
*
|
||||
* @author: yanglei
|
||||
* @since: 2025-11-10 10:13
|
||||
*/
|
||||
|
||||
@Getter
|
||||
public enum EbikeFileTypeEnum {
|
||||
|
||||
// 用户身份证
|
||||
USER_IDCARD("user_idCard/"),
|
||||
|
||||
// 文件故障上报
|
||||
FAULT_ATTACHMENT("fault_attachment/");
|
||||
|
||||
private final String pathPrefix;
|
||||
|
||||
EbikeFileTypeEnum(String pathPrefix) {
|
||||
this.pathPrefix = pathPrefix;
|
||||
}
|
||||
|
||||
}
|
||||
@ -87,11 +87,6 @@ public class EbikeUser implements Serializable {
|
||||
*/
|
||||
private Integer userStatus;
|
||||
|
||||
/**
|
||||
* 用户是否通过实名认证 1-是 2-否
|
||||
*/
|
||||
private Integer userRealNameVerified;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package com.cdzy.user.service.impl;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.cdzy.common.ex.EbikeException;
|
||||
import com.cdzy.user.enums.EbikeFileTypeEnum;
|
||||
import com.cdzy.user.mapper.EbikeFaultReportMapper;
|
||||
import com.cdzy.user.model.dto.EbikeAttachmentFileDto;
|
||||
import com.cdzy.user.model.dto.EbikeFaultReportDto;
|
||||
@ -85,17 +85,19 @@ public class EbikeFaultReportServiceImpl extends ServiceImpl<EbikeFaultReportMap
|
||||
|
||||
@Override
|
||||
public EbikeAttachmentFileVo uploadFile(MultipartFile multipartFile) {
|
||||
String fileType = null;
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
if (!StringUtils.hasText(fileName)) {
|
||||
throw new EbikeException("文件名不能为空");
|
||||
}
|
||||
try {
|
||||
InputStream inputStream = multipartFile.getInputStream();
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
if (StringUtils.hasText(fileName)) {
|
||||
fileType = fileName.substring(fileName.lastIndexOf("."));
|
||||
}
|
||||
minioUtil.uploadFile(inputStream, fileName);
|
||||
String fileUrl = minioUtil.getFileUrl(fileName);
|
||||
String fileType = fileName.substring(fileName.lastIndexOf("."));
|
||||
String saveFileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
|
||||
// 文件上传
|
||||
minioUtil.uploadFile(EbikeFileTypeEnum.FAULT_ATTACHMENT, inputStream, saveFileName);
|
||||
String fileUrl = minioUtil.getFileUrl(EbikeFileTypeEnum.FAULT_ATTACHMENT, saveFileName);
|
||||
return EbikeAttachmentFileVo.builder()
|
||||
.fileName(fileName)
|
||||
.fileName(saveFileName)
|
||||
.fileUrl(fileUrl)
|
||||
.fileSize(multipartFile.getSize())
|
||||
.fileType(fileType)
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.cdzy.user.utils;
|
||||
|
||||
import com.cdzy.user.enums.EbikeFileTypeEnum;
|
||||
import io.minio.*;
|
||||
import io.minio.http.Method;
|
||||
import io.minio.messages.Bucket;
|
||||
@ -31,45 +32,67 @@ public class MinioUtil {
|
||||
|
||||
/**
|
||||
* 创建桶
|
||||
*
|
||||
* @param bucket 数据桶名称
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public void createBucket(String bucket) throws Exception {
|
||||
//查看桶是否存在
|
||||
boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
|
||||
if (!exists){
|
||||
if (!exists) {
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param inputStream 文件流
|
||||
* @param bucket 数据桶名称
|
||||
* @param objectName 文件名称
|
||||
* @param bucket 数据桶名称
|
||||
* @param objectName 文件名称
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public void uploadFile(InputStream inputStream, String bucket, String objectName) throws Exception{
|
||||
public void uploadFile(InputStream inputStream, String bucket, String objectName) throws Exception {
|
||||
//上传文件
|
||||
minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
|
||||
.stream(inputStream,-1,Integer.MAX_VALUE).build());
|
||||
.stream(inputStream, -1, Integer.MAX_VALUE).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件至默认桶内
|
||||
*
|
||||
* @param inputStream 文件流
|
||||
* @param objectName 文件名称
|
||||
* @param objectName 文件名称
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public void uploadFile(InputStream inputStream, String objectName) throws Exception{
|
||||
public void uploadFile(InputStream inputStream, String objectName) throws Exception {
|
||||
//上传文件
|
||||
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName)
|
||||
.stream(inputStream,-1,Integer.MAX_VALUE).build());
|
||||
.stream(inputStream, -1, Integer.MAX_VALUE).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件至默认桶不同的路径下
|
||||
*
|
||||
* @param fileType 文件类型
|
||||
* @param inputStream 文件流
|
||||
* @param objectName 文件名称
|
||||
*/
|
||||
public void uploadFile(EbikeFileTypeEnum fileType, InputStream inputStream, String objectName) throws Exception {
|
||||
// 路径
|
||||
String fullObjectName = fileType.getPathPrefix() + objectName;
|
||||
//上传文件
|
||||
minioClient.putObject(PutObjectArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.object(fullObjectName)
|
||||
.stream(inputStream, -1, Integer.MAX_VALUE)
|
||||
.build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件分析连接(7天有效
|
||||
* @param bucket 数据桶名称
|
||||
*
|
||||
* @param bucket 数据桶名称
|
||||
* @param fileName 文件名称
|
||||
* @return url
|
||||
* @throws Exception 异常
|
||||
@ -88,6 +111,7 @@ public class MinioUtil {
|
||||
|
||||
/**
|
||||
* 获取默认桶内文件分析连接(7天有效
|
||||
*
|
||||
* @param fileName 文件名称
|
||||
* @return url
|
||||
* @throws Exception 异常
|
||||
@ -104,69 +128,96 @@ public class MinioUtil {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认桶内文件不同路径分析连接(7天有效
|
||||
*
|
||||
* @param fileName 文件名称
|
||||
* @return url
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public String getFileUrl(EbikeFileTypeEnum fileType, String fileName) throws Exception {
|
||||
// 路径
|
||||
String fullObjectName = fileType.getPathPrefix() + fileName;
|
||||
return minioClient.getPresignedObjectUrl(
|
||||
GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.GET)
|
||||
.bucket(bucketName)
|
||||
.object(fullObjectName)
|
||||
.expiry(7, TimeUnit.DAYS)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的桶
|
||||
*
|
||||
* @return 数据桶名称列表
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public List<String> getAllBucket() throws Exception{
|
||||
public List<String> getAllBucket() throws Exception {
|
||||
List<Bucket> buckets = minioClient.listBuckets();
|
||||
return buckets.stream().map(Bucket::name).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
* @param bucket 数据桶名称
|
||||
*
|
||||
* @param bucket 数据桶名称
|
||||
* @param objectName 文件名称
|
||||
* @return 文件流
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public InputStream downLoad(String bucket,String objectName) throws Exception{
|
||||
public InputStream downLoad(String bucket, String objectName) throws Exception {
|
||||
return minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载默认桶内文件
|
||||
*
|
||||
* @param objectName 文件名称
|
||||
* @return 文件流
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public InputStream downLoad(String objectName) throws Exception{
|
||||
public InputStream downLoad(String objectName) throws Exception {
|
||||
return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除桶
|
||||
*
|
||||
* @param bucket 数据桶名称
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public void deleteBucket(String bucket) throws Exception{
|
||||
public void deleteBucket(String bucket) throws Exception {
|
||||
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param bucket 数据桶名称
|
||||
*
|
||||
* @param bucket 数据桶名称
|
||||
* @param objectName 文件名称
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public void deleteObject(String bucket,String objectName) throws Exception{
|
||||
public void deleteObject(String bucket, String objectName) throws Exception {
|
||||
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectName).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除默认桶内文件
|
||||
*
|
||||
* @param objectName 文件名称
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public void deleteObject(String objectName) throws Exception{
|
||||
public void deleteObject(String objectName) throws Exception {
|
||||
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量上传文件(原子性操作)
|
||||
*
|
||||
* @param fileStreams 文件流列表
|
||||
* @param bucket 数据桶名称
|
||||
* @param bucket 数据桶名称
|
||||
* @param objectNames 文件名列表
|
||||
* @return 是否全部成功
|
||||
* @throws Exception 异常
|
||||
@ -209,7 +260,8 @@ public class MinioUtil {
|
||||
|
||||
/**
|
||||
* 回滚批量上传操作
|
||||
* @param bucket 数据桶名称
|
||||
*
|
||||
* @param bucket 数据桶名称
|
||||
* @param objectNames 需要删除的文件名列表
|
||||
*/
|
||||
private void rollbackBatchUpload(String bucket, List<String> objectNames) {
|
||||
@ -240,7 +292,8 @@ public class MinioUtil {
|
||||
|
||||
/**
|
||||
* 获取MinIO中指定文件的文件流
|
||||
* @param bucket 数据桶名称
|
||||
*
|
||||
* @param bucket 数据桶名称
|
||||
* @param objectName 文件名称
|
||||
* @return 文件流
|
||||
* @throws Exception 异常
|
||||
@ -256,6 +309,7 @@ public class MinioUtil {
|
||||
|
||||
/**
|
||||
* 获取默认桶中文件的文件流
|
||||
*
|
||||
* @param objectName 文件名称
|
||||
* @return 文件流
|
||||
* @throws Exception 异常
|
||||
|
||||
@ -123,7 +123,6 @@ CREATE TABLE "public"."ebike_user" (
|
||||
"address" varchar(200) COLLATE "pg_catalog"."default",
|
||||
"last_login_time" timestamp(6),
|
||||
"user_status" int2,
|
||||
"user_real_name_verified" int2,
|
||||
"create_by" int8,
|
||||
"create_time" timestamp(6) DEFAULT CURRENT_TIMESTAMP,
|
||||
"update_by" int8,
|
||||
@ -142,7 +141,6 @@ COMMENT ON COLUMN "public"."ebike_user"."city" IS '市';
|
||||
COMMENT ON COLUMN "public"."ebike_user"."address" IS '地址';
|
||||
COMMENT ON COLUMN "public"."ebike_user"."last_login_time" IS '记录用户最后一次登录的时间';
|
||||
COMMENT ON COLUMN "public"."ebike_user"."user_status" IS '用户的状态,-1-注销 1-激活 2-禁用';
|
||||
COMMENT ON COLUMN "public"."ebike_user"."user_real_name_verified" IS '用户是否通过实名认证 1-是 2-否';
|
||||
COMMENT ON COLUMN "public"."ebike_user"."create_by" IS '创建人ID';
|
||||
COMMENT ON COLUMN "public"."ebike_user"."create_time" IS '创建时间';
|
||||
COMMENT ON COLUMN "public"."ebike_user"."update_by" IS '最后修改人ID';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user