Use of MINIO in java

Posted by wendymelon on Sun, 16 Jan 2022 01:24:40 +0100

introduction

MINIO is the world's leading object storage pioneer, with millions of users all over the world On standard hardware, the read / write speeds are up to 183 GB / s and 171 GB / s. Object storage can act as the main storage layer to handle Spark, Presto, TensorFlow, H2O AI and other complex workloads, as well as becoming a substitute for Hadoop HDFS -- quoted from the official website. It's good nb to see this mini so fast. This speed is worth learning from.

Environment introduction

Programming language: you love me, I love your java

Framework: springboot

And today's protagonist: MinIO

Deployment of MinIo

In fact, the documents on the official website are very good, but they are old, I'm afraid they are ancient versions. Why do you say that? Because I started according to the document, I couldn't run and vomited. I make complaints about make blind and disorderly conjectures. Later, I asked them in the community. Then the good official official told me that they had to tuck up the document, and update it. The situation is as follows

​ http://slack.minio.org.cn/question/737

1. Download

We can download MinIO directly to run locally, or use the container to download and run

I use docker

#docker pulls the MinIo image
docker pull minio/minio

2. Run

If you read the link of MinIO community I sent above, you must also know the startup method. Here is a real case of my running on this machine

docker run -d -p 9000:9000  -p 9001:9001  -e "MINIO_ROOT_USER=AKIAIOSFODNN7EXAMPLE"  -e "MINIO_ROOT_PASSWORD=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" -v E:\data:/data  -v E:\minio\config:/root/.minio  minio/minio server /data --console-address ":9001"

3. Testing

Visit the page to check whether the operation is successful. Enter the user name (mini_root_user) and password (mini_root_password) we typed in the command. In fact, the user name and password duck don't have to be so long. I should have lost my mind at that time.

After logging in, you can see the following interface. If you also see this interface, congratulations on completing the first step. There are 999 steps left. This still uses some operations. You can try it.

SpringBoot integration MinIo

1. pom add dependency

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

2. yml add MinIo configuration

minio:
    endpoint: http://127.0.0.1:9000
    accesskey: SUCWGJ8W4H36HC7KR6XO
    secretkwy: d9OkZ14ab04zwCd/+6iEXbPqeVapDOaDvhYcw88r

3. Add configuration class in SpringBoot

Configuration file value injection. Here we get the configuration information in yml for the next connection to MinIO.

package com.zggk.mas.entity.sys;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * minio Attribute value
 */
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProp {
    //Connection url
    private String endpoint;
    //Public key
    private String accesskey;
    //Private key
    private  String secretkwy;
}

Configure MinIO connections

package com.zggk.mas.config;

import com.zggk.mas.entity.sys.MinioProp;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * minio Core configuration class
 * The MinioClient object is obtained by injecting the relevant configuration information of the MinIO server. The uploaded file depends on this object
 */
@Configuration
public class MinioConfig {

    @Autowired
    private MinioProp prop;

    /**
     * Get MinioClient
     * @return MinioClient
     */
    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder().endpoint(prop.getEndpoint()).
                credentials(prop.getAccesskey(),prop.getSecretkwy()).build();
    }
}

Next, we will write a tool class uploaded by MinIo

package com.zggk.mas.entity.util;

import com.zggk.mas.entity.sys.MinioProp;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * minio Operation class
 */
@Component
public class MinioUtils {
    @Autowired
    private MinioClient client;
    @Autowired
    private MinioProp minioProp;

    /**
     * Create bucket
     *
     * @param bucketName Bucket name
     */
    @SneakyThrows
    public void createBucket(String bucketName) {
        boolean found =
                client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!found) {
            client.makeBucket(
                    MakeBucketArgs.builder()
                            .bucket(bucketName)
                            .region("cn-beijing")
                            .build());
        }
    }

    /**
     * Delete bucket
     *
     * @param bucketName Bucket name
     */
    @SneakyThrows
    public void removeBucket(String bucketName) {
        client.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * Get file information
     *
     * @param bucketName Bucket name
     * @param objectName File name
     * @return
     */
    @SneakyThrows
    public ObjectStat getObjectInfo(String bucketName, String objectName) {
        return client.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

    /**
     * Upload file
     *
     * @param bucketName Bucket name
     * @param objectName file name
     * @param stream     flow
     * @param fileSize   file size
     * @param type       file type
     * @throws Exception
     */
    public void putObject(String bucketName, String objectName, InputStream stream, Long fileSize, String type) throws Exception {
        client.putObject(
                PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(
                        stream, fileSize, -1)
                        .contentType(type)
                        .build());
    }


    /**
     * Determine whether the folder exists
     *
     * @param bucketName Bucket name
     * @param prefix     Folder name
     * @return
     */
    @SneakyThrows
    public Boolean folderExists(String bucketName, String prefix) {
        Iterable<Result<Item>> results = client.listObjects(ListObjectsArgs.builder().bucket(bucketName)
                .prefix(prefix).recursive(false).build());
        for (Result<Item> result:results){
            Item item = result.get();
            if (item.isDir()){
                return true;
            }
        }
        return false;
    }

    /**
     * create folder
     * @param bucketName Bucket name
     * @param path route
     */
    @SneakyThrows
    public void createFolder(String bucketName,String path) {
        client.putObject(PutObjectArgs.builder().bucket(bucketName).object(path)
                .stream(new ByteArrayInputStream(new byte[]{}),0,-1).build());
    }

    /**
     * Get the external chain of the file on the minio server
     * @param bucketName Bucket name
     * @param objectName file name
     * @return
     */
    @SneakyThrows
    public String getObjectUrl(String bucketName,String objectName){
        return client.getPresignedObjectUrl(
                     GetPresignedObjectUrlArgs.builder()
                           .method(Method.GET)
                          .bucket(bucketName)
                          .object(objectName)
                          .build());
    }

}

At this point, we can happily use MinIo, with a speed of tens of miles and a free mood.

epilogue

If you find anything wrong with the article, please give your child some advice. Thank you.

Topics: Java Spring Boot MinIO