天天看點

第一章 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