当前位置:网站首页>New and old versions of Minio tool classes and deployment documents

New and old versions of Minio tool classes and deployment documents

2022-06-09 05:49:00 Xiao Sheng, you are polite

work Document format , Adjust later

MinIO  Using document 
 Download and install 
 Official website download address :https://min.io/download

 notes : The table of contents and ip To set according to your own project 
 Single deployment 
1. Create directory 
1.1 Create save minio Catalog 
mkdir -p /minio
1.2 Create a startup directory 
mkdir -p /minio/bin/
1.3 establish minio bucket Catalog , That is, upload the file and save the directory 
mkdir -p /minio/data

2. Create startup script 
start.sh
#!/bin/bash
# Set user name and password 
export MINIO_ACCESS_KEY=panys
export MINIO_SECRET_KEY=panys123456
# Set port  --address ip:port
# Background start 
nohup /minio/minio server --address 192.168.201.149:9002 /minio/data/ > /minio/logs/minio.log 2>&1 &


3. Create stop script 
#!/bin/bash
# stop it  stop
ps -ef | grep -w minio | grep -v grep | awk '{print $2}'|xargs kill -9


4. to grant authorization 
4.1 to grant authorization minio file 
chmod +x minio
4.2 To script start.sh and stop.sh to grant authorization 
chmod+x start.sh stop.sh
5. Start the test 
 Run startup script 
./minio/bin/start.sh
 Enter the access address on the browser http://192.168.201.149:9002, Test creation bucket And upload files 

 The above figure shows that the single machine deployment is successful 

 Cluster deployment 
 Because the official recommendation is that the cluster deployment requires four nodes , So this document uses two machines , Two disk mode deployments per machine 
192.168.201.149  /minio/data   /minio/data2
192.168.201.150  /minio/data   /minio/data2
   
1. Create directory 
1.1 Create save minio Catalog 
mkdir -p /minio
 Upload and download minio File to minio Catalog 
1.2 Create a startup directory 
mkdir -p /minio/bin/
1.3 establish minio bucket Catalog 
 That is, upload the file and save the directory , Both machines need to create 
mkdir -p /minio/{
    data,data2}
2. Create startup script 
start.sh
#!/bin/bash
# Set user name and password 
export MINIO_ACCESS_KEY=panys
export MINIO_SECRET_KEY=panys123456
# Modify access  --address ip:port
# Background start 
nohup /minio/minio server --address 0.0.0.0:9002 http://192.168.201.149/minio/data http://192.168.201.149/minio/data2 http://192.168.201.150/minio/data http://192.168.201.150/minio/data2 > /minio/logs/minio.log 2>&1 &


3. Create stop script 
stop.sh
#!/bin/bash
# stop it  stop
ps -ef | grep -w minio | grep -v grep | awk '{print $2}'|xargs kill -9

4. to grant authorization 
 Both machines need to perform 
4.1 to grant authorization minio file 
chmod +x minio
4.2 To script start.sh and stop.sh to grant authorization 
chmod+x start.sh stop.sh

5. Start the test 
 Two machines run the startup script 
./minio/bin/start.sh
 Enter the access address on the browser http://192.168.201.149:9002, Test creation bucket And upload files 

 Enter the access address on the browser http://192.168.201.150:9002, If there are the same files, the cluster will deploy minio success .
Nginx agent 
minio Cluster deployment , Provide a unified access path , have access to nginx Configure load balancing 
 as follows :
upstream minio{
    
	server 192.168.201.149:9002;
	server 192.168.201.150:9002;
}
server {
    
	listen 9002;
	server_name minio;
	location / {
    
		proxy_set_header Host $http_host;
		proxy_set_header X-Forwarded-For $remote_addr;
		client_body_buffer_size 10M;
		client_max_body_size 10G;
		proxy_buffers 1024 4k;
		proxy_read_timeout 300;
		proxy_next_upstream error timeout http_404;
		proxy_pass http://minio;
	}
}

 Integrate springboot 2.x API
Java client API  Official documents 
https://docs.min.io/docs/java-client-quickstart-guide.html

 explain :minio from 7.0.2 Changes started after the version ,API Somewhat different , The parameters are specified 

 The configuration file yaml
# minio  To configure ,endpoint Access path ,accessKey  user name ,secretKey  password ,bucketName  bucket 
min:
  io:
    endpoint: http://192.168.201.148:9002
    accessKey: aaa
secretKey: aaa123456
bucketName: test


 Old edition 
 introduce maven file 
 Old edition :
<dependency>
	<groupId>io.minio</groupId>
	<artifactId>minio</artifactId>
	<version>7.0.2</version>
</dependency>
 new edition :
<dependency>
	<groupId>io.minio</groupId>
	<artifactId>minio</artifactId>
	<version>8.2.1</version>
</dependency>

 Tool class 
MinIoProperties
package cn.emd.platform.common.file.config;

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

/** * MinIo Entity parameter class  * * @author pan * @date 2021/4/26 */
@Data
@Component
@ConfigurationProperties(prefix = "min.io")
public class MinIoProperties {
    

    /** *  Access address  */
    private String endpoint;

    /** *  user name  */
    private String accessKey;

    /** *  password  */
    private String secretKey;

    /** *  bucket  */
    private String bucketName="pan";

}


 Description of special parameters 
objectName The file name of the bucket , It could be a file , It could be a folder + file 
 for example :Entity.vm perhaps pan/Entity.vm
filename  The address of the document , The location address of the file when uploading the file 
 for example :D://Entity.vm
fileName  File storage address , Save file location address when downloading 
 for example :D://pan/entity.vm 

 Old version tool class 
MinIoUtils

package cn.emd.platform.common.file.util;

import cn.emd.platform.common.core.exception.DynamicException;
import cn.emd.platform.common.file.config.MinIoProperties;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;

import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.InputStream;

/** * MinIo Tool class  * * @author pan * @date 2021/4/25 */
@Component
@Configuration
@EnableConfigurationProperties(MinIoProperties.class)
public class MinIoUtils {
    

    private MinIoProperties minIoProperties;
    private MinioClient minioClient;

    public MinIoUtils(MinIoProperties minIoProperties){
    
        this.minIoProperties=minIoProperties;
    }

    @PostConstruct
    public void init(){
    
        try {
    
            minioClient=new MinioClient(minIoProperties.getEndpoint(),
                    minIoProperties.getAccessKey(),
                    minIoProperties.getSecretKey());
            // Create a default bucket
            boolean found =this.bucketExists(minIoProperties.getBucketName());
            if (!found) {
    
                this.makeBucket(minIoProperties.getBucketName());
            }
        } catch (Exception e) {
    
            e.printStackTrace();
        }
    }

    /** *  Judge whether it exists  bucket * @param bucketName  bucket  * @return  There is true, non-existent false */
    public Boolean bucketExists(String bucketName){
    

        try {
    
            return minioClient.bucketExists(bucketName);
        } catch (Exception e) {
    
            e.printStackTrace();
        }
        return false;
    }

    /** *  establish bucket * * @author pan * @date 2021/4/26 */
    public void makeBucket(String bucketName){
    

        try {
    
            if(bucketExists(bucketName)){
    
                throw new DynamicException(bucketName+" Already exists ");
            }
            minioClient.makeBucket(bucketName);

        } catch (Exception e) {
    
            e.printStackTrace();
        }
    }

    /** *  Use the default bucket bucket * * @author pan * @date 2021/4/26 */
    public String useDefaultBucket(String bucketName){
    
        if(StringUtils.isBlank(bucketName)){
    
            bucketName=minIoProperties.getBucketName();
        }
        return bucketName;
    }

    /** *  Upload files ( Address ) * @param bucketName  Bucket name  * @param objectName  Files saved in buckets  * @param filename  file + route  * @return  File save location  */
    @SneakyThrows
    public String uploadFile(String bucketName,String objectName,String filename){
    
        // If the bucket is empty , Use the default 
        bucketName = useDefaultBucket(bucketName);
        if (!this.bucketExists(bucketName)) {
    
            throw new DynamicException(bucketName + " Bucket does not exist ");
        }
        minioClient.putObject(bucketName,objectName,filename,null);
        return objectName;
    }

    /** *  Upload files  * @param bucketName  Bucket name  * @param objectName  Files saved in buckets  * @param multipartFile  File stream  * @return  File save location  */
    @SneakyThrows
    public String uploadFile(String bucketName, String objectName, MultipartFile multipartFile){
    
        // If the bucket is empty , Use the default 
        bucketName = useDefaultBucket(bucketName);
        if (!this.bucketExists(bucketName)) {
    
            throw new DynamicException(bucketName + " Bucket does not exist ");
        }
        InputStream inputStream=multipartFile.getInputStream();
        PutObjectOptions putObjectOptions=new PutObjectOptions(multipartFile.getSize(),-1L);
        minioClient.putObject(bucketName,objectName,inputStream,putObjectOptions);
        return objectName;
    }

    /** *  Delete file  * @param bucketName  Bucket name  * @param objectName  Files saved in buckets  * @return  File save location  */
    @SneakyThrows
    public String deleteFile(String bucketName, String objectName){
    
        bucketName = useDefaultBucket(bucketName);
        if (!this.bucketExists(bucketName)) {
    
            throw new DynamicException(bucketName + " Bucket does not exist ");
        }
        minioClient.removeObject(bucketName,objectName);
        return objectName;
    }

    /** *  Download the file  * @param bucketName  Bucket name  * @param objectName  Files saved in buckets  * @param fileName  File save location  * @return  File save location  */
    @SneakyThrows
    public String downloadFile(String bucketName, String objectName,String fileName){
    
        bucketName = useDefaultBucket(bucketName);
        if (!this.bucketExists(bucketName)) {
    
            throw new DynamicException(bucketName + " Bucket does not exist ");
        }
        minioClient.getObject(bucketName,objectName,fileName);
        return fileName;
    }

    /** *  Download the file  * @param bucketName  Bucket name  * @param objectName  Files saved in buckets  * @return  File save location  */
    @SneakyThrows
    public InputStream downloadFile(String bucketName, String objectName){
    
        bucketName = useDefaultBucket(bucketName);
        if (!this.bucketExists(bucketName)) {
    
            throw new DynamicException(bucketName + " Bucket does not exist ");
        }
        return minioClient.getObject(bucketName,objectName);
    }
}


 New tools 
MinIoUtil
package cn.emd.platform.common.file.util;

import cn.emd.platform.common.core.exception.DynamicException;
import cn.emd.platform.common.file.config.MinIoProperties;
import io.minio.*;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.InputStream;

/** * MinIo Tool class  * * @author pan * @date 2021/4/27 */
@Component
@Configuration
@EnableConfigurationProperties(MinIoProperties.class)
public class MinIoUtil {
    

    private MinIoProperties minIoProperties;
    private MinioClient minioClient;

    public MinIoUtil(MinIoProperties minIoProperties){
    
        this.minIoProperties=minIoProperties;
    }

    @PostConstruct
    public void init(){
    

        minioClient=MinioClient.builder()
                .endpoint(minIoProperties.getEndpoint())
                .credentials(minIoProperties.getAccessKey(),minIoProperties.getSecretKey())
                .build();
        // Create a default bucket, Storage bucket 
        try {
    
            boolean found =this.bucketExists(minIoProperties.getBucketName());
            if (!found) {
    
                this.makeBucket(minIoProperties.getBucketName());
            }
        }catch (Exception e){
    
            e.printStackTrace();
        }
    }

    /** *  Judge whether it exists  bucket * @param bucketName  bucket  * @return  There is true, non-existent false */
    public Boolean bucketExists(String bucketName){
    

        try {
    
            return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        } catch (Exception e) {
    
            e.printStackTrace();
        }
        return false;
    }

    /** *  establish bucket * * @author pan * @date 2021/4/26 */
    public void makeBucket(String bucketName){
    

        try {
    
            if(this.bucketExists(bucketName)){
    
                throw new DynamicException(bucketName+" Already exists ");
            }
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());

        } catch (Exception e) {
    
            e.printStackTrace();
        }
    }

    /** *  Use the default bucket bucket * * @author pan * @date 2021/4/26 */
    public String useDefaultBucket(String bucketName){
    
        if(StringUtils.isBlank(bucketName)){
    
            bucketName=minIoProperties.getBucketName();
        }
        return bucketName;
    }

    /** *  Upload files ( Browser mode ) * * @param bucketName  Bucket name  * @param objectName  Files saved in buckets  * @param multipartFile  File stream  * @return  File save location  */
    @SneakyThrows
    public String uploadFile(String bucketName, String objectName, MultipartFile multipartFile) {
    
        // If the bucket is empty , Use the default 
        bucketName=useDefaultBucket(bucketName);
        if (!this.bucketExists(bucketName)) {
    
            throw new DynamicException(bucketName + " Bucket does not exist ");
        }
        InputStream inputStream = multipartFile.getInputStream();
        minioClient.putObject(PutObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .stream(inputStream,multipartFile.getSize(),-1L)
                .build());
        return objectName;
    }

    /** *  Upload files ( Address ) * * @param bucketName  Bucket name  * @param objectName  Files saved in buckets  * @param filename  File address  * @return  File save location  */
    @SneakyThrows
    public String uploadFile(String bucketName, String objectName, String filename) {
    
        // If the bucket is empty , Use the default 
        bucketName = useDefaultBucket(bucketName);
        if (!this.bucketExists(bucketName)) {
    
            throw new DynamicException(bucketName + " Bucket does not exist ");
        }
        minioClient.uploadObject(UploadObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .filename(filename)
                .build()
        );
        return objectName;
    }

    /** *  Download the file  * * @param bucketName  Bucket name  * @param objectName  Files saved in buckets  * @param fileName  File save location  * @return  File save location  */
    @SneakyThrows
    public String downloadFile(String bucketName, String objectName, String fileName) {
    
        bucketName = useDefaultBucket(bucketName);
        if (!this.bucketExists(bucketName)) {
    
            throw new DynamicException(bucketName + " Bucket does not exist ");
        }
        minioClient.downloadObject(DownloadObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .filename(fileName)
                .build()
        );
        return fileName;
    }

    /** *  Download the file  * * @param bucketName  Bucket name  * @param objectName  Files saved in buckets  * @return  File stream  */
    @SneakyThrows
    public InputStream downloadFile(String bucketName, String objectName) {
    
        bucketName = useDefaultBucket(bucketName);
        if (!this.bucketExists(bucketName)) {
    
            throw new DynamicException(bucketName + " Bucket does not exist ");
        }
        InputStream inputStream=minioClient.getObject(GetObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build()
        );
        return inputStream;
    }


    /** *  Delete file  * * @param bucketName  Bucket name  * @param objectName  Files saved in buckets  * @return  File save location  */
    @SneakyThrows
    public String deleteFile(String bucketName, String objectName) {
    
        bucketName = useDefaultBucket(bucketName);
        if (!this.bucketExists(bucketName)) {
    
            throw new DynamicException(bucketName + " Bucket does not exist ");
        }
        minioClient.removeObject(RemoveObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build());
        return objectName;
    }

}


原网站

版权声明
本文为[Xiao Sheng, you are polite]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021426592243.html