天天看點

使用MinIO搭建對象存儲服務 docker的具體使用

1.MinIO是什麼?

  • MinIO 是一款高性能、分布式的對象存儲系統. 它是一款軟體産品, 可以100%的運作在标準硬體。即X86等低成本機器也能夠很好的運作MinIO。
  • MinIO與傳統的存儲和其他的對象存儲不同的是:它一開始就針對性能要求更高的私有雲标準進行軟體架構設計。因為MinIO一開始就隻為對象存儲而設計。是以他采用了更易用的方式進行設計,它能實作對象存儲所需要的全部功能,在性能上也更加強勁,它不會為了更多的業務功能而妥協,失去MinIO的易用性、高效性。 這樣的結果所帶來的好處是:它能夠更簡單的實作局有彈性伸縮能力的原生對象存儲服務。
  • MinIO在傳統對象存儲用例(例如輔助存儲,災難恢複和歸檔)方面表現出色。同時,它在機器學習、大資料、私有雲、混合雲等方面的存儲技術上也獨樹一幟。當然,也不排除資料分析、高性能應用負載、原生雲的支援。

之前我使用的是阿裡雲OSS,想了解阿裡雲OSS的小夥伴參考SpringBoot整合阿裡雲OSS

2.我這裡使用的是docker安裝方式,使用的前提是小夥伴你已經安裝好了一個docker了,關于docker的具體使用可以參考docker的具體使用

使用MinIO搭建對象存儲服務 docker的具體使用

3.使用步驟

(1)下載下傳鏡像,下載下傳可能有點慢大家等待一下或者配置一下阿裡雲鏡像加速器

docker run -d -p 9000:9000 -p 9001:9001 --name minio -e MINIO_ACCESS_KEY=qbb -e MINIO_SECRET_KEY=startqbb -v /opt/minio/data:/data -v /opt/minio/config:/root/.minio minio/minio server /data --console-address ":9000" --address ":9001"

  • MINIO_ACCESS_KEY:指定的是"使用者名",可以這麼了解,後面我們檔案上傳需要使用
  • MINIO_SECRET_KEY:指定的是"密碼",可以這麼了解,後面我們檔案上傳需要使用
使用MinIO搭建對象存儲服務 docker的具體使用
下載下傳并運作完成
使用MinIO搭建對象存儲服務 docker的具體使用
注意:
  • 有可能下載下傳的過程中會出現如下圖,minio容器并沒有正常啟動

    docker ps -a

    :檢視所有容器
    使用MinIO搭建對象存儲服務 docker的具體使用
  • 我們可以使用

    docker logs 容器ID(CONTAINERID)

    檢視一下容器的日志
    使用MinIO搭建對象存儲服務 docker的具體使用
  • 可以看出數data目錄沒有權限,執行如下指令在啟動試試

    chmod 777 /opt/minio/data

    :設定目錄權限為可讀可寫可執行

    docker start 容器ID

    :啟動容器
    使用MinIO搭建對象存儲服務 docker的具體使用

(2)我們先在浏覽器通路一下

http://192.168.137.72:9000/

使用MinIO搭建對象存儲服務 docker的具體使用

(3)我們在minio中建立一個bucket

(4)接下來我們建立一個SpringBoot項目

使用MinIO搭建對象存儲服務 docker的具體使用

(5)導入相關的依賴

<?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>org.qbb</groupId>
    <artifactId>springboot-minio</artifactId>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.7.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>

        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!--swagger ui-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>8.3.0</version>
        </dependency>
    </dependencies>

</project>
           

(6)修改application.yml配置檔案

server:
  port: 7200

spring:
  application:
    name: minio

app:
  minio:
    endpoint: http://192.168.137.72:9001
    accessKey: qbb
    secretKey: startqbb
    bucket: qbb
           

注意:9000是我們浏覽器通路控制台的端口,而9001是SDK代碼操作的端口

(7)主啟動類

package com.qbb.minio;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author QiuQiu&LL (個人部落格:https://www.cnblogs.com/qbbit)
 * @version 1.0
 * @date 2022-05-20  15:45
 * @Description:
 */
@SpringBootApplication
public class MinioApplication {
    public static void main(String[] args) {
        SpringApplication.run(MinioApplication.class, args);
    }
}
           

(8)這裡我配置一個swagger2友善測試,還配置了一個minio的配置類綁定配置檔案資訊,具體配置如下

package com.qbb.minio.config;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author QiuQiu&LL (個人部落格:https://www.cnblogs.com/qbbit)
 * @version 1.0
 * @date 2022-05-20  16:23
 * @Description:swagger配置類
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket adminApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("minioApi")
                .apiInfo(adminApiInfo())
                .select()
                //隻顯示admin路徑下的頁面
                .paths(Predicates.and(PathSelectors.regex("/minio/.*")))
                .build();
    }

    private ApiInfo adminApiInfo() {

        return new ApiInfoBuilder()
                .title("minio-API文檔")
                .version("1.0")
                .contact(new Contact("QIUQIU&LL", "https://www.cnblogs.com/qbbit", "[email protected]"))
                .build();
    }
}



package com.qbb.minio.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @author QiuQiu&LL (個人部落格:https://www.cnblogs.com/qbbit)
 * @version 1.0
 * @date 2022-05-20  18:57
 * @Description:
 */
@ConfigurationProperties(prefix = "app.minio")
@EnableConfigurationProperties
@Configuration
@Data
public class MinioConfig {

    private String endpoint;
    private String accessKey;
    private String secretKey;
    private String bucket;
}

           

(9)編寫測試代碼

package com.qbb.minio.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author QiuQiu&LL (個人部落格:https://www.cnblogs.com/qbbit)
 * @version 1.0
 * @date 2022-05-20  16:23
 * @Description:
 */
@Api(tags = "minio檔案管理")
@RestController
@RequestMapping("/minio")
public class FileController {

    @ApiOperation("測試程式")
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

}
           
使用MinIO搭建對象存儲服務 docker的具體使用

(10)接下來我們編寫檔案上傳代碼

package com.qbb.minio.controller;

import com.qbb.minio.config.MinioConfig;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;


/**
 * @author QiuQiu&LL (個人部落格:https://www.cnblogs.com/qbbit)
 * @version 1.0
 * @date 2022-05-20  16:23
 * @Description:
 */
@Api(tags = "minio檔案管理")
@RestController
@RequestMapping("/minio")
public class FileController {

    @Autowired
    private MinioConfig minioConfig;

    @ApiOperation("測試程式")
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @ApiOperation("檔案上傳")
    @PostMapping("/fileUpload")
    public String fileUpload(MultipartFile file) {
        /**
         * 這裡應該放在service層處理的,将上傳的檔案路徑存入資料庫,我就不這麼麻煩了,直接傳回給前端
         */
        String bucket = minioConfig.getBucket();
        String endpoint = minioConfig.getEndpoint();
        String accessKey = minioConfig.getAccessKey();
        String secretKey = minioConfig.getSecretKey();

        // 1.擷取檔案名
        String originalFilename = file.getOriginalFilename();

        // 2.修改檔案名,防止上傳重複檔案名,導緻檔案覆寫
        String fileName = UUID.randomUUID().toString().replace("-", "") + "_" + originalFilename;
        // 檔案流
        try (InputStream inputStream = file.getInputStream()) {
            // 3.使用MinIO服務的URL,端口,Access key和Secret key建立一個MinioClient對象
            MinioClient minioClient = new MinioClient(endpoint, accessKey, secretKey);

            // 4.檢查存儲桶是否已經存在
            boolean isExist = minioClient.bucketExists(bucket);
            if (isExist) {
                System.out.println("Bucket already exists");
            } else {
                // 不存在則建立一個名為bucket的存儲桶,用于存儲照片的zip檔案。
                minioClient.makeBucket(bucket);
            }
            // 6.上傳參數設定項
            PutObjectOptions options = new PutObjectOptions(inputStream.available(), -1);

            // 7.設定此次上傳的檔案的内容類型
            String contentType = file.getContentType();
            options.setContentType(contentType);

            // 8.上傳
            minioClient.putObject(bucket, fileName, inputStream, options);

            // 9.通路路徑
            String url = endpoint + "/" + bucket + "/" + fileName;
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

           

(11)使用Swagger測試一下

使用MinIO搭建對象存儲服務 docker的具體使用

(12)結果

  • swagger
    使用MinIO搭建對象存儲服務 docker的具體使用
  • minio控制台
    使用MinIO搭建對象存儲服務 docker的具體使用

(13)删除minio中的檔案

@ApiOperation("删除檔案")
    @DeleteMapping("/remove")
    public boolean remove() {
        try {
            // 使用MinIO服務的URL,端口,Access key和Secret key建立一個MinioClient對象
            MinioClient minioClient = new MinioClient(minioConfig.getEndpoint(), minioConfig.getAccessKey(), minioConfig.getSecretKey());
            // 從bucket中删除檔案(檔案名)。
            minioClient.removeObject(minioConfig.getBucket(), "ed8aa56a8ec640d6a36a271ed4222605_xiaomi.png");
            System.out.println("successfully removed mybucket/myobject");
            return true;
        } catch (Exception e) {
            System.out.println("Error: " + e);
            return false;
        }
    }
           

(14)删除後的結果

  • swagger
    使用MinIO搭建對象存儲服務 docker的具體使用
  • minio控制台
    使用MinIO搭建對象存儲服務 docker的具體使用

(15)注意:我們配置完了,啟動我們的項目可能會出現這麼一個錯誤,說我們的請求時間與伺服器時間相差太大

使用MinIO搭建對象存儲服務 docker的具體使用

(16)解決辦法,linux控制台

yum -y install ntp ntpdate

:安裝插件工具

hwclock --systohc

:同步時間

docker restart minio鏡像ID

:重新開機鏡像

(17)相關代碼已上傳GitHub代碼倉庫,小夥伴們可以參考參考SpringBoot-Minio

至此SpringBoot整合Minio實作檔案上傳完成,更多詳細的功能配置各位小夥伴可以參考minio官方