This article source: making here | | GitEE, click here

I. Introduction to MinIO

1. Basic description

MinIO is an open source object storage service. Suitable for storing large amounts of unstructured data, such as images, videos, log files, backup data, and container/VIRTUAL machine images. An object file can be any size, ranging from a few KB to a maximum of 5 TB.

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 using an embedded erasing code per object, written in assembly code for maximum performance. MinIO uses the 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 sharding an object into six data and six parity blocks in a 12-drive setup. Even if as many as five ((n/2) -1) drives (whether parity or data) are lost, data can still be reliably rebuilt from the remaining drives. MinIO’s implementation 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 fix one object at a time.

2. MinIO environment construction

1. Download the installation package

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

You are advised to use a lightning mine to download the software faster. Upload the downloaded package to the /opt/minioconfig/run directory.

2. Create a data store directory

mkdir -p /data/minio/data
Copy the code

3. Start the service

Start and specify the location to store the data

/opt/minioconfig/run/minio server /data/minio/data/
Copy the code

The output log

Endpoint:  http://localhost:9000  http://127.0.0.1:9000    
AccessKey: minioadmin 
SecretKey: minioadmin
Copy the code

Here is the login address and account password.

Third, integrate the SpringBoot environment

1. Base dependency

<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>
Copy the code

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
Copy the code

After the file is uploaded, you can directly access the file based on the file address. However, you need to configure read and write permissions for the file in MinIO:

3. Configure the 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
}
Copy the code

4, based on the MinIO configuration class

Encapsulates the MinIO client connection tool, the basic method of file uploading, and returns the URL 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 the MinIO client */
    @PostConstruct
    private void init(a){
        try {
            minioClient = new MinioClient(paramConfig.getEndpoint(),
                                          paramConfig.getAccessKey(),
                                          paramConfig.getSecretKey());
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("MinIoClient init fail ..."); }}/** * Upload the < HTML > page */
    public String uploadHtml (String fileName, String filePath) throws Exception {
        minioClient.putObject(paramConfig.getBucketNameHtml(),fileName,filePath);
        return paramConfig.getEndpoint()+"/"+paramConfig.getBucketNameHtml()+"/"+fileName ;
    }

    /** * upload  image */
    public String uploadImg (String imgName, String imgPath) throws Exception {
        minioClient.putObject(paramConfig.getBucketNameImage(),imgName,imgPath);
        return paramConfig.getEndpoint()+"/"+paramConfig.getBucketNameImage()+"/"+imgName ; }}Copy the code

5. Service implementation

Two basic methods are provided: HTML and image upload, 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  and return the server address
    @Override
    public String uploadImg(String imgName, String imgPath) throws Exception {
        returnminIOConfig.uploadImg(imgName,imgPath); }}Copy the code

After uploading, view the result based on the URL returned by the browser access interface:

Source code address

Making address GitEE, https://github.com/cicadasmile/middle-ware-parent, https://gitee.com/cicadasmile/middle-ware-parentCopy the code