MinIO安裝和使用

MinIO介紹

  • MinIO基于Apache License v2.0開源協(xié)議的對象存儲服務,可以做為云存儲的解決方案用來保存海量的圖片,視頻,文檔

  • 由于采用Golang實現(xiàn),服務端可以工作在Windows,Linux, OS X和FreeBSD上

  • 配置簡單,基本是復制可執(zhí)行程序,單行命令可以運行起來

  • MinIO兼容亞馬遜S3云存儲服務接口,非常適合于存儲大容量非結構化的數(shù)據(jù),例如圖片、視頻、日志文件、備份數(shù)據(jù)和容器/虛擬機鏡像等,而一個對象文件可以是任意大小,從幾kb到最大5T不等

分布式文件系統(tǒng)(存儲)方案

  • 付費云服務

    • 阿里云OSS
    • 七牛云存儲
    • 騰訊云存儲
    • 華為云存儲
  • 免費的私有化服務

    • 阿里的FastDFS
    • MinIO

MinIO的一些概念

  • bucket (桶)– 類比于文件系統(tǒng)的目錄
  • Object – 類比文件系統(tǒng)的文件
  • Keys – 類比文件名

官網(wǎng)文檔:http://docs.minio.org.cn/docs/

MinIO的特點

  • 數(shù)據(jù)保護

    • Minio使用Minio Erasure Code(糾刪碼)來防止硬件故障。即便損壞一半以上的driver,但是仍然可以從中恢復
  • 高性能

    • 作為高性能對象存儲,在標準硬件條件下它能達到55GB/s的讀、35GB/s的寫速率
  • 可擴容

    • 不同MinIO集群可以組成聯(lián)邦,并形成一個全局的命名空間,并跨越多個數(shù)據(jù)中心
  • SDK支持

    • 基于Minio輕量的特點,它得到類似Java、Python或Go等語言的sdk支持
  • 有操作頁面

    • 面向用戶友好的簡單操作界面,非常方便的管理Bucket及里面的文件資源
  • 功能簡單

    • 這一設計原則讓MinIO不容易出錯、更快啟動
  • 豐富的API

    • 支持文件資源的分享連接及分享鏈接的過期策略、存儲桶操作、文件列表訪問及文件上傳下載的基本功能等
  • 文件變化主動通知

    • 存儲桶(Bucket)如果發(fā)生改變,比如上傳對象和刪除對象,可以使用存儲桶事件通知機制進行監(jiān)控,并通過以下方式發(fā)布出去:AMQP、MQTT、Elasticsearch、Redis、NATS、MySQL、Kafka、Webhooks等

MinIO的安裝

下載鏡像

  • 安裝最新版
docker pull minio/minio
  • 安裝指定版本
docker pull minio/minio:RELEASE.2022-06-20T23-13-45Z.fips

安裝容器

  • 設置Minio的登錄賬號為minio,密碼為minio123
docker run -d -p 9000:9000 -p 50000:50000 --name minio -e "MINIO_ROOT_USER=minio" -e "MINIO_ROOT_PASSWORD=minio123" -v /home/data:/data -v /home/config:/root/.minio minio/minio server --console-address ":50000" /data

登錄管理后臺

  • Minio的后臺管理系統(tǒng)的端口為50000,ip + 端口,輸入上面的賬號和密碼,點擊登錄,即可進入登錄界面
http://192.168.253.133:50000

快速入門

基于SpringBoot工程,學習MinIO的使用

  • 添加MinIO依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zh</groupId>
    <artifactId>minio-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- 繼承Spring boot工程 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.8.RELEASE</version>
    </parent>

    <dependencies>
        <!-- MinIO依賴 -->
        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>7.1.0</version>
        </dependency>
        <!-- SpringBoot的Web啟動器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringBoot的測試啟動器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>
</project>
  • 啟動類
@SpringBootApplication
public class MinIOApplication {
    public static void main(String[] args) {
        SpringApplication.run(MinIOApplication.class, args);
    }
}
  • 新建測試類,定義MinIO地址、賬號、密碼的公共常量
public class MinioTest {
    /**
     * minio 服務器地址
     */
    private static final String MINIO_SERVER_ADDRESS = "http://192.168.253.133:9000";
    /**
     * 賬號
     */
    private final String ACCESS_KEY = "minio";
    /**
     * 密碼
     */
    private final String SECRET_KEY = "minio123";
}
  • 上傳文件
@Test
public void testUpload() throws Exception {
    //創(chuàng)建MinioClient對象
    MinioClient minioClient = MinioClient.builder()
            //服務器地址
            .endpoint(MINIO_SERVER_ADDRESS)
            //賬號和密碼
            .credentials(ACCESS_KEY, SECRET_KEY)
            .build();

    //準備文件流
    FileInputStream inputStream = new FileInputStream("c:/test/list-1.html");

    //準備上傳參數(shù)
    PutObjectArgs putObjectArgs = PutObjectArgs.builder()
            //bucket名稱
            .bucket("leadnews")
            //在bucket的名稱
            .object("list-test.html")
            //參數(shù)一:文件流 參數(shù)二:文件大小 參數(shù)三:部分大小,-1代表整個文件大小
            .stream(inputStream, inputStream.available(), -1)
            //文件類型
            .contentType("text/html")
            .build();

    //開始上傳
    minioClient.putObject(putObjectArgs);

    //關閉流
    inputStream.close();
}
  • 下載文件
@Test
public void testDownload() throws Exception {
    //創(chuàng)建MinioClient對象
    MinioClient minioClient = MinioClient.builder()
            //服務器地址
            .endpoint(MINIO_SERVER_ADDRESS)
            //賬號和密碼
            .credentials(ACCESS_KEY, SECRET_KEY)
            .build();

    GetObjectArgs getObjectArgs = GetObjectArgs.builder()
            .bucket("leadnews")
            .object("list-test.html")
            .build();

    //讀取minio中的文件,使用流
    InputStream inputStream = minioClient.getObject(getObjectArgs);

    //將文件寫出到磁盤
    FileOutputStream outputStream = new FileOutputStream("c:/test/list-test.html");

    //保存到磁盤
    byte[] buf = new byte[1024];
    int len;
    while ((len = inputStream.read(buf)) != -1) {
        outputStream.write(buf, 0, len);
    }

    //關閉流
    outputStream.close();
    inputStream.close();
}
  • 刪除文件
@Test
public void removeFile() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
    MinioClient minioClient = MinioClient.builder()
            //服務器地址
            .endpoint(MINIO_SERVER_ADDRESS)
            //賬號和密碼
            .credentials(ACCESS_KEY, SECRET_KEY)
            .build();

    RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
            //桶
            .bucket("leadnews")
            //文件夾
            .object("list-test.html")
            .build();

    //發(fā)起刪除
    minioClient.removeObject(removeObjectArgs);
}

封裝MinIO

MinIO的MinioClient應該是一個單例存放在IOC容器,MinIO的配置應該存放在配置文件中,但MinIO沒有提供SpringBoot的Starter啟動器依賴,所以需要自己封裝

  • 封裝MinIO配置屬性類,使用@ConfigurationProperties注解,定義一個前綴,在yml配置文件中統(tǒng)一配置的前綴,并且配置文件中的屬性,自動映射到配置屬性類的類屬性上
/**
 * 定義MinIO的參數(shù)屬性類
 */
@Data
//聲明為配置屬性類,加入IOC容器,只會加載yml文件中的指定,前綴下的屬性
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {
    /**
     * 賬戶名稱
     */
    private String accessKey;
    /**
     * 賬戶密碼
     */
    private String secretKey;
    /**
     * MinIO連接地址,例:http://192.168.253.133:9000
     */
    private String endpoint;
    /**
     * 桶名稱
     */
    private String bucket;
    /**
     * 訪問文件的地址,例:http://192.168.253.133:9000
     */
    private String readPath;
}
  • 封裝MinIO的配置類,導入MinioProperties配置屬性類,并聲明MinioClient到Spring的IOC容器中
/**
 * 初始MinIO連接對象配置類
 */
@Configuration
//加載配置屬性類
@EnableConfigurationProperties(MinioProperties.class)
public class MinIOConfiguration {
    /**
     * 注入屬性配置類
     */
    @Autowired
    private MinioProperties minIOConfigProperties;

    /**
     * 將Minio的客戶端對象,加入IOC容器
     */
    @Bean
    public MinioClient createMinioClient() {
        return MinioClient.builder()
                //用戶名和密碼
                .credentials(minIOConfigProperties.getAccessKey(), minIOConfigProperties.getSecretKey())
                //服務器地址
                .endpoint(minIOConfigProperties.getEndpoint())
                .build();
    }
}
  • 定義MinIO工具類,通過@Component注解,將工具類放入IOC容器,后面使用,就可以使用@Autowired注解進行注入該對象

  • uploadImgFile()方法,上傳圖片

  • uploadHtmlFile()方法,上傳HTML文件

  • downLoadFile()方法,下載文件

  • delete()方法,刪除文件

@Component
@Slf4j
public class MinIOFileStorageService {
    /**
     * Minio客戶端
     */
    @Autowired
    private MinioClient minioClient;

    /**
     * Minio配置屬性類
     */
    @Autowired
    private MinioProperties minIOConfigProperties;

    /**
     * 分隔符
     */
    private final static String separator = "/";

    /**
     * 拼接上年月日,生成文件地址,方便分類管理
     *
     * @param dirPath  目錄地址
     * @param filename yyyy/mm/dd/file.jpg
     * @return 新文件名
     */
    public String builderFilePath(String dirPath, String filename) {
        StringBuilder stringBuilder = new StringBuilder(50);
        if (!StringUtils.isEmpty(dirPath)) {
            stringBuilder.append(dirPath).append(separator);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String todayStr = sdf.format(new Date());
        stringBuilder.append(todayStr).append(separator);
        stringBuilder.append(filename);
        return stringBuilder.toString();
    }

    /**
     * 上傳圖片文件
     *
     * @param prefix      文件前綴
     * @param filename    文件名
     * @param inputStream 文件流
     * @return 文件全路徑
     */
    public String uploadImgFile(String prefix, String filename, InputStream inputStream) {
        String filePath = builderFilePath(prefix, filename);
        try {
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .object(filePath)
                    .contentType("image/jpg")
                    .bucket(minIOConfigProperties.getBucket()).stream(inputStream, inputStream.available(), -1)
                    .build();
            minioClient.putObject(putObjectArgs);
            StringBuilder urlPath = new StringBuilder(minIOConfigProperties.getReadPath());
            urlPath.append(separator + minIOConfigProperties.getBucket());
            urlPath.append(separator);
            urlPath.append(filePath);
            return urlPath.toString();
        } catch (Exception ex) {
            log.error("minio put file error.", ex);
            throw new RuntimeException("上傳文件失敗");
        }
    }

    /**
     * 上傳html文件
     *
     * @param prefix      文件前綴
     * @param filename    文件名
     * @param inputStream 文件流
     * @return 文件全路徑
     */
    public String uploadHtmlFile(String prefix, String filename, InputStream inputStream) {
        String filePath = builderFilePath(prefix, filename);
        try {
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .object(filePath)
                    .contentType("text/html")
                    .bucket(minIOConfigProperties.getBucket()).stream(inputStream, inputStream.available(), -1)
                    .build();
            minioClient.putObject(putObjectArgs);
            StringBuilder urlPath = new StringBuilder(minIOConfigProperties.getReadPath());
            urlPath.append(separator + minIOConfigProperties.getBucket());
            urlPath.append(separator);
            urlPath.append(filePath);
            return urlPath.toString();
        } catch (Exception ex) {
            log.error("minio put file error.", ex);
            ex.printStackTrace();
            throw new RuntimeException("上傳文件失敗");
        }
    }

    /**
     * 刪除文件
     *
     * @param pathUrl 文件全路徑
     */
    public void delete(String pathUrl) {
        String key = pathUrl.replace(minIOConfigProperties.getEndpoint() + "/", "");
        int index = key.indexOf(separator);
        String bucket = key.substring(0, index);
        String filePath = key.substring(index + 1);
        // 刪除Objects
        RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(bucket).object(filePath).build();
        try {
            minioClient.removeObject(removeObjectArgs);
        } catch (Exception e) {
            log.error("minio remove file error.  pathUrl:{}", pathUrl);
            e.printStackTrace();
        }
    }


    /**
     * 下載文件
     *
     * @param pathUrl 文件全路徑
     * @return 文件流
     */
    public byte[] downLoadFile(String pathUrl) {
        String key = pathUrl.replace(minIOConfigProperties.getEndpoint() + "/", "");
        int index = key.indexOf(separator);
        String bucket = key.substring(0, index);
        String filePath = key.substring(index + 1);
        InputStream inputStream = null;
        try {
            inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(minIOConfigProperties.getBucket()).object(filePath).build());
        } catch (Exception e) {
            log.error("minio down file error.  pathUrl:{}", pathUrl);
            e.printStackTrace();
        }

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while (true) {
            try {
                if (!((rc = inputStream.read(buff, 0, 100)) > 0)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            byteArrayOutputStream.write(buff, 0, rc);
        }
        return byteArrayOutputStream.toByteArray();
    }
}
  • 微服務添加MinIO配置
minio:
  accessKey: minio
  secretKey: minio123
  bucket: leadnews
  endpoint: http://192.168.253.133:9000
  readPath: http://192.168.253.133:9000
  • 添加MinIO配置類,掃描MinIO封裝類的包
/**
 * Minio配置類
 */
@Configuration
//掃包
@ComponentScan("com.zh.common.minio")
public class MinioConfig {
}
  • 測試
/**
 * Minio測試類
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ArticleApplication.class)
public class MinioTest {
    @Autowired
    private MinIOFileStorageService storageService;

    /**
     * 上傳文件
     */
    @Test
    public void testUploadFile() throws Exception {
        FileInputStream inputStream = new FileInputStream("c:/test/list-1.html");
        String url = storageService.uploadHtmlFile("", "list-article-1.html", inputStream);
        System.out.println(url);
    }

    /**
     * 下載文件
     */
    @Test
    public void testDownloadFile() throws Exception {
        String url = "http://192.168.253.133:9000/leadnews/2023/01/14/list-article-1.html";
        byte[] bytes = storageService.downLoadFile(url);
        System.out.println(bytes);
    }

    /**
     * 刪除文件
     */
    @Test
    public void testDeleteFile() throws Exception {
        String url = "http://192.168.253.133:9000/leadnews/2023/01/14/list-article-1.html";
        storageService.delete(url);
    }
}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容