MinIO服务部署指南

目录

MinIO的下载与安装

Spring boot集成MinIO

一:添加MinIO的依赖

二:配置MinIO

三:创建配置类

 四:使用MinIO


MinIO是一个基于‌Apache License v2.0开源协议的对象存储服务‌,它兼容‌亚马逊S3云存储服务接口,非常适合存储大容量非结构化的数据,如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,文件大小可以从几KB到最大5TB不等

MinIO 官网地址:https://min.io/docs/minio/kubernetes/upstream/

MinIO Github地址:

GitHub - minio/minio: MinIO is a high-performance, S3 compatible object store, open sourced under GNU AGPLv3 license.MinIO is a high-performance, S3 compatible object store, open sourced under GNU AGPLv3 license. - minio/minioicon-default.png?t=O83Ahttps://github.com/minio/minio

MinIO的下载与安装

MinIO的下载地址:https://dl.min.io/server/minio/release/windows-amd64/minio.exe

建议下载到一个没有中文名字的文件夹下,因此我下载到D盘下的文件夹中,如下图:

文件下载好后,使用cmd命令进行到开即可

 在cmd窗口使用以下命令,即可打开MinIO

minio.exe server d:\MySoft\MinIO\data

当出现以下画面就表示已经登陆好了

在cmd窗口界面可以看到以下信息:

 因此,我们选择2的WebUI的地址,密码和用户名都是默认的minioadmin,当出现以下画面就表示登陆成功

以上是Windows版本的下载与安装方式,Linux版本大致类似,我将官方的文档放在下面,大家可以自行查看:https://min.io/docs/minio/linux/index.html 

Spring boot集成MinIO

一:添加MinIO的依赖

<dependencies>
    <!-- Spring Boot Starter for MinIO -->
    <dependency>
        <groupId>io.minio</groupId>
        <artifactId>minio</artifactId>
        <version>8.5.3</version>
    </dependency>
    <!-- Spring Boot Starter for Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

二:配置MinIO

在spring项目下的resource文件下application.yml配置MinIO

minio:
  endpoint: http://127.0.0.1:9000
  accessKey: YOUR_ACCESS_KEY
  secretKey: YOUR_SECRET_KEY
  bucket: your-bucket-name

关于配置文件中的accessKey,secretKey,bucket这三个参数,要在WebUI界面进行配置,如下:

1.在主页界面找到Buckets

2.点击Create Buckets创建即可看到三个配置信息,粘贴复制到响相应的配置文件即可

 

三:创建配置类

 在项目的config包下创建MinIO的配置类

@Configuration
public class MinioConfig {
    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;
    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }
}

 四:使用MinIO

在创建好配置类之后,便可以使用MinIO了,例如在Service以下使用

Service
public class MinioService {
    private final MinioClient minioClient;
    private final String bucketName;
    @Autowired
    public MinioService(MinioClient minioClient,
                        @Value("${minio.bucketName}") String bucketName) {
        this.minioClient = minioClient;
        this.bucketName = bucketName;
    }
    // 上传
    public String uploadFile(String objectName, InputStream stream, String contentType) throws Exception {
        boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!isExist) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
        // 保存图片
        minioClient.putObject(PutObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .stream(stream, -1, 10485760)
                .contentType(contentType)
                .build());
        // 返回 URL 地址
        return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                .bucket(bucketName)
                .object(fileName)
                .method(Method.GET)
        .build());
    }
    // 下载
    public InputStream downloadFile(String objectName) throws Exception {
        return minioClient.getObject(GetObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build());
    }
}

其中的 uploadFile 为文件的上传方法 ,downloadFile 为文件的下载方法。

在controller层可以去调用方法,如下:

@RestController
@RequestMapping("/minio")
public class MinioController {

    @Autowired
    private MinioService minioService;

    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            minioService.uploadFile(file.getOriginalFilename(), inputStream, file.getContentType());
            return ResponseEntity.ok("File uploaded successfully.");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to upload file.");
        }
    }

    @GetMapping("/download/{filename}")
    public ResponseEntity<Resource> downloadFile(@PathVariable String filename) throws IOException {
        try {
            InputStream stream = minioService.downloadFile(filename);
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
                    .body(new InputStreamResource(stream));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}

以上就是MinIO的服务部署指南。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dlwlrma-IU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值