Location>code7788 >text

MinIO Linux Installation & SpringBoot Integration with MinIO

Popularity:812 ℃/2024-11-19 10:20:12

catalogs
  • MinIO Linux Installation
    • Single Node Deployment
    • Create the systemd system startup service file
    • Creating an environment variable file
    • Start the MinIO service
    • Connecting to MinIO Services
  • SpringBoot Project Integration with MinIO
    • configuration item
    • tools
    • beta (software)

Minio is an object storage service based on the Apache License v2.0 open source agreement . It can run on a variety of operating systems , including Linux and Windows and so on.
It is compatible with the Amazon S3 cloud storage service interface and is ideal for storing high-capacity unstructured data such as images, videos, log files, backup data and container/virtual machine images, etc., while an object file can be of any size, ranging from a few kilobytes up to a maximum of 5 terabytes.

Minio official website:/
Chinese address:
Chinese Documentation:/docs/minio/linux/
Demo: Username: minioadmin, password minioadmin

MinIO Linux Installation

Single Node Deployment

Download: /#/linux
Use the following command to download and install the latest version of the stable MinIO binary and set $PATH :

[root@localhost ~]# wget /server/minio/release/linux-amd64/minio
[root@localhost ~]# chmod +x minio
[root@localhost ~]# sudo mv minio /usr/local/bin/

image

Create the systemd system startup service file

Create the startup file and make sure it is in the/usr/lib/systemd/system/

[root@localhost ~]# vi /usr/lib/systemd/system/

[Unit]
Description=MinIO
Documentation=/docs/minio/linux/
Wants=
After=
AssertFileIsExecutable=/usr/local/bin/minio

[Service]
WorkingDirectory=/usr/local

User=minio-user
Group=minio-user
ProtectProc=invisible

EnvironmentFile=-/etc/default/minio
ExecStartPre=/bin/bash -c "if [ -z \"${MINIO_VOLUMES}\" ]; then echo \"Variable MINIO_VOLUMES not set in /etc/default/minio\"; exit 1; fi"
ExecStart=/usr/local/bin/minio server $MINIO_OPTS $MINIO_VOLUMES

# MinIO RELEASE.2023-05-04T21-44-30Z adds support for Type=notify (/software/systemd/man/#Type=)
# This may improve systemctl setups where other services use `After=`
# Uncomment the line to enable the functionality
# Type=notify

# Let systemd restart this service always
Restart=always

# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=65536

# Specifies the maximum number of threads this process can create
TasksMax=infinity

# Disable timeout logic and wait until process is stopped
TimeoutStopSec=infinity
SendSIGKILL=no

[Install]
WantedBy=

# Built for ${}-${} (${})

By default, the fileminio-user users and groups to run as. You can use thegroupadd cap (a poem)useradd Create users and groups. command. The following example creates a user and group and sets permissions to access folder paths for MinIO use. These commands typically require root (sudo) privileges.

[root@localhost ~]# groupadd -r minio-user
[root@localhost ~]# useradd -M -r -g minio-user minio-user
# Storage Path
[root@localhost ~]# chown minio-user:minio-user /mnt/data
chown: inaccessible"/mnt/data": There is no such file or directory.
[root@localhost ~]# mkdir /mnt/data
[root@localhost ~]# chown minio-user:minio-user /mnt/data

The drive path in this example is specified by the MINIO_VOLUMES environment variable. Change the values here and in the environment variable file to match the drive path that MinIO intends to use.

Creating an environment variable file

exist/etc/default/minio Create an environment variable file. The MinIO server container can use this file as the environment variable file for allenvironment variables
The following example provides a starting environment file.
The password must not be less than 8 characters, otherwise it won't start.

[root@localhost ~]# vi /etc/default/minio
# MINIO_ROOT_USER and MINIO_ROOT_PASSWORD sets the root account for the MinIO server.
# This user has unrestricted permissions to perform S3 and administrative API operations on any resource in the deployment.
# Omit to use the default values 'minioadmin:minioadmin'.
# MinIO recommends setting non-default values as a best practice, regardless of environment
# The length of the username cannot be less than3characters
MINIO_ROOT_USER=admin
# The password cannot be smaller than8characters, Otherwise it won't start.
MINIO_ROOT_PASSWORD=minioadmin

# MINIO_VOLUMES sets the storage volume or path to use for the MinIO server.

MINIO_VOLUMES="/mnt/data"

# MINIO_OPTS sets any additional commandline options to pass to the MinIO server.
# For example, `--console-address :9001` sets the MinIO Console listen port
MINIO_OPTS="--console-address :9001"

Start the MinIO service

Start MinIO SNSD Deployment as a Service:

[root@localhost ~]# sudo systemctl start 

Use the following command to confirm that the service is online and functioning properly:

[root@localhost ~]# sudo systemctl status 
[root@localhost ~]# journalctl -f -u 

Self-starting, where the process is used as part of the host boot, and the process is automatically restarted during a server reboot without having to be managed manually.

[root@localhost ~]# sudo systemctl enable 

image

Connecting to MinIO Services

Enter it in your browser:http://localhost:9001
The username and password configuration parameters for logging into MinIO areMINIO_ROOT_USER cap (a poem)MINIO_ROOT_PASSWORD These configurations can be modified in the environment file specified in the container.
image
image
You can use the MinIO Console for general administrative tasks such as identity and access management, metrics and log monitoring, or server configuration. Each MinIO server contains its own embedded MinIO console. If your local host firewall allows external access to the console port, other hosts on the same network can access the console using your local host's IP address or host name.

SpringBoot Project Integration with MinIO

/artifact//minio

configuration item

Adding Dependencies

<dependency>
    <groupId></groupId>
    <artifactId>minio</artifactId>
    <version>8.5.10</version>
</dependency>

Configuring minio


spring.
     mvc.
      hiddenmethod.
        filter.
          enabled: true
  # Set file upload size limit
  servlet.
    multipart: max-file-size: -1 #Set the size limit for single file uploads.
      max-file-size: -1 #Set the size of a single file -1 means no limitation
      max-request-size: -1 #The total size of a single request - 1 means no limit

minio: endpoint:
  endpoint: http://172.16.3.195:9000
  accessKey: admin
  secretKey: minioadmin
  bucketName: vipsoft-devminioadmin

package ;


import ;
import ;
import ;
import ;

@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
    /**
     * service address:http://172.16.3.195:9000
     */
    private String endpoint;

    /**
     * user ID
     */
    private String accessKey;

    /**
     * cryptographic
     */
    private String secretKey;

    /**
     * Storage Drum Name
     */
    private String bucketName;


    @Bean
    public MinioClient getMinioClient() {
        MinioClient minioClient = ().endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
        return minioClient;
    }

    //todo make unnecesary getter & setter
}

tools

package ;

import ;
import ;
import .*;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;

@Component
public class MinioUtil {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioConfig minioConfig;

    private static final int DEFAULT_EXPIRY_TIME = 7 * 24 * 3600;

    /**
     * judgementsbucketwhether or not
     */
    public boolean bucketExists(String bucketName) {
        boolean exists;
        try {
            exists = (().bucket(bucketName).build());
        } catch (Exception e) {
            ();
            exists = false;
        }
        return exists;
    }


    /**
     * Creating Storage Buckets,Not created if it exists
     */
    public boolean makeBucket(String bucketName) {
        boolean exists;
        try {
            exists = bucketExists(bucketName);
            if (!exists) {
                (().bucket(bucketName).build());
                exists = true;
            }
        } catch (Exception e) {
            ();
            exists = false;
        }
        return exists;
    }

    /**
     * Deleting buckets -- documented,Not letting it be deleted
     *
     * @param bucketName Storage Drum Name
     * @return boolean
     */
    public boolean removeBucket(String bucketName) {
        try {
            boolean flag = bucketExists(bucketName);
            if (flag) {
                Iterable<Result<Item>> myObjects = listObjects(bucketName);
                for (Result<Item> result : myObjects) {
                    Item item = ();
                    // Object files available,then the deletion fails
                    if (() > 0) {
                        return false;
                    }
                }
                // Deleting buckets,take note of,Deletion will only succeed if the bucket is empty。
                (().bucket(bucketName).build());
                flag = bucketExists(bucketName);
                if (!flag) {
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            ();
            return false;
        }
    }


    /**
     * List all objects in the bucket
     *
     * @param bucketName Storage Drum Name
     * @return Iterable<Result < Item>>
     */
    public Iterable<Result<Item>> listObjects(String bucketName) {
        boolean flag = bucketExists(bucketName);
        if (flag) {
            return (().bucket(bucketName).build());
        }
        return null;
    }


    /**
     * 列出所有Storage Drum Name
     *
     * @return List<String>
     */
    public List<String> listBucketNames() {
        List<String> bucketListName = new ArrayList<>();
        try {
            List<Bucket> bucketList = ();
            for (Bucket bucket : bucketList) {
                (());
            }
        } catch (Exception e) {
            ();
        }
        return bucketListName;
    }

    /**
     * List all objects in the bucket名称
     *
     * @param bucketName Storage Drum Name
     * @return List<String>
     */
    public List<String> listObjectNames(String bucketName) {
        List<String> listObjectNames = new ArrayList<>();
        try {
            boolean flag = bucketExists(bucketName);
            if (flag) {
                Iterable<Result<Item>> myObjects = listObjects(bucketName);
                for (Result<Item> result : myObjects) {
                    Item item = ();
                    (());
                }
            }
        } catch (Exception e) {
            ();
        }
        return listObjectNames;
    }


    /**
     * Getting metadata about an object
     *
     * @param objectName Name of the object in the storage bucket
     * @return
     */
    public StatObjectResponse statObject(String objectName) {
        StatObjectResponse statObject = null;
        try {
            statObject = (().bucket(()).object(objectName).build());
        } catch (Exception e) {
            ();
        }
        return statObject;
    }

    //region Uploading files

    /**
     * Uploading files
     *
     * @param file       file
     * @param objectName file名称
     */
    public boolean uploadObject(MultipartFile file, String objectName) {
        // utilizationputObject上传一个file到存储桶中。
        try {
            InputStream inputStream = ();
            (()
                    .bucket(())
                    .object(objectName)
                    .stream(inputStream, (), -1)
                    .contentType(())
                    .build());
            (inputStream);
//            return ("{}/{}/{}", (), (), objectName);
            return true;
        } catch (Exception e) {
            ();
        }
        return false;
    }

    /**
     * pass (a bill or inspection etc)InputStreamUploaded by
     *
     * @param objectName  Name of the object in the storage bucket
     * @param inputStream Streams to be uploaded
     * @param contentType 上传的file类型 for example video/mp4  image/jpg
     * @return boolean
     */
    public boolean uploadObject(InputStream inputStream, String objectName, String contentType) {
        try {
            (().bucket(()).object(objectName).stream(
                            //不清楚file的大小时,transferable-1,10485760。如果知道大小也transferable入size,partsize。
                            inputStream, -1, 10485760)
                    .contentType(contentType)
                    .build());
            (inputStream);
            StatObjectResponse statObject = statObject(objectName);
            if (statObject != null && () > 0) {
                return true;
            }
        } catch (Exception e) {
            ();
        }
        return false;
    }

    /**
     * pass (a bill or inspection etc)file上传到对象
     *
     * @param objectName Name of the object in the storage bucket
     * @param fileName   File name
     * @return boolean
     */
    public boolean uploadObject(String objectName, String fileName) {
        try {
            (()
                    .bucket(())
                    .object(objectName)
                    .filename(fileName)
                    .build());
            StatObjectResponse statObject = statObject(objectName);
            if (statObject != null && () > 0) {
                return true;
            }
        } catch (Exception e) {
            ();
        }

        return false;
    }

    //endregion


    /**
     * 获取file访问地址
     *
     * @param fileName file名称
     */
    public String getPresignedObjectUrl(String fileName) {
        try {
            return (()
                    .method()
                    .bucket(())
                    .object(fileName)
                    .build()
            );
        } catch (Exception e) {
            ();
        }
        return null;
    }

    public String getObjectUrl(String objectName) {
        String url = "";
        try {
            url = (()
                    .method()
                    .bucket(())
                    .object(objectName)
                    .build());

        } catch (Exception e) {
            ();
        }
        return url;
    }

    /**
     * Generate a file that gives theHTTP GETrequestedpresigned URL。
     * browser (software)/The mobile client can use thisURLDownload,Even if the bucket it is in is private。this onepresigned URLAn expiration time can be set,The default value is7sky。
     *
     * @param objectName Name of the object in the storage bucket
     * @param expires    lapse time(in hours),The default is7sky,不得大于七sky
     * @return
     */
    public String getObjectUrl(String objectName, Integer expires) {
        String url = "";
        try {
            if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
                throw new Exception("Expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
            }
            url = (()
                    .method()
                    .bucket(())
                    .object(objectName)
                    .expiry(expires, )//dynamic parameter
                    // .expiry(24 * 60 * 60)//用秒来计算一sky时间有效期
                    // .expiry(1, )//按sky传参
                    // .expiry(1, )//Transmission of parameters by hour
                    .build());

        } catch (Exception e) {
            ();
        }
        return url;
    }

    /**
     * 下载file,pass (a bill or inspection etc) HttpServletResponse come (or go) back
     *
     * @param objectName Name of the object in the storage bucket
     */
    public void downloadObject(HttpServletResponse response, String objectName) {
        try {
            InputStream file = (()
                    .bucket(())
                    .object(objectName)
                    .build());
            ("Content-Disposition", "attachment;filename=" + (objectName, "UTF-8"));
            ServletOutputStream servletOutputStream = ();
            int len;
            byte[] buffer = new byte[1024];
            while ((len = (buffer)) > 0) {
                (buffer, 0, len);
            }
            ();
            ();
            ();
        } catch (Exception e) {
            ();
        }
    }


    /**
     * 下载并将file保存到本地
     *
     * @param objectName Name of the object in the storage bucket
     * @param fileName   下载保存的file名
     * @return boolean
     */
    public boolean downloadObject(String objectName, String fileName) {
        try {
            StatObjectResponse statObject = statObject(objectName);
            if (statObject != null && () > 0) {
                (()
                        .bucket(())
                        .object(objectName)
                        .filename(fileName)
                        .build());
                return true;
            }
        } catch (Exception e) {
            ();
        }
        return false;
    }


    /**
     * 以流的形式获取一个file对象
     *
     * @param bucketName Storage Drum Name
     * @param objectName Name of the object in the storage bucket
     * @return InputStream
     */
    public InputStream getObject(String bucketName, String objectName) {
        try {
            StatObjectResponse statObject = statObject(objectName);
            if (statObject != null && () > 0) {
                InputStream stream = (()
                        .bucket(bucketName)
                        .object(objectName)
                        .build());
                return stream;
            }
        } catch (Exception e) {
            ();
        }
        return null;
    }

    /**
     * 以流的形式获取一个file对象(download at breakpoint)
     *
     * @param bucketName Storage Drum Name
     * @param objectName Name of the object in the storage bucket
     * @param offset     Position of the start byte
     * @param length     Length to be read (selectable,如果无值则代表读到file结尾)
     * @return InputStream
     */
    public InputStream getObject(String bucketName, String objectName, long offset, Long length) {
        try {
            StatObjectResponse statObject = statObject(objectName);
            if (statObject != null && () > 0) {
                InputStream stream = (()
                        .bucket(bucketName)
                        .object(objectName)
                        .offset(1024L)
                        .length(4096L)
                        .build());
                return stream;
            }
        } catch (Exception e) {
            ();
        }
        return null;
    }


    /**
     * Deleting an object
     *
     * @param objectName Name of the object in the storage bucket
     */
    public boolean removeObject(String objectName) {
        try {
            (().bucket(()).object(objectName).build());
            return true;
        } catch (Exception e) {
            ();
        }
        return false;
    }

    /**
     * 删除指定桶的多个file对象,come (or go) back删除错误的对象列表,All deleted successfully,come (or go) back空列表
     *
     * @param bucketName  Storage Drum Name
     * @param objectNames Containing more than one item to be deletedobjectIterator object with name eg:
     *                    List<DeleteObject> objects = new LinkedList<>();
     *                    (new DeleteObject("my-objectname1"));
     *                    (new DeleteObject("my-objectname2"));
     *                    (new DeleteObject("my-objectname3"));
     * @return If there is a value,说明当前file删除失败
     */
    public List<String> removeObjects(String bucketName, List<DeleteObject> objectNames) {
        List<String> deleteErrorNames = new ArrayList<>();
        try {
            Iterable<Result<DeleteError>> results = (().bucket(bucketName).objects(objectNames).build());
            for (Result<DeleteError> result : results) {
                DeleteError error = ();
                (());
            }
        } catch (Exception e) {
            ();
        }
        return deleteErrorNames;
    }
}


beta (software)

package ;

import ;
import ;
import ;
import org.;
import org.;
import ;
import ;
import ;

@SpringBootTest
public class MinioTest {

    Logger logger = (());

    @Autowired
    private MinioUtil minioUtil;


    @Test
    void makeBucketTest() {
        boolean flag = ("public");
        (flag, "Inquiry Exception");
    }


    @Test
    void uploadObjectTest() {
        boolean flag = ("/avatar/", "D:\\Users\\Pictures\\");
        (flag, "Upload anomalies");
    }


    @Test
    void getObjectUrlTest() {
        String objectName="/avatar/";
        String url = (objectName);
        ("url:{}", url);
        url = (objectName, 3);
        ("expires url:{}", url);
        ((url), "Upload anomalies");
    }
}

image