介绍

用于开始组建OSS文件服务项目 以完成了的对接

  • Minio
  • 七牛云
  • 本地上传

image.png

  1. 业务中很多上次下载的工作,以前是在项目中直接写文件的上次下载,后面改成了OSS服务器,由于很多外包项目所以导致需要对接很多种OSS服务,所有有了这个公共的文件服务上传下载基础API
  2. 此项目用到的是Spring 的 SPI

依赖

  1. <properties>
  2. <java.version>1.8</java.version>
  3. <okhttp3.version>4.9.0</okhttp3.version>
  4. </properties>
  5. <dependencies>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <!-- web starters -->
  11. <dependency>
  12. <groupId>cn.jdevelops</groupId>
  13. <artifactId>starters-jdevelops-boot-web</artifactId>
  14. <exclusions>
  15. <exclusion>
  16. <groupId>cn.jdevelops</groupId>
  17. <artifactId>webs-jwt</artifactId>
  18. </exclusion>
  19. </exclusions>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-test</artifactId>
  24. <scope>test</scope>
  25. </dependency>
  26. <dependency>
  27. <groupId>commons-fileupload</groupId>
  28. <artifactId>commons-fileupload</artifactId>
  29. <version>1.4</version>
  30. </dependency>
  31. <!--oss spi-->
  32. <dependency>
  33. <groupId>cn.jdevelops</groupId>
  34. <!-- <artifactId>minio-driver</artifactId>-->
  35. <artifactId>qiniu-driver</artifactId>
  36. <!-- <artifactId>local-driver</artifactId>-->
  37. <version>2.0.4</version>
  38. </dependency>
  39. </dependencies>

使用

接口

  1. package cn.jdevelops.file.controller;
  2. import cn.jdevelops.exception.exception.BusinessException;
  3. import cn.jdevelops.file.OssOperateAPI;
  4. import cn.jdevelops.file.bean.*;
  5. import cn.jdevelops.result.result.ResultVO;
  6. import io.swagger.annotations.Api;
  7. import io.swagger.annotations.ApiOperation;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.web.bind.annotation.*;
  10. import javax.servlet.http.HttpServletResponse;
  11. import javax.validation.Valid;
  12. import java.util.List;
  13. /**
  14. * 文件上传下载
  15. *
  16. * @author lxw
  17. * @version V1.0
  18. * @date 2021/11/10
  19. */
  20. @RestController
  21. @Api(tags = "文件操作", value = "文件管理")
  22. public class FileController {
  23. @Autowired
  24. private OssOperateAPI fileOperation;
  25. @ApiOperation(value = "文件上传", notes = "文件管理")
  26. @PostMapping(value = "upload")
  27. public ResultVO<FilePathResult> upload(@Valid UploadDTO uploadDTO) {
  28. try {
  29. FilePathResult filePathResult = fileOperation.uploadFile(uploadDTO);
  30. return ResultVO.successForData(filePathResult);
  31. } catch (Exception e) {
  32. throw new BusinessException("文件上传失败!", e);
  33. }
  34. }
  35. @GetMapping("/download")
  36. @ApiOperation(value = "文件下载", notes = "文件管理")
  37. public void download(HttpServletResponse response, @Valid DownloadDTO dto) {
  38. try {
  39. fileOperation.downloadFile(response, dto);
  40. } catch (Exception e) {
  41. throw new BusinessException("文件下载失败!");
  42. }
  43. }
  44. @GetMapping("/getExpiryObjectUrl")
  45. @ApiOperation(value = "获取有效期访问地址", notes = "文件管理")
  46. public ResultVO<String> getExpiryObjectUrl(@Valid ExpireDateDTO dto) {
  47. try {
  48. String url = fileOperation.expireDateUrl(dto);
  49. return ResultVO.successForData(url);
  50. } catch (Exception e) {
  51. throw new BusinessException("获取有效期访问地址失败!");
  52. }
  53. }
  54. @DeleteMapping("/removeObjects")
  55. @ApiOperation(value = "删除", notes = "文件管理")
  56. public ResultVO<List<String>> removeObjects(@RequestBody @Valid RemoveFileDTO dto) {
  57. try {
  58. fileOperation.removeFiles(dto);
  59. return ResultVO.success();
  60. } catch (Exception e) {
  61. throw new BusinessException("删除失败!",e);
  62. }
  63. }
  64. }

必看

如果需要切换OSS服务器,不需要改动任何代码,只需要做到如下两点:

  1. 修改 oss-driver 依赖(目前支持的 oss-driver如下)
    1. minio-driver
    2. qiniu-driver
    3. local-driver
  2. 修改配置文件
    1. https://github.com/en-o/Jdevelops-Example/blob/main/file/src/main/resources/application-minio.yml
    2. https://github.com/en-o/Jdevelops-Example/blob/main/file/src/main/resources/application-qiniu.yml
    3. https://github.com/en-o/Jdevelops-Example/blob/main/file/src/main/resources/application-local.yml
  3. 然后重启项目就可以了

    示例项目地址

    https://github.com/en-o/Jdevelops-Example/tree/main/file