Spring boot integrates Minio file storage


The company's development framework integrates local storage of accessories, Alibaba cloud, Huawei cloud, etc. the current project requires that the accessory storage and application deployment environment cannot be the same server or use cloud storage. After technical selection, it is decided to integrate minio into the framework, deploy minio on another server, and open an external network port to solve the problem

Minio installation deployment

Download the minio installation deployment package and create the corresponding configuration file. An integrated compressed package is provided here

Download address: https://download.csdn.net/dow...

Create the minioData folder as the file storage path, extract the installation package, and modify the corresponding configuration file according to the placement path

minio-service.xml and run.bat

  <name>MinIO Service</name>
  <description>MinIO is a High Performance Object Storage</description>
  <log mode="roll-by-size">
set MINIO_SECRET_KEY=abcd@1234
minio.exe server -address :9999 D:\minioData

After decompressing the deployment package, cmd enters the corresponding decompression path, enters the command minio.exe server D:\minioData, and closes the cmd command after initialization

Use the service installation tool to install the service, and select minio-service.exe

Download address of windows service installation tool: https://download.csdn.net/dow...

Access after starting the service

User name: admin Password: abcd@1234 (the port and account password are configured in the run.bat file)

Enter the system and create a bucket to store files (similar to Alibaba cloud)

Configuring pom files


Configuration yml file

The upload / download addresses configured here are formal items. After the external network port is configured, the server cannot access the corresponding external network port. Upload to the internal network and download to the external network

  #minio configuration
  # Upload address
  # Download address
  # account
  minio_accesskey: admin
  # password
  minio_secrectkey: abcd@1234
  # Storage folder
  minio_bucknetname: xxx

Minio tool class

Initialize client

 public MinioClient InitMinio() {
        MinioClient minioClient = MinioClient.builder().
        endpoint(frameConfig.getMinio_uploadurl()).                       credentials(frameConfig.getMinio_accesskey(),frameConfig.getMinio_secrectkey()).build();
            boolean isExist = minioClient.bucketExists(frameConfig.getMinio_bucknetname());
            if (!isExist) {
        }catch (Exception e){
        return minioClient;

Upload file

The getkey method only specifies the corresponding custom storage path

Content type is specified so that pictures, pdf and other files can be browsed online when the browser can open the attachment

Do not specify that the default is stream. Open files for download

 public boolean uploadMinioFile(InputStream stream, AttachmentDO attachmentDO,String contentType){
        boolean result = true;
            MinioClient minioClient = InitMinio();
            String bucketName =frameConfig.getMinio_bucknetname();
            PutObjectOptions option = new PutObjectOptions(stream.available(), -1);
        }catch (Exception e){
            logger.error("Minio Failed to upload file:" + e.getMessage());
            result = false;
        return result;

Download File

It should be noted that the generated file download address is bound to the server address specified in MinioClient and is invalid after using nginx agent

    public  String readMinioCommonFile(AttachmentDO attachmentDO){
        String fileurl = "";
            MinioClient minioClient = InitMinio();
            String bucketName =frameConfig.getMinio_bucknetname();
            return minioClient.presignedGetObject(bucketName, getKey(attachmentDO));
        }catch (Exception e){
            logger.error("Minio fail to read file:" + e.getMessage());
        return fileurl;

Delete file

    public  boolean deleteMinioFile(AttachmentDO attachmentDO){
        boolean result = true;
            MinioClient minioClient = InitMinio();
            String bucketName =frameConfig.getMinio_bucknetname();
        }catch (Exception e){
            logger.error("Minio Failed to delete file:" + e.getMessage());
            result = false;
        return result;

Tags: Spring Boot MinIO

Posted on Sat, 20 Nov 2021 05:59:44 -0500 by Patty