pom文件引入Minio
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>3.0.10</version>
</dependency>
#配置文件配置Minio配置参数
minio:
endpoint: http://localhost:8080
bucketName: test
accessKey: xxx
secretKey: xxx
Minio工具类
@Component
public class MinIoUtils{
@Value("${minio.endpoint}")
private String ENDPOINT;
@Value("${minio.bucketName}")
private String BUCKETNAME;
@Value("${minio.accessKey}")
private String ACCESSKEY;
@Value("${minio.secretKey}")
private String SECRETKEY;
//上传
public String upload(MultipartFile file) {
try {
String filename = file.getOriginalFilename();
MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY,
SECRETKEY);
if (!minioClient.bucketExists(BUCKETNAME)) {
minioClient.makeBucket(BUCKETNAME);
minioClient.setBucketPolicy(BUCKETNAME, "*.*",
PolicyType.READ_ONLY);
}
minioClient.putObject(BUCKETNAME, filename , file.getInputStream(),
file.getContentType());
return ENDPOINT + "/" + BUCKETNAME + "/" + filename;
} catch (Exception exception) {
throw new TestException("上传失败!请稍后再试");
}
}
//删除
public String delete(String filename){
try {
MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY,
SECRETKEY);
minioClient.removeObject(BUCKETNAME, filename);
} catch (Exception e) {
return "删除失败"+e.getMessage();
}
return "删除成功";
}
//下载
public void downloadFiles(String filename, HttpServletResponse httpResponse) {
try {
MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY);
InputStream object = minioClient.getObject(BUCKETNAME, filename);
byte buf[] = new byte[1024];
int length = 0;
httpResponse.reset();
httpResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
httpResponse.setContentType("application/octet-stream");
httpResponse.setCharacterEncoding("utf-8");
OutputStream outputStream = httpResponse.getOutputStream();
while ((length = object.read(buf)) > 0) {
outputStream.write(buf, 0, length);
}
outputStream.close();
} catch (Exception ex) {
log.info("导出失败:", ex.getMessage());
}
}
}
这就基本实现了Minio上传下载删除的基本功能!