springboot + minio + kkfile实现文件预览

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文件预览地址
image.png

3.3、文件预览

3.2中的接口返回一个地址,将地址放到kkfileviewer文件预览服务中,可以预览文件
在这里插入图片描述

实现Spring BootMinio的大文件上传并解压的功能,可以按照以下步骤进行操作: 1. 首先,在Spring Boot项目的pom.xml文件中引入Minio的依赖。可以使用以下代码将Minio的依赖添加到项目中: ``` <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>8.2.1</version> </dependency> ``` 2. 然后,将大文件分片并上传到Minio对象存储服务。可以按照以下步骤进行操作: - 将要上传的大文件进行分片处理,可以使用文件分片算法将大文件分成多个小块。 - 将分片后的文件依次上传到Minio对象存储服务,并指定一个临时文件名。 3. 分片上传完成后,将所有分片合并为一个完整的文件。可以按照以下步骤进行操作: - 从Minio中读取所有分片文件的流。 - 将分片流写入到一个临时文件中,以便后续解压。 4. 解压临时文件。可以按照以下步骤进行操作: - 使用合适的解压库或工具,对临时文件进行解压。 - 解压后的文件可以按需求进行进一步处理,例如再次上传到Minio或其他存储系统。 综上所述,要实现Spring BootMinio的大文件上传并解压功能,需要将文件分片并上传到Minio,然后将分片合并为完整文件,最后解压文件。这样可以实现文件的上传和解压操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Spring boot + minio 分片上传](https://blog.csdn.net/XIAOTONGZHU/article/details/130346735)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [SpringBoot 使用 Minio 进行文件存储](https://blog.csdn.net/qq_43692950/article/details/125961685)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Windows常用软件压缩包,后端Java适用于springboot 2.6.x等高版本](https://download.csdn.net/download/m0_55710969/85062866)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值