文章目录
- 1.linux安装minio
- 2.springboot整合minio使用
1.linux安装minio
1.新建文件夹
mkdir /home/minio
# 数据文件夹
mkdir /home/minio/data
# 创建日志文件夹
mkdir /home/minio/log
2.进入文件夹
cd /home/minio
3.下载minio,链接可从官网获取
wget https://dl.minio.org.cn/server/minio/release/linux-amd64/minio
下载下来后会在当前所在文件夹看到minio文件
4.赋予权限
要给下载下来的minio赋予权限,不然后面无法启动
chmod +x /home/minio/minio
赋予权限之后如图
5.开放端口
因为我后面会指定minio使用9090和9000端口,所以先开放这两个端口,如果你指定的是别的端口,那这里也要相应放行对应的端口
sudo firewall-cmd --zone=public --add-port=9090/tcp --permanent
sudo firewall-cmd --zone=public --add-port=9000/tcp --permanent
6.重启防火墙
sudo firewall-cmd --reload
#查看开放的端口
sudo firewall-cmd --list-ports
7.启动
启动最好指定特定端口,不然很麻烦
9000端口是java api上传文件时的连接接口,而9090是浏览器访问时的访问端口,不要混淆!!
address和:之间有一个空格
#前台启动,关闭ssh连接后自动关闭minio
/home/minio/minio server /home/minio/data --address :9000 --console-address :9090 > /home/minio/log/minio.log &
#后台启动
nohup /home/minio/minio server /home/minio/data --address :9000 --console-address :9090 > /home/minio/log/minio.log &
启动成功时查看minio.log可以看到如图
8.访问
http://ip:9090/
默认用户名密码都是minioadmin
9.修改默认密码
注意密码至少要8位,否则会出现启动不了的情况!!,具体可以查看minio.log日志
export MINIO_ROOT_USER=admin
export MINIO_ROOT_PASSWORD=12345678
10.手动创建Buckets
需要提前创建Buckets,当然也可以后面用代码创建,上传的文件都是存在Buckets中的
2.springboot整合minio使用
添加依赖
<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>7.0.2</version>
</dependency>
application.yml添加minio的配置
minio:url: http://192.168.0.107port: 9000accessKey: minioadminsecretKey: minioadmin
MinIOConfig配置类
@Configuration
public class MinIOConfig {/*** minio地址*/@Value("${minio.url}")private String endpoint;/*** 端口*/@Value("${minio.port}")private int port;/*** minio用户名*/@Value("${minio.accessKey}")private String accessKey;/*** minio密码*/@Value("${minio.secretKey}")private String secretKey;@Beanpublic MinioClient minioClient() {try {return new MinioClient(endpoint, port, accessKey, secretKey, false);} catch (InvalidEndpointException | InvalidPortException e) {throw new RuntimeException(e);}}
}
minio工具类
@Slf4j
@Component
public class MinIoUtil {@Autowiredprivate MinioClient minioClient;/*** 判断 bucket是否存在** @param bucketName 桶名* @return*/@SneakyThrows(Exception.class)public boolean bucketExists(String bucketName) {return minioClient.bucketExists(bucketName);}/*** 创建 bucket** @param bucketName 桶名*/@SneakyThrows(Exception.class)public void createBucket(String bucketName) {boolean isExist = minioClient.bucketExists(bucketName);if (!isExist) {minioClient.makeBucket(bucketName);}}/*** 桶名** @return*/@SneakyThrows(Exception.class)public List<Bucket> getAllBuckets() {return minioClient.listBuckets();}/*** 文件上传** @param bucketName 桶名* @param fileName 文件名* @param filePath 文件名* @return*/@SneakyThrows(Exception.class)public String upload(String bucketName, String fileName, String filePath) {minioClient.putObject(bucketName, fileName, filePath, null);return getFileUrl(bucketName, fileName);}/*** 文件上传** @param bucketName 桶名* @param fileName 文件名* @param stream 文件流* @return*/@SneakyThrows(Exception.class)public String upload(String bucketName, String fileName, InputStream stream) {minioClient.putObject(bucketName, fileName, stream, new PutObjectOptions(stream.available(), -1));return getFileUrl(bucketName, fileName);}/*** 文件上传** @param bucketName 桶名* @param file 文件* @return*/@SneakyThrows(Exception.class)public String upload(String bucketName, MultipartFile file) {final InputStream is = file.getInputStream();final String fileName = file.getOriginalFilename();minioClient.putObject(bucketName, fileName, is, new PutObjectOptions(is.available(), -1));is.close();return getFileUrl(bucketName, fileName);}/*** 删除文件** @param bucketName 桶名* @param fileName 文件名*/@SneakyThrows(Exception.class)public void deleteFile(String bucketName, String fileName) {minioClient.removeObject(bucketName, fileName);}/*** 下载文件** @param bucketName 桶名* @param fileName 文件名* @param response*/@SneakyThrows(Exception.class)public void download(String bucketName, String fileName, HttpServletResponse response) {// 获取对象的元数据final ObjectStat stat = minioClient.statObject(bucketName, fileName);response.setContentType(stat.contentType());response.setCharacterEncoding("UTF-8");response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));InputStream is = minioClient.getObject(bucketName, fileName);IOUtils.copy(is, response.getOutputStream());is.close();}/*** 获取minio文件的下载地址** @param bucketName 桶名* @param fileName 文件名* @return*/@SneakyThrows(Exception.class)public String getFileUrl(String bucketName, String fileName) {return minioClient.presignedGetObject(bucketName, fileName);}
}
测试用例
@Test
public void minioUpload(){String imageUrl = minIoUtil.upload("tsc", "aa", "C:\\Users\\18316\\Pictures\\Camera Roll\\a.jpg");System.out.println(imageUrl);}