MinIO的安装、简单使用、分片上传

MinIO的安装使用

下载

直接去官网下载: https://docs.min.io/docs/minio-quickstart-guide.html

image-20220109181226493

启动minio

进入 minio.exe 所在文件夹,地址栏输入cmd打开CMD窗口,输入下面的命令

.\minio.exe server E:\minio

image-20220109181345810

image-20220109181831885

浏览器输入 http://127.0.0.1:9000/,Access Key 和 Secret Key 在命令行那有显示,都是 minioadmin

image-20220109182105332

SpringBoot整合Minio

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.0.3</version>
</dependency>
server:
  port: 10002
spring:
  servlet:
    multipart:
      max-file-size: 100MB
      max-request-size: 100MB

# minio
minio:
  endpoint: http://localhost:9000
  accessKey: minioadmin
  secretKey: minioadmin
  bucketName: file
package com.example.config;
/**
 * @author: zhl
 * @Time: 2022/1/9  15:49
 */
@Configuration
public class MinIoClientConfig {

    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;

    /**
     * 注入minio 客户端
     *
     * @return
     */
    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }
}
package com.example.utils;

/**
 * @author: zhl
 * @Time: 2022/1/9  16:16
 */
@Component
@Slf4j
public class MinioUtil {
    @Resource
    private MinioClient minioClient;
    @Resource
    private CustomMinioClient customMinioClient;

    @Value("${minio.bucketName}")
    private String bucketName;

    /**
     * 上传文件
     */
    public FileDTO uploadFile(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        // 上传文件路径
        String rename = RenameUtil.generateFileName(fileName);
        String objectName = RenameUtil.getFilePath(rename);
        PutObjectArgs objectArgs = PutObjectArgs.builder().object(objectName)
                .bucket(bucketName)
                .contentType(file.getContentType())
                .stream(file.getInputStream(), file.getSize(), -1).build();
        minioClient.putObject(objectArgs);
        return FileDTO.builder().bucketName(bucketName).objectName(objectName).originalFilename(fileName).build();
    }
    /**
     * 直接下载文件
     */
    public void downloadFile(HttpServletResponse res, String fileName) {
        GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName)
                .object(fileName).build();
        OutputStream outputStream = null;
        try (GetObjectResponse response = minioClient.getObject(objectArgs)) {
            outputStream = res.getOutputStream();
            StreamUtils.copy(response, outputStream);
            res.setCharacterEncoding("utf-8");
            res.setContentType("application/force-download");// 设置强制下载不打开
            res.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(outputStream);
        }
    }
}
package com.example.controller;

/**
 * @author: zhl
 * @Time: 2022/1/9  15:50
 */
@RestController
public class UploadController {

    @Resource
    private MinioUtil minioUtil;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public String upload(MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            return "文件不能为null";
        }
        try {
            FileDTO fileDTO = minioUtil.uploadFile(file);
            return JSONUtil.toJsonStr(fileDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return "文件上传异常";
        }
    }

    /**
     * 下载文件
     *
     * @param filename
     */
    @GetMapping("/download")
    public void download(@RequestParam("filename") String filename, HttpServletResponse res) {
        minioUtil.downloadFile(res, filename);
    }
}

分片上传

分片上传去看这个demo 里面有代码仓库地址 https://github.com/WinterChenS/minio-multipart

如果你minio版本是3.0.X,应该是不支持分片上传的(反正我没有找到)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值