当前位置: 代码迷 >> 综合 >> Spring Boot 工程集成 minio 对象存储
  详细解决方案

Spring Boot 工程集成 minio 对象存储

热度:35   发布时间:2024-01-25 06:37:09.0

上一片博文里我们介绍了 minio 对象存储的部署,这篇博文 我们主要介绍下 基于minio和spring boot的工程开发

1. 构建Spring boot 工程

        选择依赖 web,lombok,devops 

2. 修改pom.xml 文件

<properties><minio.version>4.0.0</minio.version>
</properties>
<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>${minio.version}</version>
</dependency>

3. Minio 公共类

public class MinioUtil {private static String minio_url;private static String minio_name;private static String minio_pass;private static String minio_bucketName;/*** * @Title: uploadImage* @Description:上传图片* @param inputStream* @param suffix* @return* @throws Exception*/public static JSONObject uploadImage(InputStream inputStream, String suffix) throws Exception {return upload(inputStream, suffix, "image/jpeg");}/*** @Title: uploadVideo* @Description:上传视频* @param inputStream* @param suffix* @return* @throws Exception*/public static JSONObject uploadVideo(InputStream inputStream, String suffix) throws Exception {return upload(inputStream, suffix, "video/mp4");}/*** @Title: uploadVideo* @Description:上传文件* @param inputStream* @param suffix* @return* @throws Exception*/public static JSONObject uploadFile(InputStream inputStream, String suffix) throws Exception {return upload(inputStream, suffix, "application/octet-stream");}/*** 上传字符串大文本内容* * @Title: uploadString* @Description:描述方法* @param str* @return* @throws Exception*/public static JSONObject uploadString(String str) throws Exception {if (!StringUtils.notNullAndEmpty(str)) {return new JSONObject();}InputStream inputStream = new ByteArrayInputStream(str.getBytes());return upload(inputStream, null, "text/html");}/*** @Title: upload* @Description:上传主功能* @return* @throws Exception*/private static JSONObject upload(InputStream inputStream, String suffix, String contentType) throws Exception {JSONObject map = new JSONObject();PropertiesLoader p = new PropertiesLoader("system.properties");minio_url = p.getProperty("minio_url");minio_name = p.getProperty("minio_name");minio_pass = p.getProperty("minio_pass");minio_bucketName = p.getProperty("minio_bucketName");MinioClient minioClient = new MinioClient(minio_url, minio_name, minio_pass);SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");String ymd = sdf.format(new Date());String objectName = ymd + "/" + UUID.randomUUID().toString() + (suffix != null ? suffix : "");minioClient.putObject(minio_bucketName, objectName, inputStream, contentType);String url = minioClient.getObjectUrl(minio_bucketName, objectName);map.put("flag", "0");map.put("mess", "上传成功");map.put("url", url);map.put("urlval", url);map.put("path", minio_bucketName + "/" + objectName);return map;}
}

4. 上传下载文件

package com.minio.client;import io.minio.MinioClient;
import io.minio.errors.MinioException;
import lombok.extern.slf4j.Slf4j;
import org.xmlpull.v1.XmlPullParserException;import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;@Slf4j
public class FileUploader {public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeyException, XmlPullParserException {try {MinioClient minioClient =new MinioClient("https://minio.sunseaiot.com", "sunseaiot", "sunseaiot",true);// 检查存储桶是否已经存在if(minioClient.bucketExists("ota")) {log.info("Bucket already exists.");} else {// 创建一个名为ota的存储桶minioClient.makeBucket("ota");log.info("create a new bucket.");}//获取下载文件的url,直接点击该url即可在浏览器中下载文件String url = minioClient.presignedGetObject("ota","hello.txt");log.info(url);//获取上传文件的url,这个url可以用Postman工具测试,在body里放入需要上传的文件即可String url2 = minioClient.presignedPutObject("ota","ubuntu.tar");log.info(url2);// 下载文件到本地minioClient.getObject("ota","hello.txt", "/Users/hbl/hello2.txt");log.info("get");// 使用putObject上传一个本地文件到存储桶中。minioClient.putObject("ota","tenant2/hello.txt", "/Users/hbl/hello.txt");log.info("/Users/hbl/hello.txt is successfully uploaded as hello.txt to `task1` bucket.");} catch(MinioException e) {log.error("Error occurred: " + e);}}
} 

5. 介于spring boot 与 minio 的代码结成完毕

  相关解决方案