美文网首页
Springboot中使用minio

Springboot中使用minio

作者: 三生浮屠 | 来源:发表于2021-11-12 17:54 被阅读0次

    工作需求需要在项目中使用minio,项目使用springboot,如下操作:

    • 引入依赖:
    <dependency>
                <groupId>io.minio</groupId>
                <artifactId>minio</artifactId>
                <version>8.2.1</version>
    </dependency>
    
    • 配置文件
    minio:
      minioUrl: http://192.168.56.201:9999/
      accessKey: root
      secretKey: root1234
    
    • 获取配置参数并注入MinioClient 对象
    @Data
    @Component
    @ConfigurationProperties(prefix = "minio")
    @Log4j2
    public class MinioConfig {
        private String minioUrl;
        private String accessKey;
        private String secretKey;
    
        @Bean
        public MinioClient getMinioClient(){
            log.info("初始化MinioClient客户端:minioUrl:" + minioUrl + ",accessKey:" + accessKey + ",secretKey:" + secretKey);
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(minioUrl)
                    .credentials(accessKey,secretKey)
                    .build();
            return minioClient;
        }
    }
    
    • 在工具类中使用
    @Log4j2
    @Component
    public class FileServerOptUtil {
    
        @Autowired
        MinioClient minioClient;
    
        /**
         * 获取桶中某个对象的输入流
         * @param bucketName
         * @param path
         * @return
         */
        public  InputStream getObjectInputStream(String bucketName,String path){
            log.info("从桶:" + bucketName + ",获取:" + path + "对象流!");
            InputStream stream = null;
            try {
                stream = minioClient.getObject(
                                GetObjectArgs.builder().bucket(bucketName).object(path).build());
            } catch (Exception e) {
                log.error("从桶:" + bucketName + ",获取:" + path + "对象流错误!");
                e.printStackTrace();
            }
            return stream;
        }
    
        /**
         *
         * @param bucketName
         * @param dirPath 如:path/to/
         * @return
         */
        public  boolean mkdir(String bucketName,String dirPath){
            log.info("在桶:" + bucketName + ",中创建:" + dirPath);
            boolean result = false;
            try {
                minioClient.putObject(
                        PutObjectArgs.builder().bucket(bucketName).object(dirPath).stream(
                                new ByteArrayInputStream(new byte[] {}), 0, -1)
                                .build());
                result = true;
            } catch (Exception e) {
                log.error("在桶:" + bucketName + ",中创建:" + dirPath + "错误!");
                e.printStackTrace();
            }
            return result;
        }
    
    
        /**
         * 往对象存储服务目标桶的目标位置上传文件
         * @param bucketName 桶
         * @param toPath  目标位置如:test/2.txt
         * @param fromPath 本地文件位置
         * @return
         */
        public  boolean uploadObject(String bucketName,String toPath,String fromPath ){
            log.info("从本地:" + fromPath + "往桶:" + bucketName + ",中上传文件:" + toPath);
            boolean result = false;
            try {
                minioClient.uploadObject(
                        UploadObjectArgs.builder()
                                .bucket(bucketName)
                                .object(toPath)
                                .filename(fromPath)
                                .build());
                result = true;
            } catch (Exception e) {
                log.error("从本地:" + fromPath + "往桶:" + bucketName + ",中上传文件:" + toPath + "错误!");
                e.printStackTrace();
            }
            return result;
        }
    
    
        /**
         * 从桶中下载文件到本地文件
         * @param bucketName
         * @param fromPath
         * @param toPath
         * @return
         */
        public  boolean downloadFile(String bucketName,String fromPath,String toPath){
            log.info("从桶:" + bucketName + "中将" + fromPath + "文件下载到:" + toPath);
            boolean result = false;
            try {
                minioClient.downloadObject(
                        DownloadObjectArgs.builder()
                                .bucket(bucketName)
                                .object(fromPath)
                                .filename(toPath)
                                .build());
                result = true;
            } catch (Exception e) {
                log.error("从桶:" + bucketName + "中将" + fromPath + "文件下载到:" + toPath + "错误!");
                e.printStackTrace();
            }
            return result;
        }
    }
    

    相关文章

      网友评论

          本文标题:Springboot中使用minio

          本文链接:https://www.haomeiwen.com/subject/coeozltx.html