1、导入依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.1</version>
</dependency>
2、配置文件yml
minio:
bucket1:
endpoint: 192.168.10.85
port: 8855
accessKey: minioadmin
secretKey: minioadmin
bucket: bucket1
publicEndpoint: http://192.168.10.85:8855
3、写Minio配置类
@Configuration
public class MinoConfig {
@Value("${minio.bucket1.endpoint}")
private String endpoint;
@Value("${minio.bucket1.port}")
private int port;
@Value("${minio.bucket1.accessKey}")
private String accessKey;
@Value("${minio.bucket1.secretKey}")
private String secretKey;
@Value("${minio.bucket1.bucket}")
private String bucket;
@Value("${minio.bucket1.publicEndpoint}")
private String publicEndpoint;
@Bean(name = "IMinioClient")
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(endpoint,port,false)
.credentials(accessKey, secretKey)
.build();
}
}
4、常用工具类
@Component
@Slf4j
public class MinioUtil {
private MinioClient minioClient;
@Autowired
public void setMinioClient(@Qualifier("IMinioClient") MinioClient minioClient){
this.minioClient = minioClient;
}
@Value("${minio.bucket1.bucket}")
private String defaultBucketName;
/**
* 上传文件到MinIO桶
*
* @param objectName 存储对象的名称
* @param inputStream 文件输入流
* @param contentType 文件类型
*/
public void uploadFile(String objectName, InputStream inputStream, String contentType) {
this.uploadFile(defaultBucketName, objectName, inputStream, contentType);
}
/**
* 上传文件到MinIO桶
*
* @param bucketName MinIO桶的名称
* @param objectName 存储对象的名称
* @param inputStream 文件输入流
* @param contentType 文件类型
*/
public void uploadFile(String bucketName, String objectName, InputStream inputStream, String contentType) {
try {
boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!isExist) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.contentType(contentType)
.stream(inputStream, inputStream.available(), -1)
.build();
minioClient.putObject(putObjectArgs);
} catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException e) {
handleMinioException(e);
}
}
/**
* 从MinIO桶下载文件
*
* @param objectName 存储对象的名称
* @return 文件输入流
*/
public InputStream downloadFile(String objectName) {
return this.downloadFile(defaultBucketName, objectName);
}
/**
* 从MinIO桶下载文件
*
* @param bucketName MinIO桶的名称
* @param objectName 存储对象的名称
* @return 文件输入流
*/
public InputStream downloadFile(String bucketName, String objectName) {
try {
GetObjectArgs getObjectArgs = GetObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build();
// 读取文件内容到字节数组
try (InputStream inputStream = minioClient.getObject(getObjectArgs)) {
byte[] byteArray = IoUtil.readBytes(inputStream);
return new ByteArrayInputStream(byteArray);
}
} catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException e) {
handleMinioException(e);
return null;
}
}
/**
* 从MinIO桶删除文件
*
* @param objectName 存储对象的名称
*/
public void deleteFile(String objectName) {
this.deleteFile(defaultBucketName, objectName);
}
/**
* 从MinIO桶删除文件
*
* @param bucketName MinIO桶的名称
* @param objectName 存储对象的名称
*/
public void deleteFile(String bucketName, String objectName) {
try {
RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build();
minioClient.removeObject(removeObjectArgs);
} catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException e) {
handleMinioException(e);
}
}
/**
* 批量删除MinIO桶中的文件
*
* @param objectNames 存储对象的名称列表
*/
public void deleteFiles(List<String> objectNames) {
this.deleteFiles(defaultBucketName, objectNames);
}
/**
* 批量删除MinIO桶中的文件
*
* @param bucketName MinIO桶的名称
* @param objectNames 存储对象的名称列表
*/
public void deleteFiles(String bucketName, List<String> objectNames) {
try {
List<DeleteObject> objectsToDelete = new ArrayList<>();
for (String objectName : objectNames) {
objectsToDelete.add(new DeleteObject(objectName));
}
RemoveObjectsArgs removeObjectsArgs = RemoveObjectsArgs.builder()
.bucket(bucketName)
.objects(objectsToDelete)
.build();
minioClient.removeObjects(removeObjectsArgs);
} catch (Exception e) {
handleMinioException(e);
}
}
/**
* 判断MinIO桶中的对象是否存在
*
* @param objectName 存储对象的名称
* @return 文件是否存在
*/
public boolean doesObjectExist(String objectName) {
return this.doesObjectExist(defaultBucketName, objectName);
}
/**
* 判断MinIO桶中的对象是否存在
*
* @param bucketName MinIO桶的名称
* @param objectName 存储对象的名称
* @return 文件是否存在
*/
public boolean doesObjectExist(String bucketName, String objectName) {
boolean exist = true;
try {
minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
} catch (Exception e) {
exist = false;
}
return exist;
}
/**
* 处理MinIO异常
*
* @param e MinIO异常
*/
private void handleMinioException(Exception e) {
log.error("minio操作出现异常,异常为:{}", e.toString());
throw new RuntimeException(e);
}
}