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

MinIO的安装、简单使用、分片上传,第1张

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

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

启动minio

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

.minio.exe server E:minio

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

SpringBoot整合Minio

    io.minio
    minio
    8.0.3

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;

@Configuration
public class MinIoClientConfig {

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

    
    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }
}
package com.example.utils;


@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;


@RestController
public class UploadController {

    @Resource
    private MinioUtil minioUtil;

    
    @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 "文件上传异常";
        }
    }

    
    @GetMapping("/download")
    public void download(@RequestParam("filename") String filename, HttpServletResponse res) {
        minioUtil.downloadFile(res, filename);
    }
}
分片上传

分片上传去看这个demo 里面有代码仓库地址 https://blog.csdn.net/Tuine/article/details/113996848

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

完整demo https://gitee.com/mental-patient-zhu-xx/minio

参考链接:

分片 https://blog.csdn.net/Tuine/article/details/113996848

工具类 https://blog.csdn.net/weixin_42170236/article/details/109356921

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存