minio安装及使用

文章目录

  • 前言
  • minio安装
    • 下载并赋予读写权限
    • 执行
    • 新建文件目录
    • 启动
    • 查看日志
  • minio使用(java+spring boot使用)
    • pom
    • 配置文件
    • config类
    • controller类

前言

前几天弄一个报告文件生成和下载的功能,由于是镜像部署的程序而且用了k8s的多节点部署,因此需要一个共享文件存储的功能,上网查了一下最后准备用minio

minio安装

下载并赋予读写权限

mkdir minio
wget https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x minio

执行

./minio

新建文件目录

mkdir data

启动

./minio server --address 0.0.0.0:9006 --console-address 0.0.0.0:9007 data > minio.log 2>&1 &

查看日志

tail -f minio.log

minio使用(java+spring boot使用)

pom

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

配置文件

minio.access-key=your-access-key
minio.secret-key=your-secret-key
minio.endpoint=http://your-minio-endpoint

config类

import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MinioConfig {
    @Value("${minio.access-key}")
    private String accessKey;

    @Value("${minio.secret-key}")
    private String secretKey;

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

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

controller类

import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

@RestController
@RequestMapping("/files")
public class FileController {
    @Autowired
    private MinioClient minioClient;

    @PostMapping
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        // 上传文件到MinIO
        minioClient.putObject(PutObjectArgs.builder()
                .bucket("your-bucket-name")
                .object(file.getOriginalFilename())
                .contentType(file.getContentType())
                .stream(file.getInputStream(), file.getSize(), -1)
                .build());

        return ResponseEntity.status(HttpStatus.CREATED).body("File uploaded successfully");
    }

    @GetMapping("/{filename}")
    public void downloadFile(@PathVariable("filename") String filename, HttpServletResponse response) throws Exception {
        // 从MinIO下载文件
        InputStream inputStream = minioClient.getObject("your-bucket-name", filename);
        ObjectStat objectStat = minioClient.statObject("your-bucket-name", filename);

        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(filename, "UTF-8") + "\"");
        response.setContentLength(objectStat.size());

        StreamUtils.copy(inputStream, response.getOutputStream());
        response.flushBuffer();
    }
}

你可能感兴趣的:(java)