本页导航
article
Cos对象存储
AI摘要
本文详细介绍了一个基于Java的Cos对象存储控制器,涵盖文件上传、下载(到本地及流)、删除等功能的接口设计与实现。通过@RestController和@RequestMapping注解定义统一路由,结合@Resource注入服务层,提供了完整的文件操作解决方案,适合在项目中快速集成对象存储功能。
引用
@RestController
@Slf4j
@RequestMapping("/cos")
public class CosController {
...
@Resource
private CosService cosService;
...
}
文件上传
@PostMapping("/upload")
@Operation(summary = "文件上传")
public BaseResponse<String> upload(MultipartFile file, String biz) {
return cosService.uploadObject(file, biz);
}
文件下载到本地
@PostMapping("/downloadLocal")
@Operation(summary = "文件下载到本地")
public void downloadToLocal(String url, String localFilePath) {
cosService.download(url, localFilePath);
}
文件下载到流
@PostMapping("/download")
@Operation(summary = "文件下载到流")
public byte[] download(String url) throws IOException {
return cosService.getObject(url);
}
文件删除
@PostMapping("/delete")
@Operation(summary = "文件删除")
public BaseResponse<String> delete(String url) {
return cosService.deleteObject(url);
}
完整代码
@RestController
@Slf4j
@RequestMapping("/cos")
public class CosController {
@Resource
private CosService cosService;
@PostMapping("/upload")
@Operation(summary = "文件上传")
public BaseResponse<String> upload(MultipartFile file, String biz) {
return cosService.uploadObject(file, biz);
}
@PostMapping("/downloadLocal")
@Operation(summary = "文件下载到本地")
public void downloadToLocal(String url, String localFilePath) {
cosService.download(url, localFilePath);
}
@PostMapping("/download")
@Operation(summary = "文件下载到流")
public byte[] download(String url) throws IOException {
return cosService.getObject(url);
}
@PostMapping("/delete")
@Operation(summary = "文件删除")
public BaseResponse<String> delete(String url) {
return cosService.deleteObject(url);
}
}
最后更新于 2025-09-30 20:31