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
|
package com.example.simpleminio;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Tags;
import lombok.SneakyThrows;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
/**
* @author jiang
* @className TestController
* @description TODO 类描述
* @date 2022/1/16
**/
@RestController
public class TestController {
@Autowired
private MinioClient minioClient;
@Autowired
private MinioClientProperties minioClientProperties;
@Autowired
private HttpServletResponse response;
@PostMapping("/upload")
public String upload(UploadDTO uploadDTO) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
final MultipartFile file = uploadDTO.getFile();
final List<String> user = uploadDTO.getUser();
final Set<String> userSet = new HashSet<>(user);
long startTime=System.currentTimeMillis();
Assert.state(userSet.size() == user.size(), "用户存在重复,不允许上传!");
if (file.isEmpty()) {
return "文件为空!";
}
final ObjectWriteResponse response;
try (BufferedInputStream bis = new BufferedInputStream(file.getInputStream(), 10 * 1024 * 1024)) {
response = minioClient.putObject(
PutObjectArgs.builder()
.bucket(minioClientProperties.getBucketName())
.object(file.getOriginalFilename())
// 指定objectSize为-1即可上传超2G文件,partSize为分片大小 默认5M这里是100M
.stream(bis, -1L, 100 * 1024 * 1024).build());
}
Map<String, String> map = new HashMap<>();
for (int i = 1; i <= user.size(); i++) {
map.put("user" + i, user.get(i - 1));
}
minioClient.setObjectTags(SetObjectTagsArgs.builder()
.bucket(minioClientProperties.getBucketName())
.object(response.object())
.tags(map)
.build());
long endTime = System.currentTimeMillis();
System.out.println("程序运行时间:" + (endTime - startTime) + "ms");
return response.etag();
}
@SneakyThrows
@GetMapping("/download/{file}/{user}")
public void download(@PathVariable String file, @PathVariable String user) {
long startTime=System.currentTimeMillis();
final Tags objectTags = minioClient.getObjectTags(GetObjectTagsArgs.builder().bucket(minioClientProperties.getBucketName()).object(file).build());
System.out.println(objectTags.get());
// 所有tags(不可变map)
final Map<String, String> stringStringMap = objectTags.get();
final Set<Map.Entry<String, String>> entries = stringStringMap.entrySet();
final Optional<Map.Entry<String, String>> first = entries.stream().filter(e -> e.getValue().equals(user)).findFirst();
if (first.isPresent()) { // 文件存在就获取
final Map.Entry<String, String> entry = first.get();
final InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(minioClientProperties.getBucketName()).object(file).build());
try (BufferedInputStream bis = new BufferedInputStream(stream, 10 * 1024 * 1024);
ServletOutputStream outputStream = response.getOutputStream()) {
IOUtils.copy(bis, outputStream);
}
// 移除当前用户标识
final HashMap<String, String> newMap = new HashMap<>(stringStringMap);
newMap.remove(entry.getKey(), entry.getValue());
// 没有标识了就删除文件
if (newMap.isEmpty()) {
minioClient.removeObject(
RemoveObjectArgs.builder()
.bucket(minioClientProperties.getBucketName())
.object(file)
.build());
long endTime = System.currentTimeMillis();
System.out.println("程序运行时间1:" + (endTime - startTime) + "ms");
return;
}
// 覆盖tag
minioClient.setObjectTags(SetObjectTagsArgs.builder()
.bucket(minioClientProperties.getBucketName())
.object(file)
.tags(newMap)
.build());
}
long endTime = System.currentTimeMillis();
System.out.println("程序运行时间:=" + (endTime - startTime) + "ms");
}
}
|