引入maven依赖
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.3.3</version>
</dependency>
编写工具类
package com.tclazz.wiki.util;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
@Data
@Slf4j
@Component
public class MinioUtil {
@Value("${minio.server}")
private String minioServer;
@Value("${minio.username}")
private String username;
@Value("${minio.password}")
private String password;
private MinioClient minioClient;
private MinioClient getMinioClient() {
if (Objects.nonNull(minioClient)) {
return minioClient;
} else {
minioClient = MinioClient.builder().endpoint(minioServer).credentials(username, password).build();
}
return minioClient;
}
public boolean createBucket(String bucketName) {
boolean isCreated = false;
try {
boolean isExist = getMinioClient().bucketExists(BucketExistsArgs.builder().bucket(bucketName.toLowerCase()).build());
if (!isExist) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName.toLowerCase()).build());
isCreated = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return isCreated;
}
public String uploadFile(String bucket, String filePath) throws Exception {
File file = new File(filePath);
String fileName = file.getName();
String newFileName = UUID.randomUUID().toString() + fileName.substring(fileName.indexOf("."));
log.info("文件名称:" + fileName);
getMinioClient().uploadObject(UploadObjectArgs.builder()
.bucket(bucket).object(newFileName).filename(filePath).build()).toString();
return getObjectPrefixUrl(bucket) + newFileName;
}
private String getObjectPrefixUrl(String bucket) {
return String.format("%s/%s/", minioServer, bucket);
}
public String uploadInputStream(String bucket, String fileName, InputStream inputStream) throws Exception {
MinioClient client = getMinioClient();
client.putObject(PutObjectArgs.builder().bucket(bucket).object(fileName).stream(inputStream, inputStream.available(), -1).build());
return getObjectPrefixUrl(bucket) + fileName;
}
public InputStream download(String bucket, String fileName) throws Exception {
MinioClient client = getMinioClient();
return client.getObject(GetObjectArgs.builder().bucket(bucket).object(fileName).build());
}
public void deleteFile(String bucket, String fileName) throws Exception {
MinioClient client = getMinioClient();
client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(fileName).build());
}
public String getSignedUrl(String bucket, String fileName, int expires) throws Exception {
MinioClient client = getMinioClient();
return client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucket).object(fileName).expiry(expires).build());
}
public List<Bucket> listBuckets() throws Exception{
MinioClient client = getMinioClient();
return client.listBuckets();
}
}