最近在学习minio,所以想让自己的springboot项目集成minio,在网上查阅资料,并进行操作的过程中遇到一些问题,所以想把自己遇到的坑和完成步骤记录下来供自己和各位查阅。
一. minio的下载安装以及基本使用
1. 下载地址:https://dl.min.io/server/minio/release/windows-amd64/minio.exe

2. 下载好后需要手动创建data文件夹用于存储minio中的数据。
3. 键入cmd

4. 设置minio的一些变量
set minio_root_user=admin set minio_root_password=admin123 set minio_access_key=admin set minio_secret_key=admin123
下面是我踩的坑,如果只设置了minio_root_user和minio_root_password的值,而不设置minio_access_key和minio_secret_key的值,当启动minio服务的时候就会报以下异常:

所以一定要设置好后面两个变量的值。
5. 启动minio服务
> minio.exe server data

启动后就会出现minio服务的地址,按住ctrl再点击即可访问该网址。
6.进入登录页面后,输入对应的用户名和密码,也就是之前设置的minio_root_user和minio_root_password的值。
7. 进入主界面后,点击左侧导航栏中的buckets,然后点击create bucket。


8. 创建好之后,点击左侧导航栏中的object browser就可以看到创建好的桶了。

9. 进入该桶,点击upload,上传一个文件,桶的默认权限是private,所以外界访问不到,需要修改访问权限为public,但是要注意安全问题。

10. 点击左侧导航栏中的buckets,进入该桶,修改权限为public,这样外界就可以访问上传的文件了。

二. springboot集成minio
1.引入依赖
<dependency> <groupid>io.minio</groupid> <artifactid>minio</artifactid> <version>7.0.2</version> </dependency>
2. 编写配置文件
server:
port: 8081
spring:
# 配置文件上传大小限制
servlet:
multipart:
max-file-size: 200mb
max-request-size: 200mb
minio:
host: http://127.0.0.1:9000
url: ${minio.host}/${minio.bucket}/
access-key: minioadmin
secret-key: minioadmin
bucket: public 3. 添加配置文件
package com.suwell.serv.ocr.component;
import io.minio.minioclient;
import io.minio.objectstat;
import io.minio.putobjectoptions;
import io.minio.result;
import io.minio.messages.bucket;
import io.minio.messages.item;
import org.springframework.beans.factory.initializingbean;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;
import org.springframework.util.assert;
import org.springframework.web.multipart.multipartfile;
import org.springframework.web.util.uriutils;
import javax.servlet.http.httpservletresponse;
import java.io.inputstream;
import java.net.urlencoder;
import java.nio.charset.standardcharsets;
import java.util.arraylist;
import java.util.list;
@component
public class minioconfig implements initializingbean {
@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;
private minioclient minioclient;
@override
public void afterpropertiesset() throws exception {
assert.hastext(url, "minio url 为空");
assert.hastext(accesskey, "minio accesskey为空");
assert.hastext(secretkey, "minio secretkey为空");
this.minioclient = new minioclient(this.host, this.accesskey, this.secretkey);
}
/**
* 上传
*/
public string putobject(multipartfile multipartfile) throws exception {
// bucket 不存在,创建
if (!minioclient.bucketexists(this.bucket)) {
minioclient.makebucket(this.bucket);
}
try (inputstream inputstream = multipartfile.getinputstream()) {
// 上传文件的名称
string filename = multipartfile.getoriginalfilename();
// 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);
}
}
/**
* 文件下载
*/
public void download(string filename, httpservletresponse response){
// 从链接中得到文件名
inputstream inputstream;
try {
minioclient minioclient = new minioclient(host, accesskey, secretkey);
objectstat stat = minioclient.statobject(bucket, filename);
inputstream = minioclient.getobject(bucket, filename);
response.setcontenttype(stat.contenttype());
response.setcharacterencoding("utf-8");
response.setheader("content-disposition", "attachment;filename=" + urlencoder.encode(filename, "utf-8"));
byte[] buffer = new byte[1024];
int length;
while ((length = inputstream.read(buffer)) > 0) {
response.getoutputstream().write(buffer, 0, length);
}
inputstream.close();
} catch (exception e){
e.printstacktrace();
system.out.println("有异常:" + e);
}
}
/**
* 列出所有存储桶名称
*
* @return
* @throws exception
*/
public list<string> listbucketnames()
throws exception {
list<bucket> bucketlist = listbuckets();
list<string> bucketlistname = new arraylist<>();
for (bucket bucket : bucketlist) {
bucketlistname.add(bucket.name());
}
return bucketlistname;
}
/**
* 查看所有桶
*
* @return
* @throws exception
*/
public list<bucket> listbuckets()
throws exception {
return minioclient.listbuckets();
}
/**
* 检查存储桶是否存在
*
* @param bucketname
* @return
* @throws exception
*/
public boolean bucketexists(string bucketname) throws exception {
boolean flag = minioclient.bucketexists(bucketname);
if (flag) {
return true;
}
return false;
}
/**
* 创建存储桶
*
* @param bucketname
* @return
* @throws exception
*/
public boolean makebucket(string bucketname)
throws exception {
boolean flag = bucketexists(bucketname);
if (!flag) {
minioclient.makebucket(bucketname);
return true;
} else {
return false;
}
}
/**
* 删除桶
*
* @param bucketname
* @return
* @throws exception
*/
public boolean removebucket(string bucketname)
throws exception {
boolean flag = bucketexists(bucketname);
if (flag) {
iterable<result<item>> myobjects = listobjects(bucketname);
for (result<item> result : myobjects) {
item item = result.get();
// 有对象文件,则删除失败
if (item.size() > 0) {
return false;
}
}
// 删除存储桶,注意,只有存储桶为空时才能删除成功。
minioclient.removebucket(bucketname);
flag = bucketexists(bucketname);
if (!flag) {
return true;
}
}
return false;
}
/**
* 列出存储桶中的所有对象
*
* @param bucketname 存储桶名称
* @return
* @throws exception
*/
public iterable<result<item>> listobjects(string bucketname) throws exception {
boolean flag = bucketexists(bucketname);
if (flag) {
return minioclient.listobjects(bucketname);
}
return null;
}
/**
* 列出存储桶中的所有对象名称
*
* @param bucketname 存储桶名称
* @return
* @throws exception
*/
public list<string> listobjectnames(string bucketname) throws exception {
list<string> listobjectnames = new arraylist<>();
boolean flag = bucketexists(bucketname);
if (flag) {
iterable<result<item>> myobjects = listobjects(bucketname);
for (result<item> result : myobjects) {
item item = result.get();
listobjectnames.add(item.objectname());
}
}
return listobjectnames;
}
/**
* 删除一个对象
*
* @param bucketname 存储桶名称
* @param objectname 存储桶里的对象名称
* @throws exception
*/
public boolean removeobject(string bucketname, string objectname) throws exception {
boolean flag = bucketexists(bucketname);
if (flag) {
list<string> objectlist = listobjectnames(bucketname);
for (string s : objectlist) {
if(s.equals(objectname)){
minioclient.removeobject(bucketname, objectname);
return true;
}
}
}
return false;
}
/**
* 文件访问路径
*
* @param bucketname 存储桶名称
* @param objectname 存储桶里的对象名称
* @return
* @throws exception
*/
public string getobjecturl(string bucketname, string objectname) throws exception {
boolean flag = bucketexists(bucketname);
string url = "";
if (flag) {
url = minioclient.getobjecturl(bucketname, objectname);
}
return url;
}
}4. 编写测试类进行测试
package com.suwell.serv.ocr.controller;
import com.suwell.serv.ocr.component.minioconfig;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.multipartfile;
import javax.servlet.http.httpservletresponse;
import java.util.list;
@restcontroller
@crossorigin
@requestmapping("/test")
public class miniocontroller {
@autowired
minioconfig minioconfig;
// 上传
@postmapping("/upload")
public object upload(@requestparam("file") multipartfile multipartfile) throws exception {
return this.minioconfig.putobject(multipartfile);
}
// 下载文件
@getmapping("/download")
public void download(@requestparam("filename")string filename, httpservletresponse response) {
this.minioconfig.download(filename,response);
}
// 列出所有存储桶名称
@postmapping("/list")
public list<string> list() throws exception {
return this.minioconfig.listbucketnames();
}
// 创建存储桶
@postmapping("/createbucket")
public boolean createbucket(string bucketname) throws exception {
return this.minioconfig.makebucket(bucketname);
}
// 删除存储桶
@postmapping("/deletebucket")
public boolean deletebucket(string bucketname) throws exception {
return this.minioconfig.removebucket(bucketname);
}
// 列出存储桶中的所有对象名称
@postmapping("/listobjectnames")
public list<string> listobjectnames(string bucketname) throws exception {
return this.minioconfig.listobjectnames(bucketname);
}
// 删除一个对象
@postmapping("/removeobject")
public boolean removeobject(string bucketname, string objectname) throws exception {
return this.minioconfig.removeobject(bucketname, objectname);
}
// 文件访问路径
@postmapping("/getobjecturl")
public string getobjecturl(string bucketname, string objectname) throws exception {
return this.minioconfig.getobjecturl(bucketname, objectname);
}
}5. 使用postman进行本地测试

6. 最后查看public桶中是否有刚才上传的文件就可以了。如果有则表明你的项目已经成功集成minio了。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论