springboot + minio + kkfile实现文件预览

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 Tair(兼容Redis),内存型 2GB
简介: 本文介绍了如何在容器中安装和启动kkfileviewer,并通过Spring Boot集成MinIO实现文件上传与预览功能。首先,通过下载kkfileviewer源码并构建Docker镜像来部署文件预览服务。接着,在Spring Boot项目中添加MinIO依赖,配置MinIO客户端,并实现文件上传与获取预览链接的接口。最后,通过测试验证文件上传和预览功能的正确性。

1、容器安装kkfileviewer

1.1 下载文件

这里以kkfile 4.4.0-beta版本为例

下载kkfile安装包及Dockerfile:codeup.aliyun.com/6254dee9a92…

1.2、构建镜像

bash

代码解读

复制代码

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

1.3、 启动kkfileviewer

arduino

代码解读

复制代码

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添加依赖

xml

代码解读

复制代码

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

2.2、 配置

yaml

代码解读

复制代码

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

2.3、minio配置类

kotlin

代码解读

复制代码

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工具类

java

代码解读

复制代码

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、接口

kotlin

代码解读

复制代码

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文件预览服务中,可以预览文件


转载来源:https://juejin.cn/post/7407384172049891379

目录
打赏
0
3
4
0
136
分享
相关文章
Springboot静态资源映射及文件映射
在Spring Boot项目中,为了解决前端访问后端存储的图片问题,起初尝试通过静态资源映射实现,但发现这种方式仅能访问打包时已存在的文件。对于动态上传的图片(如头像),需采用资源映射配置,将特定路径映射到服务器上的文件夹,确保新上传的图片能即时访问。例如,通过`addResourceHandler(&quot;/img/**&quot;).addResourceLocations(&quot;file:E:\\myProject\\forum_server\\&quot;)`配置,使前端可通过URL直接访问图片。
Springboot静态资源映射及文件映射
Spring Boot 3 整合 Minio 实现文件存储
本文介绍了如何使用 Spring Boot 3 整合 MinIO 实现文件存储服务。MinIO 是一款高性能的对象存储服务器,适合大规模数据存储与分析,支持多种部署环境且文档完备、开源免费。从 MinIO 的快速安装、配置文件公开访问,到 Spring Boot 中集成 MinIO 客户端的步骤,包括创建用户访问密钥、引入依赖包、添加配置信息、编写 MinIO 客户端配置类及上传和预览文件的服务代码。最后通过 Apifox 进行文件上传测试,并验证文件是否成功存储及预览功能是否正常。关注公众号“Harry技术”,回复 minio 获取源码地址。
205 76
|
2月前
|
Spring Boot集成MinIO
本文介绍了如何在Spring Boot项目中集成MinIO,一个高性能的分布式对象存储服务。主要步骤包括:引入MinIO依赖、配置MinIO属性、创建MinIO配置类和服务类、使用服务类实现文件上传和下载功能,以及运行应用进行测试。通过这些步骤,可以轻松地在项目中使用MinIO的对象存储功能。
149 5
SpringBoot获取项目文件的绝对路径和相对路径
SpringBoot获取项目文件的绝对路径和相对路径
173 1
SpringBoot获取项目文件的绝对路径和相对路径
springboot配置hosts文件
springboot配置hosts文件
70 11
|
3月前
|
SpringBoot获取文件将要上传的IP地址
SpringBoot获取文件将要上传的IP地址
52 0
基于SpringBoot+Vue实现的留守儿童爱心网站设计与实现(计算机毕设项目实战+源码+文档)
博主是一位全网粉丝超过100万的CSDN特邀作者、博客专家,专注于Java、Python、PHP等技术领域。提供SpringBoot、Vue、HTML、Uniapp、PHP、Python、NodeJS、爬虫、数据可视化等技术服务,涵盖免费选题、功能设计、开题报告、论文辅导、答辩PPT等。系统采用SpringBoot后端框架和Vue前端框架,确保高效开发与良好用户体验。所有代码由博主亲自开发,并提供全程录音录屏讲解服务,保障学习效果。欢迎点赞、收藏、关注、评论,获取更多精品案例源码。
61 10

数据库

+关注