@TOC
本文只介绍如何将minio做成服务
1.将minio做成服务
- 将MinioServer.exe放在minio安装目录中
- 同目录下创建MinioServer.xml。特别注意,xml和exe必须同名 配置MinioServer.xml文件
<service>
<id>minioServer</id>
<name>minioServer</name>
<description>minio文件存储服务器</description>
<!-- 可设置环境变量 -->
<env name="HOME" value="%BASE%"/>
<executable>%BASE%\minio.exe</executable>
<arguments>server "%BASE%\minIO"</arguments>
<!-- <logmode>rotate</logmode> -->
<logpath>%BASE%\logs</logpath>
<log mode="roll-by-size-time">
<sizeThreshold>10240</sizeThreshold>
<pattern>yyyyMMdd</pattern>
<autoRollAtTime>00:00:00</autoRollAtTime>
<zipOlderThanNumDays>5</zipOlderThanNumDays>
<zipDateFormat>yyyyMMdd</zipDateFormat>
</log>
</service>启动服务
如下图所示:
- 在当前目录启动cmd安装服务
MinioServer.exe install
- 执行成功后, 在服务中查看并启动。
- 访问http://127.0.0.1:9000
- 默认端口:9000 Access Key:minioadmin SecretKey:minioadmin
- 创建桶
- 访问文件地址 ip:9000/bucketName/fileName 如访问以下图片
- 访问 i p : 9000
默认端口 : 9000 Access Key:minioadmin SecretKey : minioadmin
(线上环境建议修改账号密码) - 创建桶 .
- 设置桶
- 卸载服务命令
MinioServer.exe uninstall
- 上文安装文件,
有需要的请留言
2.springboot集成minio
1.引入依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.1.0</version>
</dependency>
2.配置application
#服务器ip地址
minio.endpoint=192.168.1.209
#端口号
minio.port=9000
#如果是true,则用的是https而不是http,默认值是true
minio.secure=false
#桶
minio.bucketName=image
#accessKey
minio.accessKey=minioadmin
#secretKey
minio.secretKey=minioadmin
3.MinioConfig.java
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Data
@Component
public class MinioConfig {
@Value("${minio.endpoint}")
private String endpoint;
@Value("${minio.port}")
private int port;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Value("${minio.secure}")
private Boolean secure;
@Value("${minio.endpoint}")
private String bucketName;
@Bean
public MinioClient getMinioClient() throws InvalidEndpointException, InvalidPortException {
MinioClient minioClient = new MinioClient(endpoint, port, accessKey, secretKey,secure);
return minioClient;
}
}
MinIoUtils.java
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class MinIoUtils {
@Autowired
private MinioClient minioClient;
private static final int DEFAULT_EXPIRY_TIME = 7 * 24 * 3600;
/**
* 检查存储桶是否存在
*
* @param bucketName 存储桶名称
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public boolean bucketExists(String bucketName) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
boolean flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName)
.build());
if (flag) {
return true;
}
return false;
}
/**
* 创建存储桶
*
* @param bucketName 存储桶名称
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
* @throws RegionConflictException
*/
public boolean makeBucket(String bucketName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException, RegionConflictException {
boolean flag = bucketExists(bucketName);
if (!flag) {
minioClient.makeBucket(
MakeBucketArgs.builder()
.bucket(bucketName)
.build());
return true;
} else {
return false;
}
}
/**
* 列出所有存储桶名称
*
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public List<String> listBucketNames() throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
List<Bucket> bucketList = listBuckets();
List<String> bucketListName = new ArrayList<>();
for (Bucket bucket : bucketList) {
bucketListName.add(bucket.name());
}
return bucketListName;
}
/**
* 列出所有存储桶
*
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public List<Bucket> listBuckets() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
return minioClient.listBuckets();
}
/**
* 删除存储桶
*
* @param bucketName 存储桶名称
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public boolean removeBucket(String bucketName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
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(RemoveBucketArgs.builder().bucket(bucketName).build());
flag = bucketExists(bucketName);
if (!flag) {
return true;
}
}
return false;
}
/**
* 列出存储桶中的所有对象名称
*
* @param bucketName 存储桶名称
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public List<String> listObjectNames(String bucketName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
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 存储桶名称
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public Iterable<Result<Item>> listObjects(String bucketName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
boolean flag = bucketExists(bucketName);
if (flag) {
return minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
}
return null;
}
/**
* 通过InputStream上传对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param stream 要上传的流
* @param contentType 上传文件的格式,若是不设定格式默认都是application/octet-stream, 浏览器访问直接下载
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public boolean putObject(String bucketName, String objectName, InputStream stream, String contentType) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
boolean flag = bucketExists(bucketName);
if (flag) {
minioClient.putObject( PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(
stream, stream.available(), -1)
.contentType(contentType)
.build());
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
return true;
}
}
return false;
}
/**
* 以流的形式获取一个文件对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public InputStream getObject(String bucketName, String objectName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
boolean flag = bucketExists(bucketName);
if (flag) {
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
return stream;
}
}
return null;
}
/**
* 以流的形式获取一个文件对象(断点下载)
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param offset 起始字节的位置
* @param length 要读取的长度 (可选,如果无值则代表读到文件结尾)
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public InputStream getObject(String bucketName, String objectName, long offset, Long length) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
boolean flag = bucketExists(bucketName);
if (flag) {
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
return stream;
}
}
return null;
}
/**
* 下载并将文件保存到本地
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param fileName File name
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public boolean getObject(String bucketName, String objectName, String fileName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
boolean flag = bucketExists(bucketName);
if (flag) {
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
return true;
}
}
return false;
}
/**
* 删除一个对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public boolean removeObject(String bucketName, String objectName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
boolean flag = bucketExists(bucketName);
if (flag) {
minioClient.removeObject(RemoveObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
return true;
}
return false;
}
/**
* 删除指定桶的多个文件对象,返回删除错误的对象列表,全部删除成功,返回空列表
*
* @param bucketName 存储桶名称
* @param objects 含有要删除的多个object名称的迭代器对象
* @return
* @throws InvalidKeyException
* @throws ErrorResponseException
* @throws IllegalArgumentException
* @throws InsufficientDataException
* @throws InternalException
* @throws InvalidBucketNameException
* @throws InvalidResponseException
* @throws NoSuchAlgorithmException
* @throws XmlParserException
* @throws IOException
*/
public List<String> removeObject(String bucketName, List<DeleteObject> objects) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
List<String> deleteErrorNames = new ArrayList<>();
boolean flag = bucketExists(bucketName);
if (flag) {
Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder()
.bucket(bucketName)
.objects(objects)
.build());
for (Result<DeleteError> result : results) {
DeleteError error = result.get();
deleteErrorNames.add(error.objectName());
}
}
return deleteErrorNames;
}
/**
* 生成一个给HTTP GET请求用的presigned URL。
* 浏览器/移动端的客户端可以用这个URL进行下载,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
* @throws InvalidExpiresRangeException
*/
public String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException, InvalidExpiresRangeException {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
throw new InvalidExpiresRangeException(expires,
"expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
}
url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket(bucketName)
.object(objectName)
.expiry(expires)
.build());
}
return url;
}
/**
* 生成一个给HTTP PUT请求用的presigned URL。
* 浏览器/移动端的客户端可以用这个URL进行上传,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
* @return
* @throws InvalidKeyException
* @throws ErrorResponseException
* @throws IllegalArgumentException
* @throws InsufficientDataException
* @throws InternalException
* @throws InvalidBucketNameException
* @throws InvalidResponseException
* @throws NoSuchAlgorithmException
* @throws XmlParserException
* @throws IOException
* @throws InvalidExpiresRangeException
*/
public String getPresignedObjectGetPutUrl(String bucketName, String objectName, Integer expires) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException, InvalidExpiresRangeException {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
throw new InvalidExpiresRangeException(expires,
"expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
}
Map<String, String> reqParams = new HashMap<String, String>();
reqParams.put("response-content-type", "application/json");
url = minioClient.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.PUT)
.bucket(bucketName)
.object(objectName)
.expiry(expires)
.extraQueryParams(reqParams)
.build());
}
return url;
}
/**
* 获取对象的元数据
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public ObjectStat statObject(String bucketName, String objectName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
boolean flag = bucketExists(bucketName);
if (flag) {
ObjectStat statObject = minioClient.statObject(StatObjectArgs.builder()
.bucket(bucketName)
.object(objectName).build());
return statObject;
}
return null;
}
/**
* 文件访问路径
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
* @throws IOException
* @throws XmlParserException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
*/
public String getObjectUrl(String bucketName, String objectName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
url = minioClient.getObjectUrl(bucketName, objectName);
}
return url;
}
/**
* 设定存储桶策略
* @param bucketName
* @param policyJson
* @throws IOException
* @throws InvalidKeyException
* @throws InvalidResponseException
* @throws InsufficientDataException
* @throws NoSuchAlgorithmException
* @throws ServerException
* @throws InternalException
* @throws XmlParserException
* @throws InvalidBucketNameException
* @throws ErrorResponseException
*/
public void setBucketPolicy(String bucketName, String policyJson) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
minioClient.setBucketPolicy(
SetBucketPolicyArgs.builder()
.bucket(bucketName)
.config(policyJson)
.build());
}
}
MinIOController.java
import com.jinmdz.fmis.api.api.service.MinIOService;
import com.jinmdz.fmis.api.base.BaseController;
import com.jinmdz.fmis.api.model.electronic.ElectronicItem;
import com.jinmdz.fmis.core.base.BaseResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
/**
* @author lvyq
* @version 1.0
* @description: file上传
* @date 2021/7/13 16:27
*/
@RestController
@RequestMapping("/minIo")
public class MinIOController extends BaseController {
@Resource
private MinIOService minIOService;
/**
* @description: 保存文件到桶
* @author: lvyq
* @date: 2021/7/13 17:55
* @version 1.0
*/
@PostMapping("/saveFile")
public BaseResult saveFile(@RequestParam("file") MultipartFile file) throws Exception{
return minIOService.saveFile(file);
}
MinIOService.java
@Service标签:集成,return,springboot,InvalidKeyException,String,IOException,bucketName,throws,mi From: https://blog.51cto.com/lvyq/5763696
public class MinIOService {
@Resource
private MinIoUtils minioUtil;
@Value("${minio.bucketName}")
private String bucketName;
/**
* @description:保存遗体抓拍-pad
* @author: lvyq
* @date: 2021/7/14 8:56
* @version 1.0
*/
public BaseResult saveFile(MultipartFile file) throws Exception {
Map<String,Object> map = new HashMap<>();
if (!minioUtil.bucketExists(bucketName)) {
minioUtil.makeBucket(bucketName);
}
String fileName = file.getOriginalFilename();
String newName = DateUtil.formatPattern10(new Date()) + "/" + UUID.randomUUID().toString().replaceAll("-", "") + fileName.substring(fileName.lastIndexOf("."));
InputStream inputStream = file.getInputStream();
minioUtil.putObject(bucketName, newName, inputStream, file.getContentType());
inputStream.close();
String url = minioUtil.getObjectUrl(bucketName, newName);
map.put("filePath",newName);
map.put("url",url);
return successData(map);
}
}