依赖

1
2
3
4
5
<dependency>  
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.0</version>
</dependency>

Config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.manpower.config;  

import io.minio.MinioClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
private String endpoint;
private String bucketName;
private String accessKey;
private String secretKey;

@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}

util

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.manpower.util;  

import com.manpower.config.MinioConfig;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.lang.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class MinioUtil {
@Resource
private MinioConfig prop;

@Resource
private MinioClient minioClient;

// 创建存储bucket
// @return Boolean
public Boolean makeBucket(String bucketName) {
try {
minioClient.makeBucket(MakeBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}

// 删除存储bucket
// @return Boolean
public Boolean removeBucket(String bucketName) {
try {
minioClient.removeBucket(RemoveBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}

// 获取全部bucket
// @return List<Bucket>
public List<Bucket> getAllBuckets() {
try {
return minioClient.listBuckets();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

// 文件上传
// @return Boolean
public Boolean upload(MultipartFile file, String filename) {
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
throw new RuntimeException();
}
try {
PutObjectArgs objectArgs = PutObjectArgs
.builder()
.bucket(prop.getBucketName())
.object(filename)
.stream(file.getInputStream(), file.getSize(), -1)
.contentType(file.getContentType())
.build();
minioClient.putObject(objectArgs);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}

// 预览图片
// @return String
public String preview(String fileName) {
GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs
.builder()
.bucket(prop.getBucketName())
.object(fileName)
.method(Method.GET)
.build();
try {
return minioClient.getPresignedObjectUrl(build);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

yml

1
2
3
4
5
minio:  
endpoint: http://1.116.135.197:9000
bucketName: image
accessKey: root
secretKey: csh20011103

controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.manpower.controller;  

import com.manpower.config.MinioConfig;
import com.manpower.util.MinioUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RequestMapping("/image")
@RestController
public class ImageController {
@Resource
private MinioUtil minioUtil;
@Resource
private MinioConfig prop;

@GetMapping("/perview/{filename}")
public String preview(@PathVariable("filename") String filename) {
return minioUtil.preview(filename);
}
}