天天看点

第一章 MinIO介绍与安装

第一章 MinIO介绍与安装

MinIO是一个高性能对象存储方案,支持分布式部署,它的API可以与

Amazon S3

云存储服务兼容。

安装MinIO

为了简单的学习

MinIO

,这里通过Docker的方式来安装。对于生产环境,官方推荐使用集群的方式来安装。

docker run \
  -p 9000:9000 \
  -p 9001:9001 \
  -e "MINIO_ROOT_USER=minio" \
  -e "MINIO_ROOT_PASSWORD=minio12345" \
  quay.io/minio/minio server /data --console-address ":9001"
           

启动成功后,控制台会打印

API

(endpoint) 的地址和

console

地址。

WARNING: Detected Linux kernel version older than 4.0.0 release, there are some known potential performance problems with this kernel version. MinIO recommends a minimum of 4.x.x linux kernel version for best performance
API: http://172.17.0.2:9000  http://127.0.0.1:9000 

Console: http://172.17.0.2:9001 http://127.0.0.1:9001 

Documentation: https://docs.min.io

           

Maven依赖

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.3.7</version>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.0</version>
</dependency>
           

YML配置

minio:
  endpoint: http://172.17.0.2:9000 # API地址
  bucketName: bucket-demo1
  accessKey: minio
  secretKey: minio12345
           

创建MinIO操作服务类

package com.example.qiniuyundemo.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.minio.*;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Service
public class MinIoService implements FileUploader {

    @Value("${minio.endpoint}")
    protected String endpoint;

    @Value("${minio.bucketName}")
    protected String bucketName;

    @Value("${minio.accessKey}")
    protected String accessKey;

    @Value("${minio.secretKey}")
    protected String secretKey;

    @Autowired
    private ObjectMapper objectMapper;

    private MinioClient client;

    @PostConstruct
    public void init() {
        client = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }

    @Override
    public String upload(String filename, String key) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        ObjectWriteResponse response = client.uploadObject(
                UploadObjectArgs.builder()
                        .bucket(bucketName)
                        .object(System.currentTimeMillis()+".png")
                        .filename(filename)
                        .build()
        );
        String bucket = response.bucket();
        String region = response.region();
        String object = response.object();
        System.out.println(bucket + "-" + region + "-" + object);
        return null;
    }


    @Override
    public String getFile(String key) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        GetObjectResponse response = client.getObject(
                GetObjectArgs.builder()
                        .bucket(bucketName)
                        .object(key)
                        .build()
        );

        return String.join("/", endpoint, response.bucket(), response.object());
    }
}

           

单元测试

@SpringBootTest
class MinIoServiceTest {

    @Autowired
    private FileUploader fileUploader;

    @Test
    void upload() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String filename = "C:\\imgs\\mybatis1.png";
        fileUploader.upload(filename, null);
    }

    @Test
    void getFile() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String url = fileUploader.getFile("1647745868109.png");
        System.out.println(url);
    }
}
           

参考

  • MinIO Docker Quickstart Guide
  • 版权声明:本文为CSDN博主「buffeer」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

    原文链接:https://blog.csdn.net/buffeer/article/details/123610318