⭐⭐⭐ Spring Boot 项目实战 ⭐⭐⭐ Spring Cloud 项目实战
《Dubbo 实现原理与源码解析 —— 精品合集》 《Netty 实现原理与源码解析 —— 精品合集》
《Spring 实现原理与源码解析 —— 精品合集》 《MyBatis 实现原理与源码解析 —— 精品合集》
《Spring MVC 实现原理与源码解析 —— 精品合集》 《数据库实体设计合集》
《Spring Boot 实现原理与源码解析 —— 精品合集》 《Java 面试题 + Java 学习指南》

摘要: 原创出处 juejin.cn/post/7407384172049891379 「小太阳」欢迎转载,保留摘要,谢谢!


🙂🙂🙂关注微信公众号:【芋道源码】有福利:

  1. RocketMQ / MyCAT / Sharding-JDBC 所有源码分析文章列表
  2. RocketMQ / MyCAT / Sharding-JDBC 中文注释源码 GitHub 地址
  3. 您对于源码的疑问每条留言都将得到认真回复。甚至不知道如何读源码也可以请教噢。
  4. 新的源码解析文章实时收到通知。每周更新一篇左右。
  5. 认真的源码交流微信群。

1、容器安装kkfileviewer

1.1 下载文件

这里以kkfile 4.4.0-beta版本为例

下载kkfile安装包及Dockerfile:

https://codeup.aliyun.com/6254dee9a923b68581caaf50/kkfileviewer.git

1.2、构建镜像

git clone https://codeup.aliyun.com/6254dee9a923b68581caaf50/kkfileviewer.git
cd kkfileviewer
docker build -t kkfileview:v4.4.0 .

1.3、 启动kkfileviewer

docker run -d -p 8012:8012 --name kkfileview kkfileview:v4.4.0

1.4、 访问测试

http://you-ip:8012

2、springboot集成minio

2.1 pom.xml添加依赖

<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.11</version>
</dependency>

2.2、 配置

# minio 文件存储配置信息
minio:
endpoint: http://xxxxx:9000
accessKey: xxxx
secretKey: xxxxx
bucketName: test

2.3、minio配置类

package com.sunny.config;

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

@Configuration
public class MinioConfig {

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

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

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

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


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

2.4、 minio工具类

package com.sunny.utils;

import com.sunny.entity.common.ApiResult;
import com.sunny.exception.AppException;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.http.Method;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Component
public class MinioUtils {

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

@Resource
private MinioClient minioClient;

public ApiResult uploadFile(MultipartFile file) throws AppException {
String fileName = System.currentTimeMillis() + file.getOriginalFilename();
try (InputStream fi = file.getInputStream()) {
PutObjectArgs putObjectArgs = PutObjectArgs.builder().bucket(bucketName).contentType(file.getContentType()).object(fileName).stream(fi, fi.available(), -1).build();
minioClient.putObject(putObjectArgs);
} catch (Exception e) {
throw new AppException("文件上传失败" + e.getMessage());
}
return ApiResult.ok(fileName);
}

public ApiResult getPreviewUrl(String objectName) throws AppException {
try {
GetPresignedObjectUrlArgs urlArgs = GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(objectName).method(Method.GET).build();
return ApiResult.ok(minioClient.getPresignedObjectUrl(urlArgs));
} catch (Exception e) {
throw new AppException("获取预览链接失败" + e.getMessage());
}
}
}

2.5、接口

package com.sunny.controller;

import com.sunny.entity.common.ApiResult;
import com.sunny.exception.AppException;
import com.sunny.utils.MinioUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
public class FileOperationController {

@Resource
private MinioUtils minioUtils;

@PostMapping("/upload")
public ApiResult upload(MultipartFile file) throws AppException {
return minioUtils.uploadFile(file);
}

@GetMapping("/getPreviewUrl")
public ApiResult getPreviewUrl(String fileName) throws AppException {
return minioUtils.getPreviewUrl(fileName);
}
}

3、测试

3.1、上传文件

3.2、获取minio文件预览地址

3.1中返回一个文件名,该文件名为上传文件在minio中的唯一名称,使用该名称请求minio文件预览地址

3.3、文件预览

3.2中的接口返回一个地址,将地址放到kkfileviewer文件预览服务中,可以预览文件

文章目录
  1. 1. 1、容器安装kkfileviewer
    1. 1.1. 1.1 下载文件
    2. 1.2. 1.2、构建镜像
    3. 1.3. 1.3、 启动kkfileviewer
    4. 1.4. 1.4、 访问测试
  2. 2. 2、springboot集成minio
    1. 2.1. 2.1 pom.xml添加依赖
    2. 2.2. 2.2、 配置
    3. 2.3. 2.3、minio配置类
    4. 2.4. 2.4、 minio工具类
    5. 2.5. 2.5、接口
  3. 3. 3、测试
    1. 3.1. 3.1、上传文件
    2. 3.2. 3.2、获取minio文件预览地址
    3. 3.3. 3.3、文件预览