minio上传文件支持temp-前缀临时文件,有效期默认为一天

topo_dev
DESKTOP-DDTUS3E\yaxin 6 months ago
parent 3b874e2d28
commit 192ff1f265

@ -1,11 +1,19 @@
package com.supervision.config;
import io.minio.MinioClient;
import io.minio.SetBucketLifecycleArgs;
import io.minio.errors.*;
import io.minio.messages.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
/**
* Endpoint URL
* Access Key Access keyID
@ -26,15 +34,43 @@ public class MinioConfig {
@Value("${minio.secretKey}")
private String secretKey;
@Value("${minio.bucketName}")
private String bucketName;
@Bean
@Primary
public MinioClient minioClient() {
// Create a minioClient with the MinIO server playground, its access key and secret key.
return MinioClient.builder()
MinioClient client = MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
try {
client.setBucketLifecycle(
SetBucketLifecycleArgs
.builder()
.config(new LifecycleConfiguration(
List.of(
new LifecycleRule(Status.ENABLED,
null,
new Expiration((ResponseDate) null, 1, null),
new RuleFilter("temp-"),
"TempFileDeleteRule",
null,
null,
null)
)
))
.bucket(bucketName)
.build()
);
} catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException |
InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException |
XmlParserException e) {
throw new RuntimeException(e);
}
return client;
}
}

@ -2,11 +2,11 @@ package com.supervision.minio.controller;
import com.supervision.common.domain.R;
import com.supervision.minio.service.MinioService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
@ -17,8 +17,8 @@ public class MinioController {
private final MinioService minioService;
@PostMapping("/uploadFile")
public R<String> uploadFile(@RequestPart("file") MultipartFile file) throws IOException {
String fileId = minioService.uploadFile(file);
public R<String> uploadFile(@RequestPart("file") MultipartFile file, @RequestParam(required = false, defaultValue = "false") boolean temp) throws IOException {
String fileId = minioService.uploadFile(file, temp);
return R.ok(fileId);
}

@ -2,9 +2,9 @@ package com.supervision.minio.service;
import com.supervision.common.domain.R;
import com.supervision.minio.domain.MinioFile;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@ -13,6 +13,8 @@ public interface MinioService {
String uploadFile(MultipartFile file) throws IOException;
String uploadFile(MultipartFile file, boolean temp) throws IOException;
void downloadFile(String fileId, HttpServletResponse response);
R<?> delFile(String fileId);

@ -31,6 +31,11 @@ public class MinioServiceImpl implements MinioService {
@Override
public String uploadFile(MultipartFile file) throws IOException {
return uploadFile(file, false);
}
@Override
public String uploadFile(MultipartFile file, boolean temp) throws IOException {
String suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1);
InputStream in = file.getInputStream();
MinioFile minioFile = new MinioFile();
@ -40,7 +45,7 @@ public class MinioServiceImpl implements MinioService {
minioFile.setSize(in.available());
int i = minioFileMapper.insert(minioFile);
if (i > 0) {
minioTemplate.putObject(bucketName, minioFile.getId(), null, in, in.available(), file.getContentType());
minioTemplate.putObject(bucketName, temp ? "temp-" + minioFile.getId() : minioFile.getId(), null, in, in.available(), file.getContentType());
return minioFile.getId();
} else {
return null;

Loading…
Cancel
Save