SpringBoot+minio+kkfile實(shí)現(xiàn)文件預(yù)覽功能
1、容器安裝kkfileviewer
1.1 下載文件
這里以kkfile 4.4.0-beta版本為例
下載kkfile安裝包及Dockerfile:
https://codeup.aliyun.com/6254dee9a923b68581caaf50/kkfileviewer.git
1.2、構(gòu)建鏡像
git clone https://codeup.aliyun.com/6254dee9a923b68581caaf50/kkfileviewer.git cd kkfileviewer docker build -t kkfileview:v4.4.0 .
1.3、 啟動(dòng)kkfileviewer
docker run -d -p 8012:8012 --name kkfileview kkfileview:v4.4.0
1.4、 訪問測(cè)試
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 文件存儲(chǔ)配置信息 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("獲取預(yù)覽鏈接失敗" + 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、測(cè)試
3.1、上傳文件
3.2、獲取minio文件預(yù)覽地址
3.1中返回一個(gè)文件名,該文件名為上傳文件在minio中的唯一名稱,使用該名稱請(qǐng)求minio文件預(yù)覽地址
3.3、文件預(yù)覽
3.2中的接口返回一個(gè)地址,將地址放到kkfileviewer文件預(yù)覽服務(wù)中,可以預(yù)覽文件
以上就是SpringBoot+minio+kkfile實(shí)現(xiàn)文件預(yù)覽功能的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot minio kkfile文件預(yù)覽的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決Idea的選擇文件后定位瞄準(zhǔn)器"Select Opened File"的功能
使用IntelliJ IDEA時(shí),可能會(huì)發(fā)現(xiàn)"SelectOpenedFile"功能不見了,這個(gè)功能允許用戶快速定位到當(dāng)前打開文件的位置,若要找回此功能,只需在IDEA的標(biāo)題欄上右鍵,然后選擇"Always Select Opened File",這樣就可以重新啟用這個(gè)便捷的功能2024-11-11springmvc實(shí)現(xiàn)json交互-requestBody和responseBody
本文主要介紹了springmvc實(shí)現(xiàn)json交互-requestBody和responseBody的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03java實(shí)現(xiàn)的AES加密算法完整實(shí)例
這篇文章主要介紹了java實(shí)現(xiàn)的AES加密算法,結(jié)合完整實(shí)例形式分析了AES加密類的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07java網(wǎng)絡(luò)之基于UDP的聊天程序示例解析
這篇文章主要介紹了java網(wǎng)絡(luò)之基于UDP的聊天程序示例解析,文中通過步驟及示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Java實(shí)現(xiàn)在線語(yǔ)音識(shí)別
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)在線語(yǔ)音識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Java如何導(dǎo)入Jsoup庫(kù)做一個(gè)有趣的爬蟲項(xiàng)目
Jsoup庫(kù)是一款Java的HTML解析器,可用于從網(wǎng)絡(luò)或本地文件中獲取HTML文檔并解析其中的數(shù)據(jù),這篇文章給大家介紹Java導(dǎo)入Jsoup庫(kù)做一個(gè)有趣的爬蟲項(xiàng)目,感興趣的朋友跟隨小編一起看看吧2023-11-11Java多線程通訊之wait,notify的區(qū)別詳解
這篇文章主要介紹了Java多線程通訊之wait,notify的區(qū)別詳解,非常不錯(cuò),具有一定的參考借鑒借鑒價(jià)值,需要的朋友可以參考下2018-07-07IDEA配置使用Maven Helper插件的方法(詳細(xì)配置)
這篇文章主要介紹了Maven Helper插件IDEA配置使用(詳細(xì)配置),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-12-12