其他分享
首页 > 其他分享> > SpringBoot集成Minio文件服务器实现文件上传

SpringBoot集成Minio文件服务器实现文件上传

作者:互联网

1.本文章用docker启动  首先拉取minio镜像  命令:

docker load -i /home/tools/minio.tar
2.启动minion文件服务器  命令:

docker run -p 9000:9000 -d --name minio -e "MINIO_ACCESS_KEY=admin" -e "MINIO_SECRET_KEY=admin123" -v /opt/services/minio/data:/data -v /opt/services/minio/config:/root/.minio docker.io/minio/minio:RELEASE.2020-10-12T21-53-21Z server /data
 

3.查看minio是否启动成功  命令:docker ps

4. 登录minio服务器  输入步骤2中设置的用户名 密码:

 5.搭建springBoot项目工程  添加maven依赖:

<!-- minio客户端 -->
<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>7.1.0</version>
</dependency>

6.编写minio工具类:

package com.example;

import io.minio.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class MinioService
{

    @Value("${minio.url}")
    private String url;

    @Value("${minio.access-key}")
    private String accessKey;

    @Value("${minio.secret-key}")
    private String secretKey;



    /**
     * 初始化连接
     * @return
     * @throws Exception
     */
    public MinioClient getMinioClient(){
        return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
    }



    /**
     * 根据存储桶的名称判断桶是否存在,不存在就创建
     * @param bucketName
     * @throws Exception
     */
    public void isExistBucketName(String bucketName) throws Exception {
        boolean isExist =  getMinioClient().bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if(!isExist) {
            getMinioClient().makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            log.info(bucketName+"创建成功");
        }
    }

    /**
     * Minio文本地文件上传
     * @param filePath 完整路径文件名
     * @param fileName 修饰过的文件名 非源文件名
     * @return
     */
    public Results localMinioUpload(String filePath, String fileName,String bucketName) {
        try {
            //判断存储桶是否存在,不存在则创建
            isExistBucketName(bucketName);
            // minio仓库名
            File  file = new File(filePath);
            InputStream is = new FileInputStream(file);
            String contentType = new MimetypesFileTypeMap().getContentType(file);
            System.out.println(contentType);
            getMinioClient().putObject(
               PutObjectArgs.builder()
                         .bucket(bucketName)
                         .object(fileName)
                         .stream(is,
                                    file.length(),
                                    -1)
                         .contentType(contentType)
                         .build());
            log.info("成功上传文件 " + fileName + " 至 " + bucketName);
            return Results.success(System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
            return Results.requestFailMsg(System.currentTimeMillis(),"上传存储服务器出错异常");
        }
    }


    /**
     * Minio远程文件上传 上传对象是MultipartFile
     * @param file 文件实体
     * @param fileName 修饰过的文件名 非源文件名
     * @return
     */
    public Results minioUpload(MultipartFile file, String fileName, String bucketName) {
        try {
            //判断存储桶是否存在,不存在则创建
            isExistBucketName(bucketName);
            // fileName为空,说明要使用源文件名上传
            if (fileName == null) {
                fileName = file.getOriginalFilename();
                fileName = fileName.replaceAll(" ", "_");
            }
            getMinioClient().putObject(
               PutObjectArgs.builder()
                         .bucket(bucketName)
                         .object(fileName)
                         .stream(file.getInputStream(),
                                    file.getSize(),
                                    -1)
                         .contentType(file.getContentType())
                         .build());
            log.info("成功上传文件 " + fileName + " 至 " + bucketName);
//            String fileUrl = bucketName + "/" + fileName;
//            Map<String, Object> map = new HashMap<String, Object>();
//            map.put("fileUrl", fileUrl);
//            map.put("bucketName", bucketName);
//            map.put("originFileName", fileName);
            return Results.success(System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
            return Results.requestFailMsg(System.currentTimeMillis(),"上传存储服务器出错异常");
        }
    }

    /**
     * 判断文件是否存在
     * @param fileName   文件全路径
     * @param bucketName   存储桶名称
     * @return
     */
    public boolean isFileExisted(String fileName,String bucketName) {
        InputStream inputStream = null;
        try {
            inputStream = getMinioClient().getObject(
               GetObjectArgs.builder()
                         .bucket(bucketName)
                         .object(fileName)
                         .build());
            if (inputStream != null) {
                return true;
            }
        } catch (Exception e) {
            return false;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 删除文件
     * @param bucketName 桶名(文件夹)
     * @param fileName 文件名
     * @return
     */
    public boolean delete(String bucketName,String fileName) {
        try {
            getMinioClient().removeObject(
               RemoveObjectArgs.builder().bucket(bucketName).object(fileName).build());
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
    }


    /**
     * 获取文件的外链访问URL,并设置URL访问有效期
     * @param fileName
     * @return
     * @throws Exception
     */
    public String  getShowUtrl (String fileName,int expires,String bucketName) throws Exception{
        String tempUrl = getMinioClient().presignedGetObject(bucketName, fileName, expires);
        return tempUrl;
    }

    /**
     * 下载文件
     * @param objectName 文件名
     * @param response
     * @return
     */
    public Results downloadFile(String objectName, HttpServletResponse response, String bucketName) {
        try {
            InputStream stream = getMinioClient().getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
            String filename = new String(objectName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            ServletOutputStream servletOutputStream = response.getOutputStream();
            int len;
            byte[] buffer = new byte[1024];
            while((len=stream.read(buffer)) > 0){
                servletOutputStream.write(buffer, 0, len);
            }
            servletOutputStream.flush();
            stream.close();
            servletOutputStream.close();
            return Results.success(System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
            return Results.requestFailMsg(System.currentTimeMillis(),"下载报错异常!");
        }
    }



    public static void main(String[] args) throws Exception {
        MinioService ms = new MinioService();
        String strUrl = "D:/photo/1.jpg";
        ms.localMinioUpload(strUrl,"D:/photo/1.jpg","test");
    }

}
7.编写测试TestController层  
/**
 * @author 何志鹏
 * @version 1.0
 * @date 2021/10/28 17:08
 */
@RestController
@RequestMapping("/minio")
public class TestController
{

   @Value("${minio.bucket-name}")
   private String bucketName;

   @Autowired
   private MinioService minioService;

   @PostMapping("/minioUpload")
   public  Results minioUpload(MultipartFile file){
      String fileName = file.getOriginalFilename();
      return minioService.minioUpload(file, fileName, bucketName);
   }
}

8.通过postman进行文件上传测试

9.然后去minio文件服务器上查看刚才上传的文件

 

 10.最后附上码云gitee地址:https://gitee.com/hezhipeng_ek/minio.git   项目中有minio的docker镜像

标签:Minio,文件,return,String,minio,fileName,bucketName,file,SpringBoot
来源: https://blog.csdn.net/weixin_39643007/article/details/121019547