将检测任务迁移python
This commit is contained in:
@@ -1,100 +1,97 @@
|
||||
package com.ruoyi.framework.service;
|
||||
|
||||
import com.ruoyi.framework.config.MinioProperties;
|
||||
import com.ruoyi.common.utils.file.FileUploadUtils;
|
||||
import io.minio.BucketExistsArgs;
|
||||
import io.minio.MakeBucketArgs;
|
||||
import io.minio.RemoveObjectArgs;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.io.InputStream;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
@Service
|
||||
public class MinioService {
|
||||
|
||||
private final MinioClient minioClient;
|
||||
private final MinioProperties properties;
|
||||
|
||||
@Autowired
|
||||
public MinioService(MinioProperties properties) {
|
||||
this.properties = properties;
|
||||
this.minioClient = MinioClient.builder()
|
||||
.endpoint(properties.getEndpoint())
|
||||
.credentials(properties.getAccessKey(), properties.getSecretKey())
|
||||
.build();
|
||||
}
|
||||
|
||||
public UploadResult upload(MultipartFile file) throws Exception {
|
||||
ensureBucket();
|
||||
String objectName = FileUploadUtils.extractFilename(file);
|
||||
try (InputStream is = file.getInputStream()) {
|
||||
minioClient.putObject(
|
||||
PutObjectArgs.builder()
|
||||
.bucket(properties.getBucket())
|
||||
.object(objectName)
|
||||
.stream(is, file.getSize(), -1)
|
||||
.contentType(file.getContentType())
|
||||
.build()
|
||||
);
|
||||
}
|
||||
String url = buildObjectUrl(objectName);
|
||||
return new UploadResult(objectName, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* MinIO 对象存储服务接口
|
||||
*/
|
||||
public interface MinioService {
|
||||
|
||||
/**
|
||||
* Upload using the provided unique objectName.
|
||||
* 上传对象
|
||||
*
|
||||
* @param bucketName 存储桶名称
|
||||
* @param objectName 对象名称
|
||||
* @param inputStream 输入流
|
||||
* @return 对象访问URL
|
||||
*/
|
||||
public UploadResult uploadWithName(MultipartFile file, String objectName) throws Exception {
|
||||
ensureBucket();
|
||||
try (InputStream is = file.getInputStream()) {
|
||||
minioClient.putObject(
|
||||
PutObjectArgs.builder()
|
||||
.bucket(properties.getBucket())
|
||||
.object(objectName)
|
||||
.stream(is, file.getSize(), -1)
|
||||
.contentType(file.getContentType())
|
||||
.build()
|
||||
);
|
||||
}
|
||||
String url = buildObjectUrl(objectName);
|
||||
return new UploadResult(objectName, url);
|
||||
}
|
||||
|
||||
public String buildObjectUrl(String objectName) {
|
||||
String endpoint = properties.getEndpoint();
|
||||
if (endpoint.endsWith("/")) {
|
||||
endpoint = endpoint.substring(0, endpoint.length() - 1);
|
||||
}
|
||||
return endpoint + "/" + properties.getBucket() + "/" + objectName;
|
||||
}
|
||||
|
||||
private void ensureBucket() throws Exception {
|
||||
boolean exists = minioClient.bucketExists(BucketExistsArgs.builder()
|
||||
.bucket(properties.getBucket())
|
||||
.build());
|
||||
if (!exists) {
|
||||
minioClient.makeBucket(MakeBucketArgs.builder()
|
||||
.bucket(properties.getBucket())
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
||||
String putObject(String bucketName, String objectName, InputStream inputStream);
|
||||
|
||||
/**
|
||||
* Delete an object from MinIO by its object name (key).
|
||||
* 获取对象
|
||||
*
|
||||
* @param bucketName 存储桶名称
|
||||
* @param objectName 对象名称
|
||||
* @return 对象输入流
|
||||
*/
|
||||
public void deleteObject(String objectName) throws Exception {
|
||||
minioClient.removeObject(
|
||||
RemoveObjectArgs.builder()
|
||||
.bucket(properties.getBucket())
|
||||
.object(objectName)
|
||||
.build()
|
||||
);
|
||||
InputStream getObject(String bucketName, String objectName);
|
||||
|
||||
/**
|
||||
* 删除对象
|
||||
*
|
||||
* @param bucketName 存储桶名称
|
||||
* @param objectName 对象名称
|
||||
*/
|
||||
void removeObject(String bucketName, String objectName);
|
||||
|
||||
/**
|
||||
* 删除对象(别名方法,同removeObject)
|
||||
*
|
||||
* @param objectName 对象名称
|
||||
*/
|
||||
default void deleteObject(String objectName) {
|
||||
removeObject("default", objectName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除对象(别名方法)
|
||||
*
|
||||
* @param bucketName 存储桶名称
|
||||
* @param objectName 对象名称
|
||||
*/
|
||||
default void deleteObject(String bucketName, String objectName) {
|
||||
removeObject(bucketName, objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查对象是否存在
|
||||
*
|
||||
* @param bucketName 存储桶名称
|
||||
* @param objectName 对象名称
|
||||
* @return 是否存在
|
||||
*/
|
||||
boolean objectExists(String bucketName, String objectName);
|
||||
|
||||
/**
|
||||
* 获取对象URL
|
||||
*
|
||||
* @param bucketName 存储桶名称
|
||||
* @param objectName 对象名称
|
||||
* @return 对象URL
|
||||
*/
|
||||
String getObjectUrl(String bucketName, String objectName);
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 上传结果
|
||||
*/
|
||||
UploadResult upload(MultipartFile file) throws Exception;
|
||||
|
||||
/**
|
||||
* 使用指定的对象名上传文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @param objectName 对象名
|
||||
* @return 上传结果
|
||||
*/
|
||||
UploadResult uploadWithName(MultipartFile file, String objectName) throws Exception;
|
||||
|
||||
/**
|
||||
* 上传结果类
|
||||
*/
|
||||
public static class UploadResult {
|
||||
private final String objectName;
|
||||
private final String url;
|
||||
|
||||
Reference in New Issue
Block a user