依赖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 包含当前工具所有功能。
📂 oss相关文件操作
使用
添加依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <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>
|
1
| implementation 'cn.allbs:allbs-minio:1.1.7'
|
1
| implementation("cn.allbs:allbs-minio:1.1.7")
|
添加配置
1 2 3 4 5 6 7
| minio: url: nas.allbs.cn access-key: 907T52D29TYFO49WGZH2 secret-key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx port: 9006 secure: true bucket-name: pic
|
template注入
1
| private final MinioTemplate minioTemplate;
|
单文件上传
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @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()); resultMap.put("name", file.getOriginalFilename()); } catch (Exception e) { log.error("上传失败", e); return R.failed(e.getLocalizedMessage()); } return R.ok(resultMap); }
|
文件删除
1 2 3 4 5 6 7
| @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(); }
|
文件预览
1 2 3 4 5 6 7 8 9 10
| @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()); } }
|
文件批量上传
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @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(); }
|