You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
6.0 KiB
155 lines
6.0 KiB
11 months ago
|
package com.teaching.backend.utils;
|
||
|
|
||
|
import cn.hutool.core.collection.CollUtil;
|
||
|
import cn.hutool.json.JSONUtil;
|
||
|
import com.teaching.backend.common.BaseResponse;
|
||
|
import com.teaching.backend.common.ErrorCode;
|
||
|
import com.teaching.backend.common.ResultUtils;
|
||
|
import com.teaching.backend.constant.ViewContentTypeEnum;
|
||
|
import com.teaching.backend.model.dto.resource.BucketPolicyConfigDto;
|
||
|
import com.teaching.backend.model.dto.resource.ResourceUploadDto;
|
||
|
import com.teaching.backend.model.entity.resource.Resources;
|
||
|
import com.teaching.backend.service.resource.ResourceService;
|
||
|
import io.minio.*;
|
||
|
import org.slf4j.Logger;
|
||
|
import org.slf4j.LoggerFactory;
|
||
|
import org.springframework.beans.factory.annotation.Value;
|
||
|
import org.springframework.core.io.InputStreamResource;
|
||
|
import org.springframework.http.ResponseEntity;
|
||
|
import org.springframework.stereotype.Component;
|
||
|
import org.springframework.web.multipart.MultipartFile;
|
||
|
|
||
|
import java.io.File;
|
||
|
import java.io.InputStream;
|
||
|
import java.text.SimpleDateFormat;
|
||
|
import java.util.Date;
|
||
|
|
||
|
/**
|
||
|
* @Author:youhang
|
||
|
* @Date:2024-08-07-11:38
|
||
|
* @Description:
|
||
|
*/
|
||
|
@Component
|
||
|
public class MinioUtils {
|
||
|
|
||
|
private static final Logger LOGGER = LoggerFactory.getLogger(MinioUtils.class);
|
||
|
|
||
|
@Value("${filename.maxlength}")
|
||
|
private int MAX_FILENAMELENGTH;
|
||
|
|
||
|
@Value("${minio.endpoint}")
|
||
|
private String ENDPOINT;
|
||
|
|
||
|
@Value("${minio.bucketName}")
|
||
|
private String BUCKET_NAME;
|
||
|
@Value("${minio.accessKey}")
|
||
|
private String ACCESS_KEY;
|
||
|
@Value("${minio.secretKey}")
|
||
|
private String SECRET_KEY;
|
||
|
|
||
|
public ResourceUploadDto upload(MultipartFile file) {
|
||
|
ResourceUploadDto minioUploadDto = new ResourceUploadDto();
|
||
|
try {
|
||
|
//创建一个MinIO的Java客户端
|
||
|
MinioClient minioClient =MinioClient.builder()
|
||
|
.endpoint(ENDPOINT)
|
||
|
.credentials(ACCESS_KEY,SECRET_KEY)
|
||
|
.build();
|
||
|
boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(BUCKET_NAME).build());
|
||
|
if (isExist) {
|
||
|
LOGGER.info("存储桶已经存在!");
|
||
|
} else {
|
||
|
//创建存储桶并设置只读权限
|
||
|
minioClient.makeBucket(MakeBucketArgs.builder().bucket(BUCKET_NAME).build());
|
||
|
BucketPolicyConfigDto bucketPolicyConfigDto = createBucketPolicyConfigDto(BUCKET_NAME);
|
||
|
SetBucketPolicyArgs setBucketPolicyArgs = SetBucketPolicyArgs.builder()
|
||
|
.bucket(BUCKET_NAME)
|
||
|
.config(JSONUtil.toJsonStr(bucketPolicyConfigDto))
|
||
|
.build();
|
||
|
minioClient.setBucketPolicy(setBucketPolicyArgs);
|
||
|
}
|
||
|
String filename = file.getOriginalFilename();
|
||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddhhmmss");
|
||
|
// todo 字符串截取
|
||
|
int dotIndex = filename.lastIndexOf('.');
|
||
|
String prefix = filename.substring(0,dotIndex);
|
||
|
String suffix = filename.substring(dotIndex+1,filename.length());
|
||
|
if(prefix.length() > MAX_FILENAMELENGTH){
|
||
|
prefix = prefix.substring(0,MAX_FILENAMELENGTH);
|
||
|
}
|
||
|
filename = prefix+"-"+sdf.format(new Date())+"."+suffix;
|
||
|
sdf = new SimpleDateFormat("yyyyMMdd");
|
||
|
// 设置存储对象名称
|
||
|
String objectName = sdf.format(new Date()) + "/" + suffix + "/" + filename;
|
||
|
// 使用putObject上传一个文件到存储桶中
|
||
|
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
|
||
|
.bucket(BUCKET_NAME)
|
||
|
.object(objectName)
|
||
|
.contentType(ViewContentTypeEnum.getContentType(objectName))
|
||
|
.stream(file.getInputStream(), file.getSize(), ObjectWriteArgs.MIN_MULTIPART_SIZE).build();
|
||
|
minioClient.putObject(putObjectArgs);
|
||
|
LOGGER.info("文件上传成功!");
|
||
|
minioUploadDto.setName(filename);
|
||
|
minioUploadDto.setUrl(ENDPOINT + "/" + BUCKET_NAME + "/" + objectName);
|
||
|
minioUploadDto.setObjectName(objectName);
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
LOGGER.info("上传发生错误: {}!", e.getMessage());
|
||
|
}
|
||
|
return minioUploadDto;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 创建存储桶的访问策略,设置为只读权限
|
||
|
*/
|
||
|
private BucketPolicyConfigDto createBucketPolicyConfigDto(String bucketName) {
|
||
|
BucketPolicyConfigDto.Statement statement = BucketPolicyConfigDto.Statement.builder()
|
||
|
.Effect("Allow")
|
||
|
.Principal("*")
|
||
|
.Action("s3:GetObject")
|
||
|
.Resource("arn:aws:s3:::"+bucketName+"/*.**").build();
|
||
|
return BucketPolicyConfigDto.builder()
|
||
|
.Version("2012-10-17")
|
||
|
.Statement(CollUtil.toList(statement))
|
||
|
.build();
|
||
|
}
|
||
|
|
||
|
|
||
|
public void delete(String objectName) throws Exception {
|
||
|
MinioClient minioClient = MinioClient.builder()
|
||
|
.endpoint(ENDPOINT)
|
||
|
.credentials(ACCESS_KEY,SECRET_KEY)
|
||
|
.build();
|
||
|
minioClient.removeObject(RemoveObjectArgs.builder().bucket(BUCKET_NAME).object(objectName).build());
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
public ResponseEntity<InputStreamResource> readFile(String filename) {
|
||
|
try {
|
||
|
// 创建MinIO客户端
|
||
|
MinioClient minioClient = MinioClient.builder()
|
||
|
.endpoint(ENDPOINT)
|
||
|
.credentials(ACCESS_KEY, SECRET_KEY)
|
||
|
.build();
|
||
|
|
||
|
// 获取文件流
|
||
|
GetObjectResponse objectResponse = minioClient.getObject(GetObjectArgs.builder()
|
||
|
.bucket(BUCKET_NAME) // 替换为你实际的存储桶名称
|
||
|
.object(filename)
|
||
|
.build());
|
||
|
|
||
|
InputStream inputStream = objectResponse;
|
||
|
return ResponseEntity.ok()
|
||
|
.body(new InputStreamResource(inputStream));
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
return ResponseEntity.notFound().build();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|