主页 > 创业  > 

SpringBoot项目集成MinIO

SpringBoot项目集成MinIO

最近在学习MinIO,所以想让自己的SpringBoot项目集成MinIO,在网上查阅资料,并进行操作的过程中遇到一些问题,所以想把自己遇到的坑和完成步骤记录下来供自己和各位查阅。

一. MinIO的下载安装以及基本使用

1. 下载地址: dl.min.io/server/minio/release/windows-amd64/minio.exe

2. 下载好后需要手动创建data文件夹用于存储MinIO中的数据。

3. 键入cmd

4. 设置MinIO的一些变量

set MINIO_ROOT_USER=admin

set MINIO_ROOT_PASSWORD=admin123

set MINIO_ACCESS_KEY=admin

set MINIO_SECRET_KEY=admin123

下面是我踩的坑,如果只设置了MINIO_ROOT_USER和MINIO_ROOT_PASSWORD的值,而不设置MINIO_ACCESS_KEY和MINIO_SECRET_KEY的值,当启动minio服务的时候就会报以下异常:

所以一定要设置好后面两个变量的值。

5. 启动minio服务

> minio.exeserver data

启动后就会出现minio服务的地址,按住ctrl再点击即可访问该网址。

6.进入登录页面后,输入对应的用户名和密码,也就是之前设置的MINIO_ROOT_USER和MINIO_ROOT_PASSWORD的值。

7. 进入主界面后,点击左侧导航栏中的Buckets,然后点击Create Bucket。

8. 创建好之后,点击左侧导航栏中的Object Browser就可以看到创建好的桶了。

9. 进入该桶,点击upload,上传一个文件,桶的默认权限是private,所以外界访问不到,需要修改访问权限为public,但是要注意安全问题。

10. 点击左侧导航栏中的Buckets,进入该桶,修改权限为public,这样外界就可以访问上传的文件了。

二. SpringBoot集成MinIO 引入依赖 io.minio minio 7.0.2

2. 编写配置文件

server: port: 8081 spring:

配置文件上传大小限制

servlet: multipart: max-file-size: 200MB max-request-size: 200MB minio: host: http://127.0.0.1:9000 url: m i n i o . h o s t / {minio.host}/ minio.host/{minio.bucket}/ access-key: minioadmin secret-key: minioadmin bucket: public

3. 添加配置文件

package com.suwell.serv.ocr ponent; ? import io.minio.MinioClient; import io.minio.ObjectStat; import io.minio.PutObjectOptions; import io.minio.Result; import io.minio.messages.Bucket; import io.minio.messages.Item; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.util.Assert; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.util.UriUtils; ? import javax.servlet.http.HttpServletResponse; import java.io.InputStream; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; ? ? @Component public class MinioConfig implements InitializingBean { ? ? ?@Value(value = "${minio.bucket}") ? ?private String bucket; ? ? ?@Value(value = "${minio.host}") ? ?private String host; ? ? ?@Value(value = "${minio.url}") ? ?private String url; ? ? ?@Value(value = "${minio.access-key}") ? ?private String accessKey; ? ? ?@Value(value = "${minio.secret-key}") ? ?private String secretKey; ? ? ?private MinioClient minioClient; ? ? ?@Override ? ?public void afterPropertiesSet() throws Exception { ? ? ? ?Assert.hasText(url, "Minio url 为空"); ? ? ? ?Assert.hasText(accessKey, "Minio accessKey为空"); ? ? ? ?Assert.hasText(secretKey, "Minio secretKey为空"); ? ? ? ?this.minioClient = new MinioClient(this.host, this.accessKey, this.secretKey); ? } ? ? ? ? ?/** ? ? * 上传 ? ? */ ? ?public String putObject(MultipartFile multipartFile) throws Exception { ? ? ? ?// bucket 不存在,创建 ? ? ? ?if (!minioClient.bucketExists(this.bucket)) { ? ? ? ? ? ?minioClient.makeBucket(this.bucket); ? ? ? } ? ? ? ?try (InputStream inputStream = multipartFile.getInputStream()) { ? ? ? ? ? ?// 上传文件的名称 ? ? ? ? ? ?String fileName = multipartFile.getOriginalFilename(); ? ? ? ? ? ?// PutObjectOptions,上传配置(文件大小,内存中文件分片大小) ? ? ? ? ? ?PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE); ? ? ? ? ? ?// 文件的ContentType ? ? ? ? ? ?putObjectOptions.setContentType(multipartFile.getContentType()); ? ? ? ? ? ?minioClient.putObject(this.bucket, fileName, inputStream, putObjectOptions); ? ? ? ? ? ?// 返回访问路径 ? ? ? ? ? ?return this.url + UriUtils.encode(fileName, StandardCharsets.UTF_8); ? ? ? } ? } ? ? ?/** ? ? * 文件下载 ? ? */ ? ?public void download(String fileName, HttpServletResponse response){ ? ? ? ?// 从链接中得到文件名 ? ? ? ?InputStream inputStream; ? ? ? ?try { ? ? ? ? ? ?MinioClient minioClient = new MinioClient(host, accessKey, secretKey); ? ? ? ? ? ?ObjectStat stat = minioClient.statObject(bucket, fileName); ? ? ? ? ? ?inputStream = minioClient.getObject(bucket, fileName); ? ? ? ? ? ?response.setContentType(stat.contentType()); ? ? ? ? ? ?response.setCharacterEncoding("UTF-8"); ? ? ? ? ? ?response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); ? ? ? ? ? ?byte[] buffer = new byte[1024]; ? ? ? ? ? ?int length; ? ? ? ? ? ?while ((length = inputStream.read(buffer)) > 0) { ? ? ? ? ? ? ? ?response.getOutputStream().write(buffer, 0, length); ? ? ? ? ? } ? ? ? ? ? ?inputStream.close(); ? ? ? } catch (Exception e){ ? ? ? ? ? ?e.printStackTrace(); ? ? ? ? ? ?System.out.println("有异常:" + e); ? ? ? } ? } ? ? ?/** ? ? * 列出所有存储桶名称 ? ? * ? ? * @return ? ? * @throws Exception ? ? */ ? ?public List<String> listBucketNames() ? ? ? ? ? ?throws Exception { ? ? ? ?List<Bucket> bucketList = listBuckets(); ? ? ? ?List<String> bucketListName = new ArrayList<>(); ? ? ? ?for (Bucket bucket : bucketList) { ? ? ? ? ? ?bucketListName.add(bucket.name()); ? ? ? } ? ? ? ?return bucketListName; ? } ? ? ?/** ? ? * 查看所有桶 ? ? * ? ? * @return ? ? * @throws Exception ? ? */ ? ?public List<Bucket> listBuckets() ? ? ? ? ? ?throws Exception { ? ? ? ?return minioClient.listBuckets(); ? } ? ? ?/** ? ? * 检查存储桶是否存在 ? ? * ? ? * @param bucketName ? ? * @return ? ? * @throws Exception ? ? */ ? ?public boolean bucketExists(String bucketName) throws Exception { ? ? ? ?boolean flag = minioClient.bucketExists(bucketName); ? ? ? ?if (flag) { ? ? ? ? ? ?return true; ? ? ? } ? ? ? ?return false; ? } ? ? ?/** ? ? * 创建存储桶 ? ? * ? ? * @param bucketName ? ? * @return ? ? * @throws Exception ? ? */ ? ?public boolean makeBucket(String bucketName) ? ? ? ? ? ?throws Exception { ? ? ? ?boolean flag = bucketExists(bucketName); ? ? ? ?if (!flag) { ? ? ? ? ? ?minioClient.makeBucket(bucketName); ? ? ? ? ? ?return true; ? ? ? } else { ? ? ? ? ? ?return false; ? ? ? } ? } ? ? ?/** ? ? * 删除桶 ? ? * ? ? * @param bucketName ? ? * @return ? ? * @throws Exception ? ? */ ? ?public boolean removeBucket(String bucketName) ? ? ? ? ? ?throws Exception { ? ? ? ?boolean flag = bucketExists(bucketName); ? ? ? ?if (flag) { ? ? ? ? ? ?Iterable<Result<Item>> myObjects = listObjects(bucketName); ? ? ? ? ? ?for (Result<Item> result : myObjects) { ? ? ? ? ? ? ? ?Item item = result.get(); ? ? ? ? ? ? ? ?// 有对象文件,则删除失败 ? ? ? ? ? ? ? ?if (item.size() > 0) { ? ? ? ? ? ? ? ? ? ?return false; ? ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? ? ? ?// 删除存储桶,注意,只有存储桶为空时才能删除成功。 ? ? ? ? ? ?minioClient.removeBucket(bucketName); ? ? ? ? ? ?flag = bucketExists(bucketName); ? ? ? ? ? ?if (!flag) { ? ? ? ? ? ? ? ?return true; ? ? ? ? ? } ? ? ? ? } ? ? ? ?return false; ? } ? ? ?/** ? ? * 列出存储桶中的所有对象 ? ? * ? ? * @param bucketName 存储桶名称 ? ? * @return ? ? * @throws Exception ? ? */ ? ?public Iterable<Result<Item>> listObjects(String bucketName) throws Exception { ? ? ? ?boolean flag = bucketExists(bucketName); ? ? ? ?if (flag) { ? ? ? ? ? ?return minioClient.listObjects(bucketName); ? ? ? } ? ? ? ?return null; ? } ? ? ?/** ? ? * 列出存储桶中的所有对象名称 ? ? * ? ? * @param bucketName 存储桶名称 ? ? * @return ? ? * @throws Exception ? ? */ ? ?public List<String> listObjectNames(String bucketName) throws Exception { ? ? ? ?List<String> listObjectNames = new ArrayList<>(); ? ? ? ?boolean flag = bucketExists(bucketName); ? ? ? ?if (flag) { ? ? ? ? ? ?Iterable<Result<Item>> myObjects = listObjects(bucketName); ? ? ? ? ? ?for (Result<Item> result : myObjects) { ? ? ? ? ? ? ? ?Item item = result.get(); ? ? ? ? ? ? ? ?listObjectNames.add(item.objectName()); ? ? ? ? ? } ? ? ? } ? ? ? ?return listObjectNames; ? } ? ? ?/** ? ? * 删除一个对象 ? ? * ? ? * @param bucketName 存储桶名称 ? ? * @param objectName 存储桶里的对象名称 ? ? * @throws Exception ? ? */ ? ?public boolean removeObject(String bucketName, String objectName) throws Exception { ? ? ? ?boolean flag = bucketExists(bucketName); ? ? ? ?if (flag) { ? ? ? ? ? ?List<String> objectList = listObjectNames(bucketName); ? ? ? ? ? ?for (String s : objectList) { ? ? ? ? ? ? ? ?if(s.equals(objectName)){ ? ? ? ? ? ? ? ? ? ?minioClient.removeObject(bucketName, objectName); ? ? ? ? ? ? ? ? ? ?return true; ? ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? } ? ? ? ?return false; ? } ? ? ?/** ? ? * 文件访问路径 ? ? * ? ? * @param bucketName 存储桶名称 ? ? * @param objectName 存储桶里的对象名称 ? ? * @return ? ? * @throws Exception ? ? */ ? ?public String getObjectUrl(String bucketName, String objectName) throws Exception { ? ? ? ?boolean flag = bucketExists(bucketName); ? ? ? ?String url = ""; ? ? ? ?if (flag) { ? ? ? ? ? ?url = minioClient.getObjectUrl(bucketName, objectName); ? ? ? } ? ? ? ?return url; ? } ? }

4. 编写测试类进行测试

package com.suwell.serv.ocr.controller; ? import com.suwell.serv.ocr ponent.MinioConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; ? import javax.servlet.http.HttpServletResponse; import java.util.List; ? @RestController @CrossOrigin @RequestMapping("/test") public class MinioController { ? ? ?@Autowired ? ?MinioConfig minioConfig; ? ? ?// 上传 ? ?@PostMapping("/upload") ? ?public Object upload(@RequestParam("file") MultipartFile multipartFile) throws Exception { ? ? ? ?return this.minioConfig.putObject(multipartFile); ? } ? ? ?// 下载文件 ? ?@GetMapping("/download") ? ?public void download(@RequestParam("fileName")String fileName, HttpServletResponse response) { ? ? ? ?this.minioConfig.download(fileName,response); ? } ? ? ?// 列出所有存储桶名称 ? ?@PostMapping("/list") ? ?public List<String> list() throws Exception { ? ? ? ?return this.minioConfig.listBucketNames(); ? } ? ? ?// 创建存储桶 ? ?@PostMapping("/createBucket") ? ?public boolean createBucket(String bucketName) throws Exception { ? ? ? ?return this.minioConfig.makeBucket(bucketName); ? } ? ? ?// 删除存储桶 ? ?@PostMapping("/deleteBucket") ? ?public boolean deleteBucket(String bucketName) throws Exception { ? ? ? ?return this.minioConfig.removeBucket(bucketName); ? } ? ? ?// 列出存储桶中的所有对象名称 ? ?@PostMapping("/listObjectNames") ? ?public List<String> listObjectNames(String bucketName) throws Exception { ? ? ? ?return this.minioConfig.listObjectNames(bucketName); ? } ? ? ?// 删除一个对象 ? ?@PostMapping("/removeObject") ? ?public boolean removeObject(String bucketName, String objectName) throws Exception { ? ? ? ?return this.minioConfig.removeObject(bucketName, objectName); ? } ? ? ?// 文件访问路径 ? ?@PostMapping("/getObjectUrl") ? ?public String getObjectUrl(String bucketName, String objectName) throws Exception { ? ? ? ?return this.minioConfig.getObjectUrl(bucketName, objectName); ? } }

5. 使用postman进行本地测试

6. 最后查看public桶中是否有刚才上传的文件就可以了。如果有则表明你的项目已经成功集成minio了。

以上就是springboot项目如何集成minio的全部内容了,如果对你有帮助我会感到非常荣幸。

标签:

SpringBoot项目集成MinIO由讯客互联创业栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“SpringBoot项目集成MinIO