1、下载地址:
MinIO下载和安装 | 用于创建高性能对象存储的代码和下载内容
2、启动minio
set MINIO_ROOT_USER=admin
set MINIO_ROOT_PASSWORD=12345678
(双击无法启动,需要使用cmd命令板启动。解决办法:编写cmd启动命令文件或加载到开机启动)
minio.exe server D:\初始化存放路径\minio
出线如下画面说明启动成功
3、使用上边Console的地址及用户名密码登录页面客户端
具体操作参考API文档:Java Client API Reference — MinIO中文文档 | MinIO Linux中文文档
4、java代码实现
1)配置-前期准备
导入依赖包:
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.0.3</version>
</dependency>
配置文件配置连接信息
minio:
#注意此处是https,由于后续讨论协议问题因此提前修改,不需要的可自行修改为http
endpoint: https://xxx.xxx.xx:9000
accesskey: minioadmin #你的服务账号
secretkey: minioadmin #你的服务密码
配置MinioInfo文件
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioInfo {
private String endpoint;
private String accesskey;
private String secretkey;
}
配置MinioConfig文件
@Configuration
@EnableConfigurationProperties(MinioInfo.class)
public class MinioConfig {
@Autowired
private MinioInfo minioInfo;
/**
* 获取 MinioClient
*/
@Bean
public MinioClient minioClient() throws NoSuchAlgorithmException, KeyManagementException {
return MinioClient.builder().endpoint(minioInfo.getEndpoint())
.credentials(minioInfo.getAccesskey(),minioInfo.getSecretkey())
.build();
}
}
2)上传、下载、删除文件
MinioUtils类
import io.minio.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
/**
* @author: MM
* @date: 2023-03-09 10:09
*/
@Slf4j
@Component
public class MinioUtils {
@Autowired
private MinioClient minioClient;
@Autowired
private MinioInfo minioInfo;
/**
* 上传文件
* @param file
* @param bucketName
* @return
* @throws Exception
*/
public String uploadFile(MultipartFile file,String bucketName){
if (null==file || 0 == file.getSize()){
log.error("msg","上传文件不能为空");
return null;
}
try {
//判断是否存在
createBucket(bucketName);
//原文件名
String originalFilename=file.getOriginalFilename();
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(originalFilename).stream(
file.getInputStream(), file.getSize(), -1)
.contentType(file.getContentType())
.build());
return minioInfo.getEndpoint()+"/"+bucketName+"/"+originalFilename;
}catch (Exception e){
log.error("上传失败:{}",e.getMessage());
}
log.error("msg","上传失败");
return null;
}
/**
* 通过字节流上传
* @param imageFullPath
* @param bucketName
* @param imageData
* @return
*/
public String uploadImage(String imageFullPath,
String bucketName,
byte[] imageData){
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageData);
try {
//判断是否存在
createBucket(bucketName);
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(imageFullPath)
.stream(byteArrayInputStream,byteArrayInputStream.available(),-1)
.contentType(".jpg")
.build());
return minioInfo.getEndpoint()+"/"+bucketName+"/"+imageFullPath;
}catch (Exception e){
log.error("上传失败:{}",e.getMessage());
}
log.error("msg","上传失败");
return null;
}
/**
* 删除文件
* @param bucketName
* @param fileName
* @return
*/
public int removeFile(String bucketName,String fileName){
try {
//判断桶是否存在
boolean res=minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (res) {
//删除文件
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName)
.object(fileName).build());
}
} catch (Exception e) {
System.out.println("删除文件失败");
e.printStackTrace();
return 1;
}
System.out.println("删除文件成功");
return 0;
}
/**
* 下载文件
* @param fileName
* @param bucketName
* @param response
*/
public void fileDownload(String fileName,
String bucketName,
HttpServletResponse response) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
if (StringUtils.isBlank(fileName)) {
response.setHeader("Content-type", "text/html;charset=UTF-8");
String data = "文件下载失败";
OutputStream ps = response.getOutputStream();
ps.write(data.getBytes("UTF-8"));
return;
}
outputStream = response.getOutputStream();
// 获取文件对象
inputStream =minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
byte buf[] = new byte[1024];
int length = 0;
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=" +
URLEncoder.encode(fileName.substring(fileName.lastIndexOf("/") + 1), "UTF-8"));
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
// 输出文件
while ((length = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, length);
}
System.out.println("下载成功");
inputStream.close();
} catch (Throwable ex) {
response.setHeader("Content-type", "text/html;charset=UTF-8");
String data = "文件下载失败";
try {
OutputStream ps = response.getOutputStream();
ps.write(data.getBytes("UTF-8"));
}catch (IOException e){
e.printStackTrace();
}
} finally {
try {
outputStream.close();
if (inputStream != null) {
inputStream.close();
}}catch (IOException e){
e.printStackTrace();
}
}
}
@SneakyThrows
public void createBucket(String bucketName) {
//如果不存在就创建
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
}
}
Controller使用
上传
/**
* 上传文件
* @param file
* @return
*/
@PostMapping(value = "/v1/minio/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public JSONObject uploadByMinio(@RequestParam(name = "file")MultipartFile file) {
JSONObject jso = new JSONObject();
//返回存储路径
String path = minioUtils.uploadFile(file, "musicearphone");
jso.put("code", 2000);
jso.put("data", path);
return jso;
}
下载
/**
* 下载文件 根据文件名
* @param fileName
* @param response
*/
@GetMapping("/v1/get/download")
public void download(@RequestParam(name = "fileName") String fileName,
HttpServletResponse response){
try {
minioUtils.fileDownload(fileName,"musicearphone",response);
}catch (Exception e){
e.printStackTrace();
}
}
删除
/**
* 通过文件名删除文件
* @param fileName
* @return
*/
@PostMapping("/v1/minio/delete")
@ResponseBody
public JSONObject deleteByName(String fileName){
JSONObject jso = new JSONObject();
int res = minioUtils.removeFile("musicearphone", fileName);
if (res!=0){
jso.put("code",5000);
jso.put("msg","删除失败");
}
jso.put("code",2000);
jso.put("msg","删除成功");
return jso;
}
代码实现模块参考:Java实现minio上传、下载、删除文件,支持https访问_minio下载文件-CSDN博客。
如涉及隐私或利益请联系删除