1. 引入依赖,还是比较新的
<!-- minio 配置-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.1.0</version>
</dependency>
2 在配置文件中写入Minio服务器的IP,用户名,密码,桶名的基本信息。
3 写工具类
public class MinioUtils {
/**
*
* @param bucketName 桶名称
* @param sourceName 上传文件路径名
* @param targetName 保存名称
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
*/
public static RestResponse uploadFile(InputStream source,String targetName)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
RestResponse response = new RestResponse();
try {
//从配置文件中获取endpoint等信息
String endpoint = propertiesUtils.getString("minio.endpoint");
String accessKey =propertiesUtils.getString("minio.accessKey");
String secretKey = propertiesUtils.getString("minio.secretKey");
String bucketName = propertiesUtils.getString("bucket.name");
// Create a minioClient with the MinIO server playground, its access key and secret key.
MinioClient minioClient =
MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
// Make bucketName bucket if not exist.
boolean found =
minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!found) {
// Make a new bucket called
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
} else {
System.out.println("Bucket "+bucketName+" already exists.");
}
//将source上传到bucketName桶中,重命名为tatgetName,可以包含多层路径
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName)
.object(targetName)
.stream(source,source.available(), -1)
.build());
response.setCode(ResultCode.SUCCESS);
response.setMessage("上传成功");
//返回路径
response.setData("/"+targetName);
} catch (MinioException e) {
response.setCode(ResultCode.ERROR);
response.setMessage("上传失败");
}
return response;
}
/**文件下载
@Param fileName 文件路径
*/
public static InputStream downloadFile(String fileName) {
InputStream stream = null;
RestResponse response = new RestResponse();
try {
//获取endpoint等信息
String endpoint = MinIOPropertiesUtils.getString("minio.endpoint");
String accessKey = MinIOPropertiesUtils.getString("minio.accessKey");
String secretKey = MinIOPropertiesUtils.getString("minio.secretKey");
String bucketName = MinIOPropertiesUtils.getString("bucket.name");
// Create a minioClient with the MinIO server playground, its access key and secret key.
MinioClient minioClient =
MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
StatObjectResponse objectStat = minioClient
.statObject(StatObjectArgs.builder().bucket(bucketName).object(fileName).build());
if(objectStat == null) {
response.setCode(ResultCode.ERROR);
response.setMessage("文件不存在");
return null;
}
stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.object(fileName)
.build());
}catch(Exception e) {
response.setCode(ResultCode.ERROR);
response.setMessage("文件不存在");
}
return stream;
}
}
4 写方法调用工具类中方法即可
注意: 1 桶命名时有要求,我现在用的命名不能包含大写字母、下划线和反斜杠,可能还有其他要求,不知道以后会不会改变。
2 可以指明文件类型: .contentType(contentType)
我只用到了上传下载文件,其他方法可以在官网中查询,根据自己的需求进行改进。
附 :官网:https://docs.min.io/docs/java-client-api-reference.html