SpringBoot2整合minio
minio各个操作实例参考:https://github.com/minio/minio-java/tree/master/examples
1、基础依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>3.0.12</version>
</dependency>
2、基础配置
配置要素:地址和端口,登录名,密码,图片Bucket。
minio:
endpoint: http://10.0.0.70:9000
accessKey: admin
secretKey: admin123456
bucketNameImage: static-img
文件上传之后,可以基于文件地址直接访问,但是需要在MinIO中配置文件的读写权限:
3、配置参数类
@Data
@Configuration
public class MinioConfig {
@Value("${minio.endpoint}")
private String endpoint;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Value("${minio.bucketNameImage}")
private String bucketNameImage ;
@Bean
public MinioClient minioClient(){
MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
return minioClient;
}
}
4.minio服务工具类
import com.dkn.config.MinioConfig;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 参考 https://github.com/minio/minio-java/tree/master/examples
*/
@Service
@Slf4j
public class MinioService {
@Autowired
MinioConfig minioConfig;
@Autowired
MinioClient minioClient;
//获取列表
public List<String> listObjects() {
List<String> list=new ArrayList<>();
try {
ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder()
.bucket(minioConfig.getBucketNameImage())
.build();
Iterable<Result<Item>> results =minioClient.listObjects(listObjectsArgs);
for (Result<Item> result : results) {
Item item = result.get();
log.info(item.lastModified() + ", " + item.size() + ", " + item.objectName());
list.add(item.objectName());
}
}catch (Exception e){
log.error("错误:"+e.getMessage());
}
return list;
}
//删除
public void deleteObject(String objectName) {
try {
RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
.bucket(minioConfig.getBucketNameImage())
.object(objectName)
.build();
minioClient.removeObject(removeObjectArgs);
}catch (Exception e){
log.error("错误:"+e.getMessage());
}
}
//上传
public void uploadObject(InputStream is,String fileName,String contentType) {
try {
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(minioConfig.getBucketNameImage())
.object(fileName)
.contentType(contentType)
.stream(is, is.available(), -1)
.build();
minioClient.putObject(putObjectArgs);
is.close();
}catch (Exception e){
log.error("错误:"+e.getMessage());
}
}
//获取minio中地址
public String getObjectUrl(String objectName){
try {
GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket(minioConfig.getBucketNameImage())
.object(objectName)
.expiry(7, TimeUnit.DAYS)
.build();
return minioClient.getPresignedObjectUrl(getPresignedObjectUrlArgs);
}catch (Exception e){
e.printStackTrace();
log.error("错误:"+e.getMessage());
}
return "";
}
//下载minio服务的文件
public InputStream getObject(String objectName){
try {
GetObjectArgs getObjectArgs = GetObjectArgs.builder()
.bucket(minioConfig.getBucketNameImage())
.object(objectName)
.build();
return minioClient.getObject(getObjectArgs);
}catch (Exception e){
log.error("错误:"+e.getMessage());
}
return null;
}
}
5.控制器类
@Slf4j
@Controller
public class MinioController {
@Autowired
MinioService minioService;
@GetMapping("/")
public String index(){
return "redirect:/minio/list";
}
//列表
@GetMapping("/minio/list")
public String list(Model model){
model.addAttribute("list",minioService.listObjects());
return "list";
}
//删除
@GetMapping("/minio/delete")
@ResponseBody
public AjaxResult delete(String filename){
minioService.deleteObject(filename);
return AjaxResult.success();
}
//上传文件
@PostMapping("/minio/upload")
public String upload(@RequestParam("file") MultipartFile file) {
try {
InputStream is= file.getInputStream(); //得到文件流
String fileName = file.getOriginalFilename(); //文件名
String newFileName= System.currentTimeMillis()+"."+ StringUtils.substringAfterLast(fileName,".");
String contentType = file.getContentType(); //类型
minioService.uploadObject(is,newFileName,contentType);
}catch (Exception e){
log.error("上传失败");
}
return "redirect:/minio/list";
}
//下载minio服务的文件
@GetMapping("/minio/download")
public void download(String filename,HttpServletResponse response){
try {
InputStream fileInputStream = minioService.getObject(filename);
response.setHeader("Content-Disposition", "attachment;filename=" + "test.jpg");
response.setContentType("application/force-download");
response.setCharacterEncoding("UTF-8");
IOUtils.copy(fileInputStream,response.getOutputStream());
}catch (Exception e){
log.error("下载失败");
}
}
//获取minio文件的下载地址
@GetMapping("/minio/getHttpUrl")
@ResponseBody
public AjaxResult getHttpUrl(String filename){
try {
String url = minioService.getObjectUrl(filename);
return AjaxResult.success(AjaxResult.buildMap("url",url));
}catch (Exception e){
return AjaxResult.error();
}
}
}