出入库新增审核相关接口
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
package com.zg.common.utils;
|
||||
|
||||
import io.minio.*;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import io.minio.MakeBucketArgs;
|
||||
import io.minio.BucketExistsArgs;
|
||||
import io.minio.errors.*;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.InputStream;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
@@ -23,38 +26,54 @@ public class MinioUtil {
|
||||
private String secretKey;
|
||||
|
||||
@Value("${minio.bucketName}")
|
||||
private String bucketName;
|
||||
private String defaultBucket;
|
||||
|
||||
/**
|
||||
* 上传文件到 MinIO,自动根据日期生成路径
|
||||
* 如:2025/07/10/uuid-xxx.jpg
|
||||
*/
|
||||
public String uploadFile(MultipartFile file) throws Exception {
|
||||
MinioClient client = MinioClient.builder()
|
||||
private MinioClient minioClient;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
minioClient = MinioClient.builder()
|
||||
.endpoint(endpoint)
|
||||
.credentials(accessKey, secretKey)
|
||||
.build();
|
||||
}
|
||||
|
||||
// 检查桶是否存在,不存在则创建
|
||||
boolean found = client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
|
||||
if (!found) {
|
||||
client.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
||||
/**
|
||||
* 上传文件到指定 bucket
|
||||
* @param file 文件
|
||||
* @param bucketName bucket 名称
|
||||
* @return 返回访问URL
|
||||
*/
|
||||
public String upload(MultipartFile file, String bucketName) throws Exception {
|
||||
String originalName = file.getOriginalFilename();
|
||||
String ext = originalName != null && originalName.contains(".")
|
||||
? originalName.substring(originalName.lastIndexOf("."))
|
||||
: "";
|
||||
String objectName = UUID.randomUUID().toString().replace("-", "") + ext;
|
||||
|
||||
InputStream in = file.getInputStream();
|
||||
|
||||
// 自动创建 bucket(如果不存在)
|
||||
boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
|
||||
if (!exists) {
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
||||
}
|
||||
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
String datePath = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
|
||||
String objectName = datePath + "/" + UUID.randomUUID().toString() + "-" + originalFilename;
|
||||
// 上传文件
|
||||
minioClient.putObject(PutObjectArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.object(objectName)
|
||||
.stream(in, file.getSize(), -1)
|
||||
.contentType(file.getContentType())
|
||||
.build());
|
||||
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
client.putObject(PutObjectArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.object(objectName)
|
||||
.stream(inputStream, file.getSize(), -1)
|
||||
.contentType(file.getContentType())
|
||||
.build());
|
||||
}
|
||||
|
||||
// 返回完整可访问 URL(前端可直接使用)
|
||||
return endpoint + "/" + bucketName + "/" + objectName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传到默认 bucket
|
||||
*/
|
||||
public String upload(MultipartFile file) throws Exception {
|
||||
return upload(file, defaultBucket);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user