xiangpei
2025-04-18 ccadf9480d4e6a9dcc227a2a0b1f9ae0612e36fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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<MultipartFile> files) {
        return ossFileService.putObjects(files);
    }
 
    /**
     * 多文件上传
     *
     * @param files
     * @return
     */
    @PostMapping("/multi/upload/no/expire")
    @ApiOperation(value = "多文件上传(返回没有过期时间的链接)")
    public Result uploadsNoExpire(@RequestPart("files") @NotEmpty List<MultipartFile> 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<String> 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<String> fileKeys) {
        return ossFileService.getObjects(fileKeys);
    }
 
    @GetMapping("/sts")
    @ApiOperation(value = "获取STS访问令牌", notes = "前端做直传")
    public Result getSTSToken() {
        return ossFileService.getSTSToken();
    }
 
}