maven *** 作minio 文件服务器

maven *** 作minio 文件服务器,第1张

maven *** 作minio 文件服务

  工作中用到了minio文件服务,记录下minio 文件简单使用。

1.添加maven依赖

    io.minio
    minio
    7.0.2
 2.配置信息
package com.bochao.cim.file.config;

import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;



@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    
    private String endpoint;
    
    private Integer port;
    
    private String accesskey;
    
    private String secretkey;
    
    private Boolean secure;
    
    private String bucketName;

    @Bean
    public MinioClient getMinioClient() {
        MinioClient minioClient = null;
        try {
            minioClient = new MinioClient(endpoint, port, accesskey, secretkey, secure);
            logger.info("Minio文件存储服务初始化完成");
        } catch (Exception e) {
            logger.error("Minio文件存储服务初始化异常" + e.getMessage());
            e.printStackTrace();
        }
        return minioClient;
    }

}
3.application.yml 配置文件服务连接信息
minio:
  endpoint: 127.0.0.1
  port: 9001
  accesskey: minioadmin
  secretkey: minioadmin
  secure: false #如果是true,则用的是https而不是http,默认值是true
  bucketName: test#默认存储桶
4.文件上传下载工具类
@EnableConfigurationProperties(MinioConfig.class)
@ConditionalOnBean(MinioConfig.class)
@Service
public class MinioStoreService  {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioConfig minioConfig;

    
    public boolean bucketExists(String bucketName) {
        try {
            return minioClient.bucketExists(bucketName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    
    public void makeBucket(String bucketName) {
        try {
            boolean isExist = minioClient.bucketExists(bucketName);
            if (!isExist) {
                minioClient.makeBucket(bucketName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    @Override
    public boolean existFile(String path) throws Exception {
        ObjectStat statObject = minioClient.statObject(minioConfig.getBucketName(), path);
        if (statObject != null && statObject.length() > 0) {
            return true;
        }
        return false;
    }

    
    @Override
    public Long uploadFile(String path, MultipartFile file) {
        try(InputStream inputStream = file.getInputStream()) {
            //检查存储桶是否已经存在
            boolean isExist = this.bucketExists(minioConfig.getBucketName());
            if (!isExist) {
                minioClient.makeBucket(minioConfig.getBucketName());
            }
            // PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
            //PutObjectOptions putObjectOptions = new PutObjectOptions(file.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
            PutObjectOptions putObjectOptions = new PutObjectOptions(file.getSize(), -1);
            //PutObjectOptions putObjectOptions = new PutObjectOptions(inputStream.available(), -1);
            // 文件的ContentType
            putObjectOptions.setContentType(file.getContentType());
            minioClient.putObject(minioConfig.getBucketName(), path, inputStream, putObjectOptions);
            return file.getSize();
        } catch (Exception e) {
            logger.error("Minio上传文件出错");
            e.printStackTrace();
        }
        return 0L;
    }

    
    @Override
    public Long uploadFile(String path, File file) {
        try (InputStream inputStream = new FileInputStream(file)) {
            //检查存储桶是否已经存在
            boolean isExist = this.bucketExists(minioConfig.getBucketName());
            if (!isExist) {
                minioClient.makeBucket(minioConfig.getBucketName());
            }
            minioClient.putObject(minioConfig.getBucketName(), path, inputStream, new PutObjectOptions(file.length(), -1));
            return file.length();
        } catch (Exception e) {
            logger.error("Minio上传文件出错");
            e.printStackTrace();
        }
        return 0L;
    }

    
    @Override
    public String mkdir() throws Exception {
        boolean b = this.bucketExists(minioConfig.getBucketName());
        if (!b) {
            this.makeBucket(minioConfig.getBucketName());
        }
        return minioConfig.getBucketName();
    }

    
    @Override
    public void downloadFile(String storePath, ServletOutputStream outputStream) throws Exception {
        boolean flag = this.existFile(storePath);
        if (!flag) {
            throw new MicroServiceException(ResultCode.RESULE_DATA_NONE.code(), "文件不存在");
        }
        try (InputStream inputStream = minioClient.getObject(minioConfig.getBucketName(), storePath)) {
            IOUtils.copy(inputStream, outputStream);
        } catch (Exception e) {
            logger.error("文件下载异常");
            e.printStackTrace();
        }
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5434141.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存