springboot集成Minio对象存储

  sre

5fd3664b449db8309.jpg_e680.jpg

pom.xml

        <!-- https://mvnrepository.com/artifact/io.minio/minio -->
        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>7.0.2</version>
        </dependency>

application.yaml

minio:
  # bucket名称
  bucket: "java"
  # Minio服务地址
  host: "https://s3.my.com"
  # 访问路径
  url: "{minio.host}/{minio.bucket}/"
  # MINIO_ACCESS_KEY
  access-key: "1111111111111111111111111111111"
  # MINIO_SECRET_KEY
  secret-key: "2222222222222222222222222222222"

MinioHelper.java

import java.io.IOException;
import java.io.InputStream;
//import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.util.UriUtils;

import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidBucketNameException;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.RegionConflictException;
import io.minio.errors.XmlParserException;


@Component
public class MinioHelper {

    @Value(value = "{minio.bucket}")
    private String bucket;

    @Value(value = "{minio.host}")
    private String host;

    @Value(value = "{minio.url}")
    private String url;

    @Value(value = "{minio.access-key}")
    private String accessKey;

    @Value(value = "${minio.secret-key}")
    private String secretKey;

    SimpleDateFormat dataDir = new SimpleDateFormat("yyyy/MM/dd");

    public String putObject(MultipartFile multipartFile) throws InvalidEndpointException, InvalidPortException,
            IOException, InvalidKeyException, ErrorResponseException, IllegalArgumentException,
            InsufficientDataException, InternalException, InvalidBucketNameException, InvalidResponseException,
            NoSuchAlgorithmException, XmlParserException, RegionConflictException {
        MinioClient minioClient = new MinioClient(this.host, this.accessKey, this.secretKey);

        // bucket 不存在,创建
        if (!minioClient.bucketExists(this.bucket)) {
            minioClient.makeBucket(this.bucket);
        }
        try (InputStream inputStream = multipartFile.getInputStream()) {

            // 上传文件的OriginalFilename
            String fileName = multipartFile.getOriginalFilename();
            //添加日期目录
            String dir = dataDir.format(new Date());
            //获取文件后缀名
            String suffixName="";
            if(fileName.contains(".")){
                suffixName = fileName.substring(fileName.lastIndexOf("."));
            }

            //重新生成文件名
            fileName = dir+'/'+UUID.randomUUID()+suffixName;


            // PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
            PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
            // 文件的ContentType
            putObjectOptions.setContentType(multipartFile.getContentType());
            minioClient.putObject(this.bucket, fileName, inputStream, putObjectOptions);

            // 返回访问路径
            //return this.url + UriUtils.encode(fileName, StandardCharsets.UTF_8);
            return this.url + fileName;
        }
    }
}

TestController.java

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidBucketNameException;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.RegionConflictException;
import io.minio.errors.XmlParserException;
//import io.springboot.twitter.minio.MinioHelper;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    MinioHelper minioHelper;

    @PostMapping("/upload")
    public Object upload (@RequestParam("file") MultipartFile multipartFile) throws InvalidKeyException, InvalidEndpointException, InvalidPortException, ErrorResponseException, IllegalArgumentException, InsufficientDataException, InternalException, InvalidBucketNameException, InvalidResponseException, NoSuchAlgorithmException, XmlParserException, RegionConflictException, IOException {
        return this.minioHelper.putObject(multipartFile);
    }
}

LEAVE A COMMENT

Captcha Code