美文网首页
Minio部署和集成

Minio部署和集成

作者: 定金喜 | 来源:发表于2023-01-17 13:30 被阅读0次

    1.部署

    • 1.docker启动minio

    创建目录/home/admin/data/minio,执行run命令

    docker run -p 9090:9090 -p 9091:9091 --name minio --privileged=true -v /home/admin/data/minio:/data -e "MINIO_ROOT_USER=admin" -e "MINIO_ROOT_PASSWORD=admin12345" quay.io/minio/minio server /data --console-address ":9091" --address ":9090"

    9091为客户端使用端口,9090为服务器连接使用的端口
    启动成功会有提示


    但是启动是阻塞式,当关闭客户端时,minio进程也会关闭,需要重启下
    docker start minio

    • 2.部署nginx
      因为minio默认是只接受内网访问的,如果需要支持外网,则可以通过nginx,先将防火墙中的端口9090和9091端口开放

    docker run --restart=always --name nginx -d -p 80:80 -p 443:443 -e TZ="Asia/Shanghai" nginx:1.13

    启动成功后,进入nginx容器,修改/etc/nginx/nginx.conf文件,增加路由转发

    server {
        listen       9090;
            server_name  _;
            root         /usr/share/nginx/html;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
    
        proxy_connect_timeout   500;
            proxy_send_timeout      500;
            proxy_read_timeout      500;
        send_timeout        500;
        fastcgi_read_timeout    500;
            proxy_buffer_size       128k;
            proxy_buffers           8 64k;
            proxy_busy_buffers_size 128k;
            proxy_temp_file_write_size 128k;
        client_max_body_size 50000m;
    
            location / {
                #转发端口
                proxy_pass http://172.17.0.12:9090;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    
            error_page 404 /404.html;
                location = /40x.html {
            }
    
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
        }
        
        server {
        listen       9091;
            server_name  _;
            root         /usr/share/nginx/html;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
    
        proxy_connect_timeout   500;
            proxy_send_timeout      500;
            proxy_read_timeout      500;
        send_timeout        500;
        fastcgi_read_timeout    500;
            proxy_buffer_size       128k;
            proxy_buffers           8 64k;
            proxy_busy_buffers_size 128k;
            proxy_temp_file_write_size 128k;
        client_max_body_size 50000m;
    
            location / {
                #转发端口
                proxy_pass http://172.17.0.12:9091;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    
            error_page 404 /404.html;
                location = /40x.html {
            }
    
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
    
        }
    

    容器内重启下nginx
    nginx -s reload

    • 3.minio操作界面体验


      image.png
      image.png

    2.使用

    • 1.springboot集成
    <dependency>
        <groupId>io.minio</groupId>
        <artifactId>minio</artifactId>
        <version>8.2.1</version>
    </dependency>
    

    工具类集成:

    package com.ali.fetchdata.controller.aspect;
    
    import java.io.ByteArrayInputStream;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Optional;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.multipart.MultipartFile;
    
    import io.minio.*;
    import io.minio.http.Method;
    import io.minio.messages.Bucket;
    import io.minio.messages.DeleteObject;
    import io.minio.messages.Item;
    
    @Slf4j
    public class MinIOUtils {
    
        private static MinioClient minioClient;
    
        private static String endpoint;
        private static String bucketName;
        private static String accessKey;
        private static String secretKey;
        private static Integer imgSize;
        private static Integer fileSize;
    
    
        private static final String SEPARATOR = "/";
    
        public MinIOUtils() {
        }
    
        public MinIOUtils(String endpoint, String bucketName, String accessKey, String secretKey) {
            MinIOUtils.endpoint = endpoint;
            MinIOUtils.bucketName = bucketName;
            MinIOUtils.accessKey = accessKey;
            MinIOUtils.secretKey = secretKey;
            createMinioClient();
        }
    
        /**
         * 创建基于Java端的MinioClient
         */
        public void createMinioClient() {
            try {
                if (null == minioClient) {
                    log.info("开始创建 MinioClient...");
                    minioClient = MinioClient
                            .builder()
                            .endpoint(endpoint)
                            .credentials(accessKey, secretKey)
                            .build();
                    createBucket(bucketName);
                    log.info("创建完毕 MinioClient...");
                }
            } catch (Exception e) {
                log.error("[Minio工具类]>>>> MinIO服务器异常:", e);
            }
        }
    
        /**
         * 获取上传文件前缀路径
         * @return
         */
        public static String getBasisUrl() {
            return endpoint + SEPARATOR + bucketName + SEPARATOR;
        }
    
        /******************************  Operate Bucket Start  ******************************/
    
        /**
         * 启动SpringBoot容器的时候初始化Bucket
         * 如果没有Bucket则创建
         * @throws Exception
         */
        private static void createBucket(String bucketName) throws Exception {
            if (!bucketExists(bucketName)) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }
        }
    
        /**
         *  判断Bucket是否存在,true:存在,false:不存在
         * @return
         * @throws Exception
         */
        public static boolean bucketExists(String bucketName) throws Exception {
            return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        }
    
    
        /**
         * 获得Bucket的策略
         * @param bucketName
         * @return
         * @throws Exception
         */
        public static String getBucketPolicy(String bucketName) throws Exception {
            return minioClient
                    .getBucketPolicy(
                            GetBucketPolicyArgs
                                    .builder()
                                    .bucket(bucketName)
                                    .build()
                    );
        }
    
    
        /**
         * 获得所有Bucket列表
         * @return
         * @throws Exception
         */
        public static List<Bucket> getAllBuckets() throws Exception {
            return minioClient.listBuckets();
        }
    
        /**
         * 根据bucketName获取其相关信息
         * @param bucketName
         * @return
         * @throws Exception
         */
        public static Optional<Bucket> getBucket(String bucketName) throws Exception {
            return getAllBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
        }
    
        /**
         * 根据bucketName删除Bucket,true:删除成功; false:删除失败,文件或已不存在
         * @param bucketName
         * @throws Exception
         */
        public static void removeBucket(String bucketName) throws Exception {
            minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
        }
    
        /******************************  Operate Bucket End  ******************************/
    
    
        /******************************  Operate Files Start  ******************************/
    
        /**
         * 判断文件是否存在
         * @param bucketName 存储桶
         * @param objectName 文件名
         * @return
         */
        public static boolean isObjectExist(String bucketName, String objectName) {
            boolean exist = true;
            try {
                minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
            } catch (Exception e) {
                log.error("[Minio工具类]>>>> 判断文件是否存在, 异常:", e);
                exist = false;
            }
            return exist;
        }
    
        /**
         * 判断文件夹是否存在
         * @param bucketName 存储桶
         * @param objectName 文件夹名称
         * @return
         */
        public static boolean isFolderExist(String bucketName, String objectName) {
            boolean exist = false;
            try {
                Iterable<Result<Item>> results = minioClient.listObjects(
                        ListObjectsArgs.builder().bucket(bucketName).prefix(objectName).recursive(false).build());
                for (Result<Item> result : results) {
                    Item item = result.get();
                    if (item.isDir() && objectName.equals(item.objectName())) {
                        exist = true;
                    }
                }
            } catch (Exception e) {
                log.error("[Minio工具类]>>>> 判断文件夹是否存在,异常:", e);
                exist = false;
            }
            return exist;
        }
    
        /**
         * 根据文件前置查询文件
         * @param bucketName 存储桶
         * @param prefix 前缀
         * @param recursive 是否使用递归查询
         * @return MinioItem 列表
         * @throws Exception
         */
        public static List<Item> getAllObjectsByPrefix(String bucketName,
                                                       String prefix,
                                                       boolean recursive) throws Exception {
            List<Item> list = new ArrayList<>();
            Iterable<Result<Item>> objectsIterator = minioClient.listObjects(
                    ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build());
            if (objectsIterator != null) {
                for (Result<Item> o : objectsIterator) {
                    Item item = o.get();
                    list.add(item);
                }
            }
            return list;
        }
    
        /**
         * 获取文件流
         * @param bucketName 存储桶
         * @param objectName 文件名
         * @return 二进制流
         */
        public static InputStream getObject(String bucketName, String objectName) throws Exception {
            return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
        }
    
        /**
         * 断点下载
         * @param bucketName 存储桶
         * @param objectName 文件名称
         * @param offset 起始字节的位置
         * @param length 要读取的长度
         * @return 二进制流
         */
        public InputStream getObject(String bucketName, String objectName, long offset, long length)throws Exception {
            return minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .offset(offset)
                            .length(length)
                            .build());
        }
    
        /**
         * 获取路径下文件列表
         * @param bucketName 存储桶
         * @param prefix 文件名称
         * @param recursive 是否递归查找,false:模拟文件夹结构查找
         * @return 二进制流
         */
        public static Iterable<Result<Item>> listObjects(String bucketName, String prefix,
                                                         boolean recursive) {
            return minioClient.listObjects(
                    ListObjectsArgs.builder()
                            .bucket(bucketName)
                            .prefix(prefix)
                            .recursive(recursive)
                            .build());
        }
    
        /**
         * 使用MultipartFile进行文件上传
         * @param bucketName 存储桶
         * @param file 文件名
         * @param objectName 对象名
         * @param contentType 类型
         * @return
         * @throws Exception
         */
        public static ObjectWriteResponse uploadFile(String bucketName, MultipartFile file,
                                                     String objectName, String contentType) throws Exception {
            InputStream inputStream = file.getInputStream();
            return minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .contentType(contentType)
                            .stream(inputStream, inputStream.available(), -1)
                            .build());
        }
    
        /**
         * 上传本地文件
         * @param bucketName 存储桶
         * @param objectName 对象名称
         * @param fileName 本地文件路径
         */
        public static ObjectWriteResponse uploadFile(String bucketName, String objectName,
                                                     String fileName) throws Exception {
            return minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .filename(fileName)
                            .build());
        }
    
        /**
         * 通过流上传文件
         *
         * @param bucketName 存储桶
         * @param objectName 文件对象
         * @param inputStream 文件流
         */
        public static ObjectWriteResponse uploadFile(String bucketName, String objectName, InputStream inputStream) throws Exception {
            return minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .stream(inputStream, inputStream.available(), -1)
                            .build());
        }
    
        /**
         * 创建文件夹或目录
         * @param bucketName 存储桶
         * @param objectName 目录路径
         */
        public static ObjectWriteResponse createDir(String bucketName, String objectName) throws Exception {
            return minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .stream(new ByteArrayInputStream(new byte[]{}), 0, -1)
                            .build());
        }
    
        /**
         * 获取文件信息, 如果抛出异常则说明文件不存在
         *
         * @param bucketName 存储桶
         * @param objectName 文件名称
         */
        public static String getFileStatusInfo(String bucketName, String objectName) throws Exception {
            return minioClient.statObject(
                    StatObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .build()).toString();
        }
    
        /**
         * 拷贝文件
         *
         * @param bucketName 存储桶
         * @param objectName 文件名
         * @param srcBucketName 目标存储桶
         * @param srcObjectName 目标文件名
         */
        public static ObjectWriteResponse copyFile(String bucketName, String objectName,
                                                   String srcBucketName, String srcObjectName) throws Exception {
            return minioClient.copyObject(
                    CopyObjectArgs.builder()
                            .source(CopySource.builder().bucket(bucketName).object(objectName).build())
                            .bucket(srcBucketName)
                            .object(srcObjectName)
                            .build());
        }
    
        /**
         * 删除文件
         * @param bucketName 存储桶
         * @param objectName 文件名称
         */
        public static void removeFile(String bucketName, String objectName) throws Exception {
            minioClient.removeObject(
                    RemoveObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .build());
        }
    
        /**
         * 批量删除文件
         * @param bucketName 存储桶
         * @param keys 需要删除的文件列表
         * @return
         */
        public static void removeFiles(String bucketName, List<String> keys) {
            List<DeleteObject> objects = new LinkedList<>();
            keys.forEach(s -> {
                objects.add(new DeleteObject(s));
                try {
                    removeFile(bucketName, s);
                } catch (Exception e) {
                    log.error("[Minio工具类]>>>> 批量删除文件,异常:", e);
                }
            });
        }
    
        /**
         * 获取文件外链
         * @param bucketName 存储桶
         * @param objectName 文件名
         * @param expires 过期时间 <=7 秒 (外链有效时间(单位:秒))
         * @return url
         * @throws Exception
         */
        public static String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) throws Exception {
            GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().expiry(expires).bucket(bucketName).object(objectName).build();
            return minioClient.getPresignedObjectUrl(args);
        }
    
        /**
         * 获得文件外链
         * @param bucketName
         * @param objectName
         * @return url
         * @throws Exception
         */
        public static String getPresignedObjectUrl(String bucketName, String objectName) throws Exception {
            GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
                    .bucket(bucketName)
                    .object(objectName)
                    .method(Method.GET).build();
            return minioClient.getPresignedObjectUrl(args);
        }
    
        /**
         * 将URLDecoder编码转成UTF8
         * @param str
         * @return
         * @throws UnsupportedEncodingException
         */
        public static String getUtf8ByURLDecoder(String str) throws UnsupportedEncodingException {
            String url = str.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            return URLDecoder.decode(url, "UTF-8");
        }
    
        /******************************  Operate Files End  ******************************/
    }
    
    • 2.demo
    public static void main(String[] args) throws Exception{
    
            MinIOUtils minIOUtils = new MinIOUtils("http://XX.XX.XX.XX:9090","cloud-patrol","root","12345");
            long now = System.currentTimeMillis();
            MinIOUtils.uploadFile("cloud-patrol","gdtterhsskwwddfrrr/edrrs/test.zip","/Users/admin/Downloads/test.zip");
            System.out.println(System.currentTimeMillis()-now);
        }
    

    相关文章

      网友评论

          本文标题:Minio部署和集成

          本文链接:https://www.haomeiwen.com/subject/aqgthdtx.html