minio-starter 使用说明
该组件作为web通用组件使用,可引入web项目模块中,目前仅添加swagger-starter功能,后续会添加关于web的其他功能。
1、使用说明
在pom.xml中引入依赖:
<dependency>
<groupId>com.pcitc.si</groupId>
<artifactId>common-minio-starter</artifactId>
</dependency>
2、应用配置说明
属性字段 | 是否必填 | 默认值 | 说明 |
---|---|---|---|
minio.enable | 否 | false | 是否开启minio,默认false |
minio.url | 是 | 空 | minio API地址 |
minio.accessKey | 是 | 空 | minio的用户名 |
minio.secretKey | 是 | 空 | minio的密码 |
minio.bucket | 是 | 空 | 默认 桶 名称 |
minio.createBucket | 否 | true | 当桶不存在,是否创建 |
minio.checkBucket | 否 | true | 启动检测桶,是否存在 |
minio.connectTimeout | 否 | 0 | 连接超时时间 (毫秒) 0代表没有超时 |
minio.writeTimeout | 否 | 0 | HTTP写超时,以毫秒为单位, 0代表没有超时 |
minio.readTimeout | 否 | 0 | HTTP读取超时,以毫秒为单位, 0代表没有超时 |
minio.fileTypeLimit | 否 | 空 | 文件类型限制,多个类型间中,号间隔,如:png,pdf(如不限制,请不要配置此参数) |
minio.fileSizeLimit | 否 | 空 | 文件上传最大限制,单位支持:K,M,G,不带单位时,默认为K(如不限制,请不要不配置此参数) |
配置示例 在application.yml中添加如下配置
minio:
enable: true
## minio API地址
url: http://x.x.x.x:9000
## 账户
accessKey: minioadmin
## 密码
secretKey: minioadmin
## 桶
bucket: default
## 当桶不存在,是否创建
createBucket: true
## 启动检测桶,是否存在
checkBucket: true
## 连接超时
connectTimeout: 6000
## 写入超时
writeTimeout: 2000
## 读取超时
readTimeout: 2000
fileTypeLimit: doc,xls,png,pdf
fileSizeLimit: 100K
3、方法使用说
说明 | 方法 |
---|---|
创建桶 | void createBucket(String bucketName); |
检查桶是否存在 | Boolean bucketExists(String bucketName); |
检查文件是否存在 | Boolean checkFileIsExist(String objectName); |
Boolean checkFileIsExist(String bucketName, String objectName); | |
Boolean checkFolderIsExist(String bucketName, String folderName); | |
检查文件夹是否存在 Boolean checkFolderIsExist(String folderName);
获取所有桶 minioTemplate.getAllBuckets();
上传文件 String putObject(MultipartFile file);
String putObject(String objectName, InputStream inputStream, String contentType);
String putObject(String objectName, byte[] bytes, String contentType);
String putObject(String objectName, File file, String contentType);
String putObject(String objectName, MultipartFile file);
String putObject(String bucketName, String objectName, MultipartFile file);
String putObject(String bucketName, String objectName, InputStream inputStream, String contentType);
String putObject(String bucketName, String objectName, byte[] bytes, String contentType);
String putObject(String bucketName, String objectName, File file, String contentType);
根据文件全路径获取文件流 InputStream getObject(String objectName);
文件桶和文件全路径获取文件流 InputStream getObject(String bucketName, String objectName);
根据url获取文件流 InputStream getObjectByUrl(String url);
根据bucketName删除信息 void removeBucket(String bucketName);
删除文件 void removeObject(String objectName);
void removeObject(String bucketName, String objectName);
根据bucketName和文件名称获取临时访问地址 String preSignedGetObject(String bucketName, String objectName, Integer expires);
图片压缩读取 void getImageOutByReduce(String bucketName, String objectName,OutputStream outputStream,float scale,float outputQuality)
4、使用示例
@RequestMapping("minio")
public class MinioController {
@Autowired
private MinioTemplate minioTemplate;
/*
* 创建存储桶
* bucketName:存储桶名称
*/
@GetMapping("/create")
public Result create(String bucketName) {
if (!minioTemplate.bucketExists(bucketName)) {
minioTemplate.createBucket(bucketName);
return Result.success("创建成功");
}
return Result.failed("桶,已存在");
}
/*
*上传文件到minio服务器
*/
@PostMapping("/upload")
public Result upload(@RequestParam("file") MultipartFile multipartFile) {
String s = minioTemplate.putObject(multipartFile);
return Result.success(s);
}
/*
* 从默认存储桶中移除 文件对象
*/
@RequestMapping("/remove")
public Result remove(String name) {
minioTemplate.removeObject(name);
return Result.success("ok");
}
/**
* 图片压缩读取
* @param response
* @return
*/
@RequestMapping("/download")
public Result fileDownLoad(HttpServletResponse response){
InputStream inputStream;
try {
String fileName = "test.jpg";
response.reset();
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName );
OutputStream os = response.getOutputStream();
//读取参数:bucket名称,文件名称,OutputStream,长宽尺寸压缩比,质量压缩比
minioTemplate.getImageOutByReduce("zsh-test", "1585572934997.jpg", os, 0.5f, 0.5f);
os.flush();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return Result.success("ok");
}
}