SpringBoot2 integrates MinIO middleware to realize convenient file management

1, Introduction to MinIO

1. Basic description

MinIO is an open source object storage service. It is suitable for storing large capacity unstructured data, such as pictures, videos, log files, backup data and container / virtual machine images. An object file can be of any size, ranging from a few kb to a maximum of 5T.

MinIO is a very lightweight service that can be easily combined with other applications, such as NodeJS, Redis or MySQL.

2. Storage mechanism

MinIO protects data with embedded erasure encoding by object, which is written in assembly code and provides the highest performance. MinIO uses Reed Solomon code to divide objects into n/2 data and n/2 parity blocks - although they can be configured to any desired level of redundancy. This means that in 12 drive settings, an object is partitioned into 6 data and 6 parity blocks. Even if up to 5 ((n/2) – 1) drives (whether parity or data) are lost, data can be reliably reconstructed from the remaining drives. The implementation of MinIO ensures that objects can be read or new objects can be written even if multiple devices are lost or unavailable. Finally, MinIO's erase code is at the object level and can repair one object at a time.

2, MinIO environment construction

1. Installation package download

https://dl.min.io/server/minio/release/linux-amd64/minio

It is recommended to use a mine to download. The speed will be faster. Upload the download package to the / opt/minioconfig/run directory.

2. Create data store directory

mkdir -p /data/minio/data

3. Service startup

Start and specify the data storage address

/opt/minioconfig/run/minio server /data/minio/data/

Output log

Endpoint:  http://localhost:9000  http://127.0.0.1:9000
AccessKey: minioadmin
SecretKey: minioadmin

//java learning exchange: 737251827

Here is the login address and account password.

3, Integrate SpringBoot environment

1. Basic dependence

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>3.0.12</version>
</dependency>

2. Basic configuration

Configuration elements: address and port, login name, password, HTML bucket, picture bucket.

minio:  endpoint: http://192.168.72.133:9000
accessKey: minioadmin
secretKey: minioadmin
bucketNameHtml: html
bucketNameImage: image

After the file is uploaded, it can be accessed directly based on the file address, but the read-write permission of the file needs to be configured in MinIO:

3. Configuration parameter class

@Component
@ConfigurationProperties(prefix = "minio")
public class ParamConfig {

    private String endpoint ;
    private String accessKey ;
    private String secretKey ;
    private String bucketNameHtml ;
    private String bucketNameImage ;
    // Omit the get and set methods
}

4. Configuration class based on MinIO

Encapsulate the MinIO client connection tool, the basic method of file upload, and return the URL address of the file on the MinIO service.

import io.minio.MinioClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
public class MinIOConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(MinIOConfig.class) ;

    @Resource
    private ParamConfig paramConfig ;

    private MinioClient minioClient ;

    /**
     * Initialize MinIO client
     */
    @PostConstruct
    private void init(){
        try {
            minioClient = new MinioClient(paramConfig.getEndpoint(),
                                          paramConfig.getAccessKey(),
                                          paramConfig.getSecretKey());
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("MinIoClient init fail ...");
        }
    }

    /**
     * Upload < HTML > page
     */
    public String uploadHtml (String fileName, String filePath) throws Exception {
        minioClient.putObject(paramConfig.getBucketNameHtml(),fileName,filePath);
        return paramConfig.getEndpoint()+"/"+paramConfig.getBucketNameHtml()+"/"+fileName ;
    }

    /**
     * Upload < img > pictures
     */
    public String uploadImg (String imgName, String imgPath) throws Exception {
        minioClient.putObject(paramConfig.getBucketNameImage(),imgName,imgPath);
        return paramConfig.getEndpoint()+"/"+paramConfig.getBucketNameImage()+"/"+imgName ;
    }
}

5. Service implementation

Two basic methods are provided: HTML and image upload, which are stored in different locations.

import com.minio.file.config.MinIOConfig;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class UploadServiceImpl implements UploadService {

    @Resource
    private MinIOConfig minIOConfig ;

    // Upload < HTML > and return the server address
    @Override
    public String uploadHtml(String fileName, String filePath) throws Exception {
        return minIOConfig.uploadHtml(fileName,filePath);
    }

    // Upload < img > and return the server address
    @Override
    public String uploadImg(String imgName, String imgPath) throws Exception {
        return minIOConfig.uploadImg(imgName,imgPath);
    }
}

After uploading, view the effect based on the url returned by the browser access interface:

Tags: Java Back-end Programmer

Posted on Tue, 09 Nov 2021 04:53:53 -0500 by Imperialdata