spring boot 整合MinIO

pom.xml

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

配置类

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {

    /**
     * minio 地址
     */
    private String url;

    /**
     * minio accessKey
     */
    private String accessKey;

    /**
     * minio secretKey
     */
    private String secretKey;

    /**
     * 桶
     */
    private String bucket;

    /**
     * 文件访问地址(nginx地址)
     */
    private String webUrl;
}

config类

@Configuration
@ConditionalOnProperty(name = "file.type", havingValue = "minio")
public class MinioConfig {

    @Autowired
    private MinioProperties minioProperties;

    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder().endpoint(minioProperties.getUrl())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey()).build();
    }

    @Bean
    public MinioUtil minioUtil(MinioProperties minioProperties, MinioClient minioClient) {
        return new MinioUtil(minioProperties, minioClient);
    }

}

yaml配置文件

#fastDFS配置
file:
  type: minio

minio:
  url: http://xxx:9000
  access-key: minioadmin
  secret-key: minioadmin
  bucket: test
  web-url: http://xxx

工具类

public class MinioUtil {

    public static final int DEFAULT_MULTIPART_SIZE = 10 * 1024 * 1024;

    private MinioProperties minioProperties;

    private MinioClient minioClient;

    public MinioUtil(MinioProperties minioProperties, MinioClient minioClient) {
        this.minioProperties = minioProperties;
        this.minioClient = minioClient;
    }

    public MinioProperties getMinioProperties() {
        return minioProperties;
    }

    /**
     * 创建桶
     *
     * @param bucket
     * @throws Exception
     */
    public void createBucket(String bucket) throws Exception {
        boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if (!found) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
        }
    }

    /**
     * 查看所有桶
     *
     * @return
     * @throws Exception
     */
    public List<String> getBuckets() throws Exception {
        return minioClient.listBuckets().stream().map(Bucket::name).collect(Collectors.toList());
    }

    /**
     * 删除桶
     *
     * @param bucket
     * @throws Exception
     */
    public void deleteBucket(String bucket) throws Exception {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     * @throws Exception
     */
    public String uploadFile(MultipartFile file) throws Exception {
        String filePath = DateUtil.today() + "/" + file.getOriginalFilename();
        minioClient.putObject(PutObjectArgs.builder()
                .bucket(minioProperties.getBucket())
                .object(filePath)
                .stream(file.getInputStream(), -1, DEFAULT_MULTIPART_SIZE)
                .build());
        return filePath;
    }

    /**
     * 下载文件
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    public InputStream downloadFile(String fileName) throws Exception {
        InputStream stream = minioClient.getObject(GetObjectArgs.builder()
                .bucket(minioProperties.getBucket())
                .object(fileName).build());
        return stream;
    }

    /**
     * 删除文件
     *
     * @param fileName
     * @throws Exception
     */
    public void removeFile(String fileName) throws Exception {
        minioClient.removeObject(RemoveObjectArgs.builder()
                .bucket(minioProperties.getBucket())
                .object(fileName).build());
    }
}

使用demo

/**
 * 上传文件
 *
 * @param file
 * @param fileInfo
 */
private void uploadFile(MultipartFile file, FileInfo fileInfo) {
    try {
        String filePath = minioUtil.uploadFile(file);
        fileInfo.setUrl(minioUtil.getMinioProperties().getWebUrl() + "/" + filePath);
        fileInfo.setPath(filePath);
    } catch (Exception e) {
        log.error("uploadFile [{}]",e.getMessage());
    }
}

/**
 * 下载文件
 *
 * @param file
 * @param fileInfo
 */
public void downLoad(String fileName, String filePath, HttpServletResponse response) {
    if (StrUtil.isNotBlank(fileName) && StrUtil.isNotBlank(filePath)) {
        try {
            InputStream inputStream = minioUtil.downloadFile(filePath);
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.setCharacterEncoding("UTF-8");
            IoUtil.copy(inputStream,response.getOutputStream());
        } catch (Exception e) {
            log.error("downLoad [{}]",e.getMessage());
        }
    }
}


/**
 * 删除文件
 *
 * @param file
 * @param fileInfo
 */
public void deleteFile(String id) {
    try {
        FileInfo fileInfo = getById(id);
        if (fileInfo != null) {
            removeById(fileInfo.getId());
            minioUtil.removeFile(fileInfo.getPath());
        }
    } catch (Exception e) {
        log.error("deleteFile [{}]",e.getMessage());
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值