diff options
Diffstat (limited to 'src/main/java/com/example/simpleminio/TestController.java')
| -rw-r--r-- | src/main/java/com/example/simpleminio/TestController.java | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/main/java/com/example/simpleminio/TestController.java b/src/main/java/com/example/simpleminio/TestController.java new file mode 100644 index 0000000..94f4876 --- /dev/null +++ b/src/main/java/com/example/simpleminio/TestController.java @@ -0,0 +1,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"); + } +} |
