开发者

Springboot集成Minio实现文件上传基本步骤

目录
  • 一 需要用到的有关Minio的核心概念
  • 二 Springboot集成Minio基本步骤
    • 1 添加相javascript关依赖
    • 2 配置Minio的连接信息
    • 3 创建Minio的配置类
    • 4 (服务层)创建Minio的服务类
    • 5 (表现层)创建控制器

一 需要用到的有关Minio的核心概念

  • 存储桶(Bucket):类似于文件系统中的顶级目录,需提前创建。
  • 对象(Object):存储在 MinIO 中的文件,每个对象有唯一的键(Key)。
  • 预签名 URL:临时授权的 URL,可控制访问权限和有效期。

二 SpriwNDuSngboot集成Minio基本步骤

1 添加相关依赖

        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>8.2.1</version>
            <exclusions>
                <exclusion>
                    <groupId>com.squareup.okhttp3</groupId>
                    <artifactId>okhttp</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-stdlib</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.9.0</version>python;
        </dependency>

2 配置Minio的连接信息

在application.yml中添加配置:

yaml
minio:
  endpoint: http://localhost:9000  # MinIO服务地址
  Access-key: your-access-key        # 访问密钥
  secret-key: your-secret-key        # 秘密密钥
  bucket-name: your-bucket-jsname      # 默认存储桶名称

3 创建Minio的配置类

import io.minio.MinioCjavascriptlient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MinIOConfig {
    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.access-key}")
    private String accessKey;
    @Value("${minio.secret-key}")
    private String secretKey;
    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }
}

4 (服务层)创建Minio的服务类

主要实现文件上传,下载操作

public String upload(MultipartFile file) throws Exception {
        // 检查存储桶是否存在,不存在则创建
        boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!isExist) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    // 生成唯一文件名,避免中文和空格
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + "." +
                originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        // 1. 上传文件到MinIO
        minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(fileName)
                        .stream(file.getInputStream(), file.getSize(), -1)
                        .contentType(file.getContentType())
                        .build()
        );
        return  url + "/" + bucketName + "/" + fileName;
    }

5 (表现层)创建控制器

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import Java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@RestController
public class FileUploadController {
    @Autowired
    private MinIOServiceImpl minioService;

到此这篇关于Springboot集成Minio实现文件上传的文章就介绍到这了,更多相关Springboot Minio文件上传内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜