allbs工具类说明 - minio操作
依赖jar包
引入包 | 版本 |
---|---|
jdk | 1.8 |
spring boot | 2.7.2 |
spring-boot-autoconfigure | 2.7.2 |
minio | 8.4.3 |
allbs-common | 1.1.6 |
spring-boot-starter-web | 2.7.2 |
说明
📖 当前工具包自 1.1.7之后不再维护更新,相关功能请看allbs-oss 包含当前工具所有功能。
使用
添加依赖
<dependency>
<groupId>cn.allbs</groupId>
<artifactId>allbs-minio</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.8.1</version>
</dependency>
implementation 'cn.allbs:allbs-minio:1.1.7'
implementation("cn.allbs:allbs-minio:1.1.7")
添加配置
minio:
url: nas.allbs.cn #链接 必须
access-key: 907T52D29TYFO49WGZH2 #access-key 必须 注意8.0 之后不再为登录账号 user->account 中生成
secret-key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx #secret-key 必须 注意8.0 之后不再为登录账号
port: 9006 # 端口 不设置将为9000
secure: true # 是否启用ssl true为https,false为http 默认为false
bucket-name: pic # 基础目录 可不设置 添加该目录后所有通过api操作的文件或者bucket都将在该目录下
template注入
private final MinioTemplate minioTemplate;
单文件上传
@PostMapping("/upload")
@ApiOperation(value = "文件上传", notes = "文件上传")
public R upload(@RequestParam("file") MultipartFile file, String folder) {
folder = Optional.ofNullable(folder).orElse(CommonConstants.BUCKET_NAME);
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
// 文件夹不存在创建
Map<String, Object> resultMap = new HashMap<>(4);
resultMap.put("fileName", fileName);
resultMap.put("url", String.format("/%s/%s", folder, fileName));
try {
minioTemplate.createBucket(folder);
minioTemplate.putObject(folder, fileName, file.getInputStream());
// el-upload 展示需要
resultMap.put("name", file.getOriginalFilename());
} catch (Exception e) {
log.error("上传失败", e);
return R.failed(e.getLocalizedMessage());
}
return R.ok(resultMap);
}
文件删除
@ApiOperation(value = "通过文件名称删除文件管理", notes = "通过文件名称删除文件管理")
@DeleteMapping("/deleteFile")
public R removeById(@RequestParam("name") String name, @RequestParam("folder") String folder) throws Exception {
folder = Optional.ofNullable(folder).orElse(CommonConstants.BUCKET_NAME);
minioTemplate.removeObject(folder, name);
return R.ok();
}
文件预览
@GetMapping("/{bucket}/{fileName}")
@ApiOperation(value = "文件读取", notes = "文件读取")
public void file(@PathVariable String bucket, @PathVariable String fileName, HttpServletResponse response) {
try (InputStream inputStream = minioTemplate.getObject(bucket, fileName)) {
response.setContentType("application/octet-stream; charset=UTF-8");
IoUtil.copy(inputStream, response.getOutputStream());
} catch (Exception e) {
log.error("文件读取异常: {}", e.getLocalizedMessage());
}
}
文件批量上传
@ApiOperation(value = "批量文件上传", notes = "文件批量上传")
@PostMapping(value = "/fileUpload", headers = "content-type=multipart/form-data")
public R save(@RequestParam(value = "file", required = false) MultipartFile[] files, @RequestParam("otherParam") Map<String, Object> params) {
if (files.length == 0) {
return R.failed("文件内容不能为空");
}
try {
// 获取文件夹名称
String folder = Optional.ofNullable(params).map(a -> MapUtil.getStr(a, "folder")).orElse(CommonConstants.BUCKET_NAME);
for (MultipartFile file : files) {
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
minioTemplate.putObject(folder, fileName, file.getInputStream());
}
} catch (Exception e) {
return R.failed(e.getLocalizedMessage());
}
return R.ok();
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 ALLBS!
评论