Skip to content
On this page

geoviswtx-minio-starter

操作 MinIO 的 SpringBoot 自动配置包

示例代码

examples

1. 引入依赖

xml
<dependency>
    <groupId>com.geoviswtx</groupId>
    <artifactId>geoviswtx-minio-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

如果工程中没有 utils 包, 则需要单独引入

xml
<dependency>
    <groupId>com.geoviswtx</groupId>
    <artifactId>geoviswtx-common</artifactId>
    <scope>provided</scope>
</dependency>

2. 加入配置

yaml
geoviswtx:
  oss:
    minio:
      end-point: http://127.0.0.1:9000
      user: yourAccount
      password: yourPassword
      default-bucket: docs
      prefix:
        - /customFixedPrefix/
        - ^date^

3. 注入

java
private final MinIoService minIoService;

   public MinioApplication(MinIoService minIoService) {
      this.minIoService = minIoService;
   }

4. 使用

java
@Test
   void putTest() throws Exception {
      File file = new File("/codeStyle/LICENSE.txt");
      final String fileName = file.getName();

      try(FileInputStream in = new FileInputStream(file)) {
         final String path = minIoService.uploadFile(in, "test", fileName);

         log.info(path);
      }
   }

5. 全路径返回

后端存储只存了相对桶的路径,而前端获取到文件需全路径访问,为了尽量避免 MinIO 路径前缀在前端写死,可在接口返回前包装数据域并添加 MinIO 地址前缀. 代码示例如下:

java
@GetMapping("/test")
public OssResponseVo<Object> testResponse(long id) {
        // 通过 id 找到图片资源
        Object data = null; // xxEntity data = xxService.findById(id);

        return minIoService.buildResponseVo(data);
        }