依赖jar包

引入包 版本
jdk 1.8
spring boot 2.7.4
spring-boot-autoconfigure 2.7.4
spring-boot-starter-web 2.7.4
aws-java-sdk-s3 1.12.332
jakarta.validation-api 3.0.2

使用

添加依赖

1
2
3
4
5
<dependency>
<groupId>cn.allbs</groupId>
<artifactId>allbs-oss</artifactId>
<version>2.0.0</version>
</dependency>
1
implementation 'cn.allbs:allbs-oss:2.0.0'
1
implementation("cn.allbs:allbs-oss:2.0.0")

添加配置

1
2
3
4
5
6
7
8
oss:
endpoint: http://xxx.xxx.xxx.xxx:9000
region: us-west-rack-2
# minio账号或者
access-key: adadmin
# 密码
secret-key: 123456778
bucket-name: test

上传

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@PostMapping("/upload")
public R upload(@RequestParam("file") MultipartFile file, String folder) {
folder = Optional.ofNullable(folder).orElse("folder");
String fileName = file.getOriginalFilename();
// 文件夹不存在创建
Map<String, Object> resultMap = new HashMap<>(4);
resultMap.put("fileName", fileName);
resultMap.put("url", String.format("/%s/%s", folder, fileName));

try {
ossTemplate.createBucket(folder);
ossTemplate.putObject(folder, fileName, file.getInputStream());
// el-upload 展示需要
resultMap.put("name", file.getOriginalFilename());
} catch (Exception e) {
log.error("上传失败", e);
return R.fail(e.getLocalizedMessage());
}
return R.ok(resultMap);
}

调用

image-20221104152152404

效果

image-20221104152336879

删除

代码示例

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("folder");
ossTemplate.removeObject(folder, name);
return R.ok();
}

调用

image-20221104153047028

读取

代码示例

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 (S3Object s3Object = ossTemplate.getObject(bucket, fileName)) {
response.setContentType("application/octet-stream; charset=UTF-8");
IoUtil.copy(s3Object.getObjectContent(), response.getOutputStream());
} catch (Exception e) {
log.error("文件读取异常: {}", e.getLocalizedMessage());
}
}

调用

image-20221104163020822

获取文件url

代码示例

1
2
3
4
5
@GetMapping("/{bucket}/{fileName}")
@ApiOperation(value = "文件读取", notes = "文件读取")
public R<String> fileUrl(@PathVariable String bucket, @PathVariable String fileName) {
return R.ok(ossTemplate.getObjectURL(bucket, fileName));
}

调用

image-20221104163415288

获取文件url带过期时间

代码示例

1
2
3
4
5
6
@GetMapping("/{bucket}/{fileName}")
@ApiOperation(value = "文件读取", notes = "文件读取")
public R<String> fileUrl(@PathVariable String bucket, @PathVariable String fileName) {
// 文件十小时过期示例
return R.ok(ossTemplate.getObjectURL(bucket, fileName, Duration.ofHours(10)));
}

调用

image-20221104163557607

文件批量上传

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@ApiOperation(value = "批量文件上传", notes = "文件批量上传")
@PostMapping(value = "/fileUpload", headers = "content-type=multipart/form-data")
public R save(@Valid @RequestParam(value = "files") MultipartFile[] files, @RequestParam("folder") String folder) {
if (files.length == 0) {
return R.fail("文件内容不能为空");
}
try {
// 获取文件夹名称
for (MultipartFile file : files) {
ossTemplate.putObject(folder, IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename()), file.getInputStream());
}
} catch (Exception e) {
return R.fail(e.getLocalizedMessage());
}
return R.ok();
}

调用

image-20221104164137648

效果

image-20221104170510271

云服务商oss服务示例

上传、下载、预览等代码同上方

添加配置

image-20221104172637827

endpoint

image-20221104172722083

access-key和secret-key

image-20221104172841532

bucket-name

可不设置,如果需要一个基础文件夹则设置这个属性

效果

image-20221104173010493

源码地址

allbs-os源码