MinIO:入門


MinIO安裝

wget http://dl.minio.org.cn/server/minio/release/linux-amd64/minio
chmod +x minio
nohup ./minio server /data > minio.log 2>&1 &
cat minio.log

image-20210510133355103

默認的賬號密碼都是minioadmin,進入網址http://192.168.1.40:9000

image-20210510133448005

基本概念

MinIO 是一個基於Apache License v2.0開源協議的對象存儲服務。它兼容亞馬遜S3雲存儲服務接口,非常適合於存儲大容量非結構化的數據,例如圖片、視頻、日志文件、備份數據和容器/虛擬機鏡像等,而一個對象文件可以是任意大小,從幾kb到最大5T不等。

MinIO是一個非常輕量的服務,可以很簡單的和其他應用的結合,類似 NodeJS, Redis 或者 MySQL。

存儲桶可以簡單理解為“根文件夾”,每個存儲桶都是minio服務下的一個一級結點,其下可以有多個子文件夾。

對象在minio服務里每個存儲內容都是一個對象。

在分布式對象存儲中,存儲桶有一定的限制,具體可見Bucket restrictions and limitations - Amazon Simple Storage Service

存儲桶的命名也有一定的限制:

  • 存儲桶名稱的長度必須介於3到63個字符之間。
  • Bucket名稱只能由小寫字母、數字、點(.)和連字符(-)組成。
  • Bucket名稱必須以字母或數字開頭和結尾。
  • Bucket名稱不能格式化為IP地址(例如192.168.5.4)。
  • 在分區中,Bucket名稱必須是唯一的。分區是一組區域
  • 與amazons3傳輸加速一起使用的bucket名稱中不能有點(.)。

SpringBoot整合Minio

基本配置

其余依賴省略

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.2.1</version>
</dependency>

配置類:

/**
 * @author wen.jie
 * @date 2021/5/10 14:22
 */
@Data
@ConfigurationProperties(prefix = "minio")
public class MinIoProperties {

    private String endpoint;

    private String accessKey;

    private String secretKey;

}

/**
 * @author wen.jie
 * @date 2021/5/10 13:44
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(MinIoProperties.class)
public class MinIOConfig {

    @Bean
    public MinioClient minioClient(MinIoProperties minIoProperties){

        HttpUrl httpUrl = HttpUrl.get(minIoProperties.getEndpoint());
        MinioClient minioClient =
                MinioClient.builder()
                        .endpoint(httpUrl)
                        .credentials(minIoProperties.getAccessKey(), minIoProperties.getSecretKey())
                        .build();
        return minioClient;
    }

}

application.yml:

minio:
  endpoint: http://192.168.1.40:9000
  secret-key: minioadmin
  access-key: minioadmin

#設置請求文件大小,可自行修改
spring:
  servlet:
    multipart:
      max-file-size: 50MB
      max-request-size: 50MB

工具類

其實在MinioClient類中有各種操作文件對象的方法,並且有使用的例子,對照着封裝就行了。

@Component
public class MinIOUtil implements ApplicationContextAware {

    private static MinioClient minioClient;

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        MinIOUtil.applicationContext = applicationContext;
        MinIOUtil.minioClient = applicationContext.getBean(MinioClient.class);
    }


    /**
     * 查看存儲bucket是否存在
     * @param bucketName 存儲bucket
     * @return boolean
     */
    public static Boolean bucketExists(String bucketName) {
        Boolean found;
        try {
            found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return found;
    }

    /**
     * 創建存儲bucket
     * @param bucketName 存儲bucket名稱
     * @return Boolean
     */
    public static Boolean makeBucket(String bucketName) {
        try {
            minioClient.makeBucket(MakeBucketArgs.builder()
                    .bucket(bucketName)
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 刪除存儲bucket
     * @param bucketName 存儲bucket名稱
     * @return Boolean
     */
    public static Boolean removeBucket(String bucketName) {
        try {
            minioClient.removeBucket(RemoveBucketArgs.builder()
                    .bucket(bucketName)
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 文件上傳
     * @param file 文件
     * @param bucketName 存儲bucket
     * @return Boolean
     */
    public static Boolean upload(MultipartFile file, String bucketName) {
        try {
            PutObjectArgs objectArgs = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(file.getOriginalFilename())
                    .stream(file.getInputStream(),file.getSize(),-1)
                    .contentType(file.getContentType())
                    .build();
            //文件名稱相同會覆蓋
            minioClient.putObject(objectArgs);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 文件下載
     * @param bucketName 存儲bucket名稱
     * @param fileName 文件名稱
     * @param res response
     */
    public static void download(String bucketName, String fileName, HttpServletResponse res) {
        GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName)
                .object(fileName).build();
        try (GetObjectResponse response = minioClient.getObject(objectArgs)){
            byte[] buf = new byte[1024];
            int len;
            try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()){
                while ((len=response.read(buf))!=-1){
                    os.write(buf,0,len);
                }
                os.flush();
                byte[] bytes = os.toByteArray();
                res.setCharacterEncoding("utf-8");
                //設置強制下載不打開
                res.setContentType("application/force-download");
                res.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
                try (ServletOutputStream stream = res.getOutputStream()){
                    stream.write(bytes);
                    stream.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查看文件對象
     * @param bucketName 存儲bucket名稱
     * @return 存儲bucket內文件對象信息
     */
    public static List<MinIOFile> listObjects(String bucketName) {
        Iterable<Result<Item>> results = minioClient.listObjects(
                ListObjectsArgs.builder().bucket(bucketName).build());
        List<MinIOFile> files = new ArrayList<>();
        try {
            for (Result<Item> result : results) {
                Item item = result.get();
                MinIOFile minIOFile = new MinIOFile();
                minIOFile.setObjectName(URLDecoder.decode(item.objectName(), "utf-8"));
                minIOFile.setIsDir(item.isDir());
                minIOFile.setOwner(item.owner().displayName());
                minIOFile.setSize(item.size());
                minIOFile.setLastModified(item.lastModified().toString());
                files.add(minIOFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
        return files;
    }

    /**
     * 批量刪除文件對象
     * @param bucketName 存儲bucket名稱
     * @param objects 對象名稱集合
     */
    public static Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> objects) {
        List<DeleteObject> dos = objects.stream().map(DeleteObject::new).collect(Collectors.toList());
        return minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
    }

}

文件類:

@Data
@ToString
public class MinIOFile {

    private String objectName;

    private String lastModified;

    private String owner;

    private Long size;

    private Boolean isDir;
}

測試

@RestController
@RequestMapping(path = "/minio", produces = "text/html;charset=UTF-8")
public class MinIOController {

    @RequestMapping("/makeBucket/{bucketName}")
    public String makeBucket(@PathVariable String bucketName){
        Boolean aBoolean = MinIOUtil.makeBucket(bucketName);
        return aBoolean.toString();
    }

    @RequestMapping("/removeBucket/{bucketName}")
    public String removeBucket(@PathVariable String bucketName){
        Boolean aBoolean = MinIOUtil.removeBucket(bucketName);
        return aBoolean.toString();
    }

    @RequestMapping("/{bucketName}/upload")
    public String upload(@PathVariable String bucketName, MultipartFile file){
        Boolean upload = MinIOUtil.upload(file, bucketName);
        return upload.toString();
    }

    @RequestMapping("/listObjects/{bucketName}")
    public List<MinIOFile> listObjects(@PathVariable String bucketName){
        return MinIOUtil.listObjects(bucketName);
    }

    @RequestMapping("/download/{bucketName}/{objectName}")
    public void download(@PathVariable String bucketName, @PathVariable String objectName, HttpServletResponse response){
        MinIOUtil.download(bucketName,objectName,response);
    }

}

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>Title</title>
</head>
<body>
<form action="/minio/wjbucket/upload" method="post" enctype="multipart/form-data">
    <p><input type="file" name="file"></p>
    <p><input type="submit" value="submit"></p>
</form>
</body>
</html>

新版本安裝

2021/08/09補

因為新版本minio和老版本minio有比較大的變化(api端口和控制台端口不再由同一個端口提供),這里提供了基於docker安裝的方式

安裝命令如下:需要額外指定控制台端口為9001,且認證信息也發生了變化,老版本的已經過時

docker run -p 9000:9000 -p 9001:9001 --name minio \
  -e "MINIO_ROOT_USER=minioadmin" \
  -e "MINIO_ROOT_PASSWORD=minioadmin" \
  -v /mydata/minio/data:/data \
  -v /mydata/minio/config:/root/.minio \
  -d minio/minio server /data --console-address ":9001"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM