Minio

毕设用Minio搭建一个对象存储服务得了,不用买oss了

docker搭建Minio

1
2
3
4
5
6
docker run -d -p 9000:9000 -p 9001:9001 --name minio \
-e "MINIO_ROOT_USER=root" \
-e "MINIO_ROOT_PASSWORD=01020304" \
-v /mnt/data:/data \
-v /mnt/config:/root/.minio \
minio/minio server --console-address ":9001" /data

密码最低八位….,不然启动失败:
20240128115859

minio concole配置

文件上传后默认是不会返回url的,但是可以获取临时的访问链接,这个链接最长7天,不符合业务要求,所以可以新增存储桶的规则:
20240128141356

这样就可以直接通过ip:port/bucket/filename来访问了

Springboot整合minio

依赖

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

配置文件

1
2
3
4
5
minio:
endpoint: http://xxxx:9000
access-key: paJorTxxxxxx5PWJ1
secre-key: gwDjy61ZhVExxxxxxw4ujifoUrCNSM
bucketName: food.guide

配置类

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
package com.zzmr.fgback.config;

import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author zzmr
* @create 2024-01-28 12:16
*/
@Configuration
public class MinioConfig {

@Autowired
private MinioProperties minioProperties;

@Bean
public MinioClient minioClient() {
MinioClient minioClient = MinioClient.builder()
.endpoint(minioProperties.getEndpoint())
.credentials(minioProperties.getAccessKey(), minioProperties.getSecreKey())
.build();
return minioClient;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.zzmr.fgback.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
* @author zzmr
* @create 2024-01-28 12:16
*/
@Component
@Data
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {
private String endpoint;
private String accessKey;
private String secreKey;
}

工具类

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

import com.zzmr.fgback.config.MinioProperties;
import com.zzmr.fgback.exception.BaseException;
import com.zzmr.fgback.result.Result;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

/**
* @author zzmr
* @create 2024-01-28 13:58
*/
@Component
public class MinioUtils {

@Autowired
private MinioClient minioClient;

@Value("${minio.bucketName}")
private String bucketName;

@Autowired
private MinioProperties minioProperties;

public String upload(MultipartFile file) {
String originalFileName = file.getOriginalFilename();
// 获取文件后缀
String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
// 使用UUID来代替原文件名,避免重复
UUID uuidName = UUID.randomUUID();
String imgName = uuidName + suffix;
try {
// 文件上传
InputStream in = file.getInputStream();
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName)
.object(imgName)
.stream(in, file.getSize(), -1)
.contentType(file.getContentType())
.build());
in.close();
System.out.println("上传成功: " + imgName);

// http://47.109.139.173:9000/food.guide/%E5%B0%8F%E5%9B%BE.jpg
String imgUrl = minioProperties.getEndpoint() + "/" + bucketName + "/" + imgName;
return imgUrl;
} catch (Exception e) {
throw new BaseException("上传失败");
}
}
}

上传接口

1
2
3
4
5
6
7
8
9
@ApiOperation("上传一个")
@PostMapping("/upload")
public Result upload(@RequestParam(name = "file") MultipartFile file) {
if (file == null) {
throw new BaseException("文件为空!");
}
String imgUrl = minioUtils.upload(file);
return Result.success(imgUrl);
}

总结

还是挺简单的,速度受限于服务器的速度