minio下载安装

minio官方文档地址:https://docs.min.io/cn/
minio的源码地址:https://github.com/minio/minio

minio下载安装_第1张图片

window安装 :

 当前目录cmd运行

minio.exe server D:\develop\minio_data\data1  D:\develop\minio_data\data2  D:\develop\minio_data\data3  D:\develop\minio_data\data4

minio下载安装_第2张图片

minio下载安装_第3张图片

docker安装minio

docker pull minio/minio:latest
cd /mydata
mkdir minio
cd minio
mkdir cofig  #存配置
cd ./
mkdir data   #存数据

docker run -p 9000:9000 -p 9090:9090 \
     --net=host \
     --name minio \
     -d --restart=always \
     -e "MINIO_ACCESS_KEY=minioadmin" \
     -e "MINIO_SECRET_KEY=minioadmin" \
     -v /mydata/minio/data:/data \
     -v /mydata/minio/config:/root/.minio \
     minio/minio server \
     /data --console-address ":9090" -address ":9000"

minio下载安装_第4张图片

浏览器访问: http://虚拟机的ip:9090/login      

用户名: minioadmin

密码 :minioadmin

 测试代码

package com.itheima.education.media;

import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.UploadObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.util.StreamUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;

@Slf4j
public class MinioTest {

    static MinioClient minioClient = MinioClient.builder()
            //报错403 原因: 是配置文件里面用了Console 控制台的端口9090,应该使用API端口9000
            .endpoint("http://192.168.126.130:9000")
            .credentials("minioadmin", "minioadmin")
            .build();

    @Test
    public void uploadFile() {
        try {
            UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
                    .bucket("testbucket")
                    //对象名,同一个桶内不能重复
                    .object("minio_logo.jpg")
                    //本地文件所在路径
                    .filename("F:\\test_data\\minio_logo.jpg")
                    .build();
            minioClient.uploadObject(uploadObjectArgs);
            System.out.println("文件上传成功!");
        } catch (Exception e) {
            System.out.println("文件上传失败!" + e);
        }
    }

    @Test
    public void upload2() {
        try {
            UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
                    .bucket("testbucket")
                    //指定桶内的子目录
                    .object("test/hello_minio.jpg")
                    .filename("F:\\test_data\\minio_logo.jpg")
                    .build();
            minioClient.uploadObject(uploadObjectArgs);
            System.out.println("文件上传成功!");
        } catch (Exception e) {
            System.out.println("文件上传失败!");
        }
    }

    @Test
    public void delete() {
        try {
            RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
                    .bucket("testbucket")
                    .object("test/hello_minio.jpg")
                    .build();
            minioClient.removeObject(removeObjectArgs);
            System.out.println("文件删除成功!");
        } catch (Exception e) {
            System.out.println("文件删除失败!");
        }
    }

    @Test
    public void downLoadFile() throws Exception {
        GetObjectArgs getObjectArgs = GetObjectArgs.builder()
                .bucket("testbucket")
                .object("test/hello_minio.jpg")
                .build();

        FilterInputStream inputStream = null;
        FileOutputStream outputStream = null;

        try {
            inputStream = minioClient.getObject(getObjectArgs);
            if (inputStream != null) {
                //如果没有文件创建文件
                String directory = "F:" + File.separator + "download";
                String fileName = "minio.jpg";
                creatFile(directory, fileName);

               //往磁盘写入数据
                outputStream = new FileOutputStream(directory + File.separator + fileName);
                StreamUtils.copy(inputStream, outputStream);
            }
        } finally {
            if (outputStream != null) {
                outputStream.flush();
                outputStream.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

    //指定目录下创建文件
    public static void creatFile(String directory, String fileName) {
        File file = new File(directory, fileName);
        boolean isSuccess;
        if (!file.exists()) {
            try {
                isSuccess = file.createNewFile();
                log.info("新建文件是否成功[{}]", isSuccess);
            } catch (IOException e) {
                log.info("开始新建指定路径的目录");
                boolean isSucceed = mkdir(directory);
                if (isSucceed) {
                    creatFile(directory, fileName);
                }
            }
        }
    }

    //创建文件夹
    public static boolean mkdir(String directory) {
        boolean flag;
        File file = new File(directory);
        if (!file.exists()) {
            flag = file.mkdirs();
            log.info("新建目录是否成功[{}]", flag);
        } else {
            flag = true;
        }
        return flag;
    }

}

你可能感兴趣的:(运维,minio)