Minio部署,并集成spring-boot

一、参考说明

二、部署说明

部署方式种类等请参考官方文档,此处使用下载安装包直接在linux中启动方式

三、单机版本

export MINIO_ROOT_USER=admin
export MINIO_ROOT_PASSWORD=admin123
./minio server --address ':9002' --console-address ':9001' /data1

四、集群版本

1. 节点: 三节点

  • 192.168.44.131
  • 192.168.44.132
  • 192.168.44.133

2. 部署路径

/opt/minio

3. 设置访问认证

export MINIO_ROOT_USER=admin
export MINIO_ROOT_PASSWORD=admin123

4. 启动命令

部署三节点,每个节点四个盘

# 前台启动
./minio server --address ':9000' --console-address ':9001' http://192.168.44.131/data1 http://192.168.44.131/data2 http://192.168.44.131/data3 http://192.168.44.131/data4 http://192.168.44.132/data1 http://192.168.44.132/data2 http://192.168.44.132/data3 http://192.168.44.132/data4 http://192.168.44.133/data1 http://192.168.44.133/data2 http://192.168.44.133/data3 http://192.168.44.133/data4

5. 验证

任意地址登录
在这里插入图片描述

6. 安装nginx

./configure
make 
make install

如果有报错

ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1

nginx配置代理

upstream cos {
	server 192.168.44.131:9000;
	server 192.168.44.132:9000;
	server 192.168.44.133:9000;
}
server {
	listen       80;
	server_name  localhost;
	location / {
		proxy_set_header Host $http_host;
		proxy_pass http://cos;
	}
	error_page   500 502 503 504  /50x.html;
	location = /50x.html {
		root   html;
	}
}

7. 此时,就可以使用nginx代理Minio访问了

五、集成spring-boot

1. 引入依赖

<!-- 对象存储操作 -->
<dependency>
	<groupId>io.minio</groupId>
	<artifactId>minio</artifactId>
	<version>8.0.3</version>
</dependency>

2. 初始化MinioClient

配置文件

# 文件大小限制
spring.servlet.multipart.max-file-size=1024MB
spring.servlet.multipart.max-request-size=1024MB
# 对象存储地址
minio.endpoint=http://192.168.44.131
# 对象存储Key
minio.accessKey=admin
# 对象存储认证Key
minio.secretKey=admin123
# 是否使用https
minio.secure=false
# 对象存储地域
minio.region=nanjing
# 对象存储端口
minio.port=80

配置类

package com.synda.file.configuration;

import com.synda.file.configuration.properties.MinioProperties;
import io.minio.MinioClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * Minio配置类
 * @author hzy
 * @since 2021-12-30
 */
@Configuration
public class MinioConfiguration {

    @Resource
    private MinioProperties minioProperties;

    @Bean
    public MinioClient minioClient(){
        return MinioClient.builder()
                .endpoint(minioProperties.getEndpoint(),minioProperties.getPort(), minioProperties.isSecure())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .region(minioProperties.getRegion())
                .build();
    }
}

对象基础操作

package com.synda.file.minio.service;

import com.synda.file.common.entity.UploadResult;
import com.synda.file.common.utils.FileUtils;
import com.synda.file.common.utils.SnowflakeIdWorker;
import com.synda.file.configuration.properties.MinioProperties;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author hzy
 * @since 2022/1/1
 */
@Service
public class ObjectService {
    private static final Logger log = LoggerFactory.getLogger(ObjectService.class);
    @Resource
    private MinioClient minioClient;

    @Resource
    private MinioProperties minioProperties;

    public InputStream getObject(String bucketName, String objectName){
        try {
            return minioClient.getObject(GetObjectArgs.builder().bucket(getBucketName(bucketName)).object(objectName).build());
        }  catch (ErrorResponseException | InvalidResponseException | InsufficientDataException | InternalException | InvalidKeyException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
            e.printStackTrace();
            return null;
        }
    }

    public UploadResult saveObject(String bucketName, MultipartFile file) {

        log.debug("file upload cos start ......");
        try {
            String fileKey = SnowflakeIdWorker.generateId();
            PutObjectArgs build = PutObjectArgs.builder().bucket(getBucketName(bucketName)).object(FileUtils.getFileFolder()+fileKey ).stream(file.getInputStream(), file.getSize(),FileUtils.getUploadPartSize(file)).build();
            ObjectWriteResponse objectWriteResponse = minioClient.putObject(build);
            UploadResult uploadResult = new UploadResult();
            uploadResult.setFileKey(objectWriteResponse.object());
            uploadResult.setFileDirectory(FileUtils.getFileFolder());
            uploadResult.setFileName(file.getOriginalFilename());
            uploadResult.setFileSuffix(FileUtils.getFileSuffix(file.getOriginalFilename()));
            log.debug("file upload cos success ! ");
            return uploadResult;
        } catch (ErrorResponseException | InvalidResponseException | InsufficientDataException | InternalException | InvalidKeyException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
            log.error("file upload cos failed ! ");
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取文件下载连接
     */
    public String getObjectUrl(String bucketName,String objectName){
        if (! StringUtils.hasText(bucketName)){
            bucketName = minioProperties.getDefaultBucketName();
        }
        try {
            return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(getBucketName(bucketName)).object(objectName).method(Method.GET).build());
        }  catch (ErrorResponseException | InvalidResponseException | InsufficientDataException | InternalException | InvalidKeyException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 获取bucketName,如果没有传参,则使用配置文件配置的默认的
     * @param bucketName 存储桶
     * @return 存储桶
     */
    private String getBucketName(String bucketName){
        if (! StringUtils.hasText(bucketName)){
            bucketName = minioProperties.getDefaultBucketName();
        }
        return bucketName;
    }
}

存储桶基础操作

package com.synda.file.minio.service;

import com.synda.file.common.entity.baseenum.BaseEnum;
import com.synda.file.exception.BucketException;
import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.RemoveBucketArgs;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * 存储桶操作
 * @author hzy
 * @since 2021/12/31
 */
@Service
public class BucketService {

    @Resource
    private MinioClient minioClient;

    /**
     * 查看存储桶是否存在
     *
     * @param bucketName 存储桶名称
     * @return true=存在 false=不存在
     */
    public boolean bucketExists(String bucketName) {
        try {
            minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            return true;
        } catch (ErrorResponseException | InvalidResponseException | InsufficientDataException | InternalException | InvalidKeyException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 查询bucket列表
     */
    public List<Bucket> listBucket() {
        try {
            return minioClient.listBuckets();
        } catch (ErrorResponseException | InvalidResponseException | InsufficientDataException | InternalException | InvalidKeyException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    public boolean removeBucket(String bucketName ){
        try {
            checkBucket(bucketName);
            minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
            return true;
        } catch (ErrorResponseException | InvalidResponseException | InsufficientDataException | InternalException | InvalidKeyException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 检查bucket是否存在
     * @param bucketName bucketName
     */
    private void checkBucket(String bucketName){
        boolean exists = bucketExists(bucketName);
        if (!exists) throw  new BucketException(BaseEnum.BUCKET_IS_NOT_EXIST);
    }
}

3. 项目地址

gitee-file

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

synda@hzy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值