package com.monkeylessey.file.controller; import com.monkeylessey.file.config.OssTemplate; import com.monkeylessey.file.serivce.OssFileService; import com.monkeylessey.response.Result; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.apache.ibatis.annotations.Delete; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import java.io.IOException; import java.util.List; /** * @author 29443 * @date 2022/4/23 */ @RestController @RequiredArgsConstructor @Api(tags = "文件OSS管理") @RequestMapping("/file") public class FileController { private final OssFileService ossFileService; private final OssTemplate ossTemplate; @PostMapping("/append") @ApiOperation(value = "追加上传demo演示") public void appendFileTest() throws IOException { ossFileService.uploadAppendDemo(); } /** * 单文件上传(返回有过期时间的链接) * * @param file * @return */ @PostMapping("/upload") @ApiOperation(value = "单文件上传(返回有过期时间的链接)") public Result upload(@RequestPart("file") @NotNull MultipartFile file) { return ossFileService.putObject(file); } /** * 单文件上传(返回没有过期时间的链接) * * @param file * @return */ @PostMapping("/upload/no/expire") @ApiOperation(value = "单文件上传(返回没有过期时间的链接)") public Result uploadNoExpire(@RequestPart("file") @NotNull MultipartFile file) { return ossFileService.putObjectNoExpire(file); } /** * 多文件上传 * * @param files * @return */ @PostMapping("/multi/upload") @ApiOperation(value = "多文件上传(返回有过期时间的链接)") public Result uploads(@RequestPart("files") @NotEmpty List files) { return ossFileService.putObjects(files); } /** * 多文件上传 * * @param files * @return */ @PostMapping("/multi/upload/no/expire") @ApiOperation(value = "多文件上传(返回没有过期时间的链接)") public Result uploadsNoExpire(@RequestPart("files") @NotEmpty List files) { return ossFileService.putObjects(files); } /** * 删除某个文件 * * @param fileKey oss文件名 * @return */ @Delete("/delete/{file_key}") @ApiOperation(value = "删除某个文件") public Result deleteObject(@PathVariable(value = "file_key") String fileKey) { return ossFileService.deleteObject(fileKey); } /** * 批量删除文件 * * @param fileKeys * @return */ @Delete("/delete/files") @ApiOperation(value = "批量删除文件") public Result deleteObjects(@RequestBody @NotEmpty List fileKeys) { return ossFileService.deleteObjects(fileKeys); } /** * 获取oss上的文件(下载) * * @param fileKey * @return */ @GetMapping("/{file_key}") @ApiOperation(value = "获取oss上的文件(下载)", notes = "如果上传的文件通过链接浏览器自动下载。此方法用于上传时没设置自动下载的文件") public Result getObject(@PathVariable(value = "file_key") String fileKey) { return ossFileService.getObject(fileKey); } /** * 批量获取oss上的文件(下载) * * @param fileKeys * @return */ @GetMapping("/files") @ApiOperation(value = "批量获取oss上的文件(下载)", notes = "如果上传的文件通过链接浏览器自动下载。此方法用于上传时没设置自动下载的文件") public Result getObject(@RequestBody List fileKeys) { return ossFileService.getObjects(fileKeys); } @GetMapping("/sts") @ApiOperation(value = "获取STS访问令牌", notes = "前端做直传") public Result getSTSToken() { return ossFileService.getSTSToken(); } }