当前位置: 首页 > article >正文

SpringBoot整合Minio

SpringBoot整合Minio

1、添加依赖

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.5.12</version>
</dependency>

2、MinioConfig

在这里插入图片描述

@Configuration
public class MinioConfig {
  	// 这是从minio生成的密钥
    private static final String ACCESS_KEY = "pkRecBbz3DqdkLy6Ol6Z";
    private static final String SECRET_KEY = "ZK3OelmZYjlxxxxxlWKxn4MwxxxxxxxxxH3EY";
  
  	// 注意这里是9000端口,如果自已映射了其他的要换成对应的
    private static final String URL = "http://xxxx:9000";

    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder().endpoint(URL).credentials(ACCESS_KEY, SECRET_KEY).build();
    }
}

3、MinioUtil

这里的bucket是在minio里面创建的桶

@Component
public class MinioUtil {

    @Autowired
    private MinioClient minioClient;

    public void upload(InputStream inputStream, String filename, long size, String contentType) {
        try {
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket("public")
                    .object(filename)
                    .stream(inputStream, size, -1)
                    .contentType(contentType)
                    .build());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public InputStream download(String filename) {
        try {
            return minioClient.getObject(GetObjectArgs.builder()
                    .bucket("public")
                    .object(filename)
                    .build()
            );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void delete(String filename) {
        try {
            minioClient.removeObject(RemoveObjectArgs.builder()
                    .bucket("public")
                    .object(filename)
                    .build()
            );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String preview(String filename) {
        try {
            return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .method(Method.GET)
                    .bucket("public")
                    .object(filename)
                    .expiry(7, TimeUnit.DAYS)
                    .build()
            );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

4、OSSService

@Service
public class OSSService {

    @Autowired
    private MinioUtil minioUtil;

    public void upload(MultipartFile file) {
        try {
            minioUtil.upload(file.getInputStream(), file.getOriginalFilename(), file.getSize(), file.getContentType());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public InputStream download(String filename) {
        return minioUtil.download(filename);
    }

    public void delete(String filename) {
        minioUtil.delete(filename);
    }

    public String preview(String filename) {
        return minioUtil.preview(filename);
    }
}

5、OSSController

@RestController
@RequestMapping("/api/oss")
public class OSSController {

    @Autowired
    private OSSService ossService;

    @RequestMapping("/hello")
    public ResponseEntity<?> hello() {
        return ResponseEntity.Success("hello world!");
    }

    @PostMapping("/upload")
    public ResponseEntity<?> upload(@RequestParam("file") MultipartFile file) {
        ossService.upload(file);
        return ResponseEntity.Success("upload file success");
    }

    @RequestMapping("/download/{filename}")
    public void download(@PathVariable String filename, HttpServletResponse response) throws IOException {
        InputStream inputStream = ossService.download(filename);
        response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
        response.setContentType("application/octet-stream");
        IOUtils.copy(inputStream, response.getOutputStream());
    }

    @RequestMapping("/delete/{filename}")
    public ResponseEntity<?> delete(@PathVariable String filename) {
        ossService.delete(filename);
        return ResponseEntity.Success("delete file success");
    }

    @RequestMapping("/preview/{filename}")
    public ResponseEntity<?> preview(@PathVariable String filename) {
        String url = ossService.preview(filename);
        return ResponseEntity.Success(url);
    }
}

http://www.kler.cn/a/396167.html

相关文章:

  • Vue3中一级导航栏的吸顶导航交互以及Pinia优化重复请求
  • LeetCode654.最大二叉树
  • 服务器上安装Orcale数据库以及PL SQL工具(中文)
  • git命令提交项目
  • 服务jar包增加高斯数据库驱动jar包
  • FlinkPipelineComposer 详解
  • 第二十章 TCP 客户端 服务器通信 - 立即发送模式(Q 模式)
  • react的import 导入语句中的特殊符号
  • Cpolar 内网穿透使用
  • 人群计数制作私有数据集教程-----自用
  • 动力商城-03 Idea集成apifox Mybatis-Plus字段策略
  • 前端开发中的CSS框架:昔日辉煌与新兴潮流
  • 电脑不显示wifi列表怎么办?电脑不显示WiF列表的解决办法
  • sychronized锁
  • QT_CONFIG宏使用
  • 【扩散——BFS】
  • 用Python将Word文档转换为Markdown格式
  • 【PyTorch】PyTorch Geometric(PyG)安装指南:如何高效配置图神经网络环境
  • excel-VLOOKUP函数使用/XVLOOKUP使用
  • AUTOSAR_EXP_ARAComAPI的7章笔记(4)
  • 单片机智能家居火灾环境安全检测
  • 蓝桥杯每日真题 - 第14天
  • ubuntu20.04默认的python3.8升级到python3.10
  • 内网、公网(外网)划分
  • 从AI新手到高手:学习提示词,让智能助手更懂你
  • stm32F4 低功耗模式实例解析