解决springboot2.X整合minio出现SSLException: Unrecognized SSL message问题

28 篇文章 1 订阅
16 篇文章 1 订阅

各种排查异常后,将之前配置的 secure参数由true改成了false,使用给定的endpoint、port、access key、secret key和一个secure选项(表示的是是否使用https)创建一个Minio client对象。
1.pom依赖

   <!-- minio-->
        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>3.0.10</version>
        </dependency>

2.配置minio

import 
import org.springframework.stereotype.Component;
@Component
public class MinIoConfig {
   /**
    * minio 服务地址 http://ip:port
    */
   private String endpoint="http://127.0.0.1:9000";
   /**
    * 用户名
    */
   private String accessKey="your accessKey";
   /**
    * 密码
    */
   private String secretKey="your secretKey ";
   /**
    * 桶名称
    */
   private String bucketName;

   private boolean secure=false;

   public boolean isSecure() {
   	return secure;
   }

   public void setSecure(boolean secure) {
   	this.secure = secure;
   }

   public String getEndpoint() {
   	return endpoint;
   }

   public void setEndpoint(String endpoint) {
   	this.endpoint = endpoint;
   }

   public String getAccessKey() {
   	return accessKey;
   }

   public void setAccessKey(String accessKey) {
   	this.accessKey = accessKey;
   }

   public String getSecretKey() {
   	return secretKey;
   }

   public void setSecretKey(String secretKey) {
   	this.secretKey = secretKey;
   }

   public String getBucketName() {
   	return bucketName;
   }

   public void setBucketName(String bucketName) {
   	this.bucketName = bucketName;
   }
}

3.工具类

import io.minio.MinioClient;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.xmlpull.v1.XmlPullParserException;

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

@Component
@Configuration
public class MinIoUtils {
	@Autowired
	private MinIoConfig minIo;
	public MinIoUtils(MinIoConfig minIo) {
		this.minIo = minIo;
	}

	private MinioClient instance;

	public MinIoUtils() {
	}

	@PostConstruct
	public void init() {
		try {
			instance = new MinioClient(minIo.getEndpoint(), minIo.getAccessKey(), minIo.getSecretKey(),minIo.isSecure());
		} catch (InvalidPortException e) {
			e.printStackTrace();
		} catch (InvalidEndpointException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 判断 bucket是否存在
	 *
	 * @param bucketName
	 * @return
	 */
	public boolean bucketExists(String bucketName) {
		try {
			return instance.bucketExists(bucketName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * 创建 bucket
	 *
	 * @param bucketName
	 */
	public void makeBucket(String bucketName) {
		try {
			boolean isExist = instance.bucketExists(bucketName);
			if (!isExist) {
				instance.makeBucket(bucketName);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 文件上传
	 *
	 * @param bucketName
	 * @param objectName
	 * @param filename
	 */
	public void putObject(String bucketName, String objectName, String filename) {
		try {
			instance.putObject(bucketName, objectName, filename,null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 文件上传
	 *
	 * @param bucketName
	 * @param objectName
	 * @param stream
	 */
	public void putObject(String bucketName, String objectName, InputStream stream) {
		try {
			instance.putObject(bucketName, objectName, stream, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除文件
	 *
	 * @param bucketName
	 * @param objectName
	 */
	public void removeObject(String bucketName, String objectName) {
		try {
			instance.removeObject(bucketName, objectName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void putObject(String bucketName, String fileName, InputStream inputStream, String contentType) {
		try {
			instance.putObject(bucketName, fileName, inputStream, contentType);
		} catch (InvalidBucketNameException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (InsufficientDataException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoResponseException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (ErrorResponseException e) {
			e.printStackTrace();
		} catch (InternalException e) {
			e.printStackTrace();
		} catch (InvalidArgumentException e) {
			e.printStackTrace();
		}
	}
}

4.controller层

@ApiOperation(value = "使用minio文件上传")
	@PostMapping(value = "/uploadFile")
	public Result uploadFile(@RequestParam("file")MultipartFile file,@RequestParam @ApiParam(name = "bucketName", value = "对象存储桶名称") String bucketName) {
		try {
			String filename = uploadFileService.uploadFile(file, bucketName);
			return Result.success(filename);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("上传失败");
		}
	}

5.实现类

@Override
	public String uploadFile(MultipartFile file, String bucketName) throws Exception {
		String s = null;
		try {
			String filename = file.getOriginalFilename();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			// 文件存储的目录结构
			String objectName = sdf.format(new Date()) + ":" + filename;
			// 存储文件
			InputStream is = file.getInputStream();
			minIoUtils.putObject(bucketName, filename, file.getInputStream(), file.getContentType());
			log.info("文件上传成功!");
			s = minIoConfig.getEndpoint() + "/" + bucketName + "/" + objectName;
			return s;
		} catch (Exception e) {
			log.info("上传发生错误: {}!", e.getMessage());
			throw new MyException("上传失败");
		}
	}

上传成功
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值