若依系统整合Minio

若依系统整合Minio

参考博文

实践

引入依赖

        <!-- minio 相关依赖 -->
        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>3.0.10</version>
        </dependency>

controller

在这里插入图片描述

    @Autowired
    private MinioUtils minioUtils;
    /**
     * 通用minio上传
     */
    @PostMapping("/minio/upload")
    public AjaxResult minioUploadFile(MultipartFile file){
    
    
        JSONObject res = null;
        AjaxResult ajax = AjaxResult.success();
        res = minioUtils.uploadFile(file,"material");
        if (Integer.parseInt(res.getString("code")) == 200){
    
    
            ajax.put("url", res.getString("data"));
        } else {
    
    
            return AjaxResult.error("上传出错");
        }
        return ajax;
    }

utils

在这里插入图片描述

package com.ruoyi.common.utils;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.bean.MinioProp;
import io.minio.MinioClient;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Component
public class MinioUtils {
    
    
    @Autowired
    private MinioClient client;
    @Autowired
    private MinioProp minioProp;
    /**
     * 创建bucket
     *
     * @param bucketName bucket名称
     */
    public void createBucket(String bucketName) {
    
    
        try {
    
    
            if(!client.bucketExists(bucketName)){
    
    
                client.makeBucket(bucketName);
            }
        } catch (InvalidBucketNameException e) {
    
    
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
    
    
            e.printStackTrace();
        } catch (InsufficientDataException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } catch (InvalidKeyException e) {
    
    
            e.printStackTrace();
        } catch (NoResponseException e) {
    
    
            e.printStackTrace();
        } catch (XmlPullParserException e) {
    
    
            e.printStackTrace();
        } catch (ErrorResponseException e) {
    
    
            e.printStackTrace();
        } catch (InternalException e) {
    
    
            e.printStackTrace();
        } catch (RegionConflictException e) {
    
    
            e.printStackTrace();
        }
    }
    /**
     * 上传文件
     *
     * @param file       文件
     * @param bucketName 存储桶
     * @return
     */
    public JSONObject uploadFile(MultipartFile file, String bucketName) {
    
    
        JSONObject res = new JSONObject();
        res.put("code",500);
        createBucket(bucketName);
        try {
    
    
            String originalFileName = file.getOriginalFilename();
            String fileName = bucketName + "_" + System.currentTimeMillis() + originalFileName.substring(originalFileName.lastIndexOf("."));
            client.putObject(bucketName,fileName,file.getInputStream(),file.getContentType());
            res.put("code",200);
            res.put("data",minioProp.getEndpoint() + "/" +bucketName + "/" + fileName);
            res.put("msg","上传成功");
            return res;
        } catch (InvalidBucketNameException e) {
    
    
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
    
    
            e.printStackTrace();
        } catch (InsufficientDataException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } catch (InvalidKeyException e) {
    
    
            e.printStackTrace();
        } catch (NoResponseException e) {
    
    
            e.printStackTrace();
        } catch (XmlPullParserException e) {
    
    
            e.printStackTrace();
        } catch (ErrorResponseException e) {
    
    
            e.printStackTrace();
        } catch (InternalException e) {
    
    
            e.printStackTrace();
        } catch (InvalidArgumentException e) {
    
    
            e.printStackTrace();
        }
        res.put("msg","上传失败");
        return res;
    }
}

minio配置

在这里插入图片描述
在这里插入图片描述

# minio配置
minio:
  endpoint: http://127.0.0.1:9000
  accesskey: minioadmin
  secretkey: minioadmin
package com.ruoyi.common.utils.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProp {
    
    
    private String endpoint;
    private String accesskey;
    private String secretkey;

    public String getEndpoint() {
    
    
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
    
    
        this.endpoint = endpoint;
    }

    public String getAccesskey() {
    
    
        return accesskey;
    }

    public void setAccesskey(String accesskey) {
    
    
        this.accesskey = accesskey;
    }

    public String getSecretkey() {
    
    
        return secretkey;
    }

    public void setSecretkey(String secretkey) {
    
    
        this.secretkey = secretkey;
    }
}

猜你喜欢

转载自blog.csdn.net/lzl980111/article/details/130024918
今日推荐