【MINIO删除对象方法】

This commit is contained in:
weizhihong 2022-10-21 09:43:15 +08:00
parent 6b6fd85cc6
commit d94039c8bb
4 changed files with 41 additions and 8 deletions

View File

@ -33,6 +33,11 @@ public class MinioController {
return minioService.downloadObjectUrl(directory, fileName); return minioService.downloadObjectUrl(directory, fileName);
} }
@DeleteMapping("/removeObject")
public void removeObject(String directory, String fileName) {
minioService.removeMinioObject(directory, fileName);
}
@PostMapping("/file/save") @PostMapping("/file/save")
public void saveFile(@RequestAttribute AccountVO user, @RequestBody MinioFileInfo fileInfo) { public void saveFile(@RequestAttribute AccountVO user, @RequestBody MinioFileInfo fileInfo) {
minioService.saveFileInfo(user, fileInfo); minioService.saveFileInfo(user, fileInfo);

View File

@ -44,4 +44,9 @@ public interface MinioService {
* 检查文件是否存在 * 检查文件是否存在
*/ */
boolean checkFileIsExist(String directory, String fileName); boolean checkFileIsExist(String directory, String fileName);
/**
* 删除MINIO库中对象
*/
void removeMinioObject(String directory, String fileName);
} }

View File

@ -41,8 +41,7 @@ public class MinioServiceImpl implements MinioService {
@Override @Override
public String preSignedObjectUrl(String directory, String fileName, Method method) { public String preSignedObjectUrl(String directory, String fileName, Method method) {
MinioClientUtil minioClientUtil = createMinioClientUtil(directory); return createMinioClientUtil(directory).getPreSignedObjectUrl(fileName, method);
return minioClientUtil.getPreSignedObjectUrl(fileName, method);
} }
@Override @Override
@ -83,12 +82,16 @@ public class MinioServiceImpl implements MinioService {
@Override @Override
public String downloadObjectUrl(String directory, String fileName) { public String downloadObjectUrl(String directory, String fileName) {
MinioClientUtil minioClientUtil = createMinioClientUtil(directory); return createMinioClientUtil(directory).getDownLoadPath(fileName);
return minioClientUtil.getDownLoadPath(fileName);
} }
@Override @Override
public boolean checkFileIsExist(String directory, String fileName) { public boolean checkFileIsExist(String directory, String fileName) {
return createMinioClientUtil(directory).checkFileIsExist(fileName); return createMinioClientUtil(directory).checkFileIsExist(fileName);
} }
@Override
public void removeMinioObject(String directory, String fileName) {
createMinioClientUtil(directory).removeObject(fileName);
}
} }

View File

@ -3,10 +3,7 @@ package club.joylink.rtss.util;
import club.joylink.rtss.configuration.MinioClientConfig; import club.joylink.rtss.configuration.MinioClientConfig;
import club.joylink.rtss.simulation.cbtc.exception.SimulationException; import club.joylink.rtss.simulation.cbtc.exception.SimulationException;
import club.joylink.rtss.simulation.cbtc.exception.SimulationExceptionType; import club.joylink.rtss.simulation.cbtc.exception.SimulationExceptionType;
import io.minio.GetObjectArgs; import io.minio.*;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.PutObjectArgs;
import io.minio.StatObjectArgs;
import io.minio.http.Method; import io.minio.http.Method;
import lombok.Data; import lombok.Data;
import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.utils.IOUtils;
@ -254,6 +251,29 @@ public class MinioClientUtil {
} }
} }
/**
* 根据文件名称删除文件
*/
public void removeObject(String fileName) {
removeObjectByPath(getSavePath(fileName));
}
/**
* 根据路径删除
*/
public void removeObjectByPath(String path) {
try {
config.getMinioClient().removeObject(
RemoveObjectArgs.builder()
.bucket(config.getBucket())
.object(path)
.build()
);
} catch (Exception e) {
throw new SimulationException(SimulationExceptionType.System_Fault, e);
}
}
private String getSavePath(String fileName) { private String getSavePath(String fileName) {
return FileInfo.getStoragePath(directory, fileName); return FileInfo.getStoragePath(directory, fileName);
} }