• SpringBoot 整合MinIO


    引入依赖

     <dependency>
                <groupId>io.minio</groupId>
                <artifactId>minio</artifactId>
                <version>7.1.0</version>
            </dependency>

    也用到了这个 如果已经引入就不需要

     <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>

    配置文件yml

    minio:
      url: http://192.168.80.134:9000
      access-key: AKIAIOSFODNN7EXAMPLE
      secret-key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
      bucket-name: test

    MinioProperties.java

    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author 
     * @date 2021/06/25
     */
    @Data
    @Configuration
    @ConfigurationProperties(prefix = "minio")
    public class MinioProperties {
        /**
         * minio 服务地址 http://ip:port
         */
        private String url;
        /**
         * 用户名
         */
        private String accessKey;
        /**
         * 密码
         */
        private String secretKey;
        /**
         * 桶名称
         */
        private String bucketName;
    }

    工具类

    MinIoUtil.java

    import com.example.minio.config.MinioProperties;
    import io.minio.MinioClient;
    import io.minio.ObjectStat;
    import io.minio.PutObjectOptions;
    import io.minio.messages.Bucket;
    import lombok.SneakyThrows;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.tomcat.util.http.fileupload.IOUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.annotation.PostConstruct;
    import javax.servlet.http.HttpServletResponse;
    import java.io.InputStream;
    import java.net.URLEncoder;
    import java.util.List;
    
    /**
     *
     * Minio工具类
     */
    @Slf4j
    @Component
    public  class MinIoUtil {
    
        public static MinioClient minioClient;
    
        @Autowired
        private MinioProperties minioProperties;
    
        public static MinIoUtil minIoUtil;
    
        /**
         * 初始化minio配置
         */
        @PostConstruct
        public  void init() {
    
            minIoUtil=this;
            minIoUtil.minioProperties=this.minioProperties;
            try {
    
                minioClient = MinioClient.builder().endpoint(minioProperties.getUrl()).credentials(minioProperties.getAccessKey(),minioProperties.getSecretKey()).build();
                createBucket(minioProperties.getBucketName());
                log.info(">>>>>>>>>>>minio 初始化成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("》》》》》》》》》》初始化minio异常: 【{}】", e.fillInStackTrace());
            }
    
    
        }
    
        /**
         * 判断 bucket是否存在
         *
         * @param bucketName 桶名
         * @return: boolean
         */
        @SneakyThrows(Exception.class)
        public static boolean bucketExists(String bucketName) {
            return minioClient.bucketExists(bucketName);
        }
    
        /**
         * 创建 bucket
         * @param bucketName 桶名
         * @return: void
         */
        @SneakyThrows(Exception.class)
        public static void createBucket(String bucketName) {
            boolean isExist = minioClient.bucketExists(bucketName);
            if (!isExist) {
                minioClient.makeBucket(bucketName);
            }
        }
    
        /**
         *
         * 获取全部bucket
         * @return: java.util.List<io.minio.messages.Bucket>
         */
        @SneakyThrows(Exception.class)
        public static List<Bucket> getAllBuckets() {
            return minioClient.listBuckets();
        }
    
        /**
         * 文件上传
         * @param bucketName 桶名
         * @param fileName 文件名
         * @param filePath 文件路径
         */
        @SneakyThrows(Exception.class)
        public static void upload(String bucketName, String fileName, String filePath) {
            minioClient.putObject(bucketName, fileName, filePath, null);
        }
    
        /**
         * 文件上传(返回URL下载地址)
         * @param bucketName 桶名
         * @param fileName 文件名
         * @param stream 文件流
         * @return: 文件url下载地址
         */
        @SneakyThrows(Exception.class)
        public static String upload(String bucketName, String fileName, InputStream stream) {
            minioClient.putObject(bucketName, fileName, stream, new PutObjectOptions(stream.available(), -1));
            return getFileUrl(bucketName, fileName);
        }
    
        /**
         * 文件上传 (返回URL下载地址)
         * @param bucketName 桶名
         * @param file 文件
         * @return: 文件url下载地址
         */
        @SneakyThrows(Exception.class)
        public static String upload(String bucketName, MultipartFile file) {
            final InputStream is = file.getInputStream();
            final String fileName = file.getOriginalFilename();
            minioClient.putObject(bucketName, fileName, is, new PutObjectOptions(is.available(), -1));
            is.close();
            return getFileUrl(bucketName, fileName);
        }
    
        /**
         * 删除文件
         * @param bucketName 桶名
         * @param fileName 文件名
         */
        @SneakyThrows(Exception.class)
        public static void deleteFile(String bucketName, String fileName) {
            minioClient.removeObject(bucketName, fileName);
        }
    
        /**
         * 下载文件 (流输出)
         * @param bucketName  桶名
         * @param fileName 文件名
         */
        @SneakyThrows(Exception.class)
        public static void download(String bucketName, String fileName, HttpServletResponse response) {
            // 获取对象的元数据
            final ObjectStat stat = minioClient.statObject(bucketName, fileName);
            response.setContentType(stat.contentType());
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            InputStream is = minioClient.getObject(bucketName, fileName);
            IOUtils.copy(is, response.getOutputStream());
            is.close();
        }
    
        /**
         *
         * 获取minio文件的下载地址
         * @param bucketName 桶名
         * @param fileName  文件名
         */
        @SneakyThrows(Exception.class)
        public static String getFileUrl(String bucketName, String fileName) {
            return minioClient.presignedGetObject(bucketName, fileName);
        }
    
    
    }

    使用直接调用工具类的方法即可 例如

        @PostMapping(value = "/upload")
        public String upload(@RequestParam(value = "file") MultipartFile file){
            String upload = MinIoUtil.upload("test", file);
            return upload;
        }
    -----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------ (蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
  • 相关阅读:
    链表(1) --- 单链表
    .Net性能调优-MemoryPool
    .Net性能调优-WeakReference
    .Net性能调优-ArrayPool
    .Net Core 中的选项Options
    .Net Core配置Configuration源码研究
    .Net中异步任务的取消和监控
    nginx 实现jira反向代理
    k8s-harbor拉取镜像权限问题-imagePullSecrets
    Studio 3T 重置试用日期
  • 原文地址:https://www.cnblogs.com/pxblog/p/14930288.html
Copyright © 2020-2023  润新知