podman部署minio

podman安装

podman

设置环境变量

1
VOL_HOME=/home/podman

设置文件夹

1
2
mkdir -p /home/podman/minio/data
mkdir -p /home/podman/minio/config

部署

1
2
3
4
5
6
7
8
9
10
11
podman run -d \
--name minio \
-p 9000:9000 \
-p 9001:9001 \
-e MINIO_ROOT_USER=用户名 \
-e MINIO_ROOT_PASSWORD=密码 \
-v $VOL_HOME/minio/data:/data \
-v $VOL_HOME/minio/config:/root/.minio \
quay.io/minio/minio \
server /data \
--console-address ":9001"

Springboot整合minio

依赖

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
<dependency>  
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.6.5</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.0</version>
</dependency>

yml

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

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();
}
}

controller

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
package com.manpower.controller;  

import com.manpower.util.MFtoFileUtil;
import com.manpower.util.MinioUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@CrossOrigin
@RequestMapping("/image")
@RestController
public class ImageController {
@Resource
private MinioUtil minioUtil;
// 根据文件名获取文件
@GetMapping("/perview/{filename}")
public String preview(@PathVariable("filename") String filename) {
// System.out.println(filename);
return minioUtil.preview(filename);
}

// 上传图片
@PostMapping("/upload/{filename}")
public Boolean upload(@RequestParam("file") MultipartFile file, @PathVariable String filename) {
try {
file = MFtoFileUtil.MFtoFile(file, filename);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return minioUtil.upload(file);
}
}

util

MinioUtil

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 originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
throw new RuntimeException();
}
try {
PutObjectArgs objectArgs = PutObjectArgs
.builder()
.bucket(prop.getBucketName())
.object(file.getName())
.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;
}
}

MockMultipartFile重命名

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
package com.manpower.util;  

import cn.hutool.core.io.FileUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

@Component
public class MFtoFileUtil {

public static MultipartFile MFtoFile(MultipartFile file, String filename) throws Exception {
assert file.getOriginalFilename() != null;
File tempFile = new File(file.getOriginalFilename());
FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile);
//重命名
tempFile = FileUtil.rename(tempFile, filename, true, true);
InputStream inputStream = new FileInputStream(tempFile);
//File转换成MultipartFile
String newFilename = tempFile.getName().substring(0, tempFile.getName().lastIndexOf('.'));
file = new MockMultipartFile(newFilename, tempFile.getName(), "multipart/form-data", inputStream);
//删除临时文件
tempFile.delete();
return file;
}
}