引入MinIO相关依赖
<!--MinIO JAVA SDK-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.5</version>
</dependency>
MinioController 上传文件工具类
/**
* MinIO对象存储管理Controller
*/
@Controller
@RequestMapping("/minio")
public class MinioController {
private String ENDPOINT = "MinIO服务所在地址 例:http://xxxxxx:9020";
private String BUCKET_NAME = "存储桶名称";
private String ACCESS_KEY = "访问的key";
private String SECRET_KEY = "访问的秘钥";
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public JSONObject upload(@RequestPart("file") MultipartFile file) {
JSONObject returnData = new JSONObject();
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) {
System.out.println("存储桶已经存在!");
} 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("yyyyMMdd");
// 设置存储对象名称
String objectName = sdf.format(new Date()) + "/" + filename;
// 使用putObject上传一个文件到存储桶中
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(BUCKET_NAME)
.object(objectName)
.contentType(file.getContentType())
.stream(file.getInputStream(), file.getSize(), ObjectWriteArgs.MIN_MULTIPART_SIZE).build();
minioClient.putObject(putObjectArgs);
System.out.println("文件上传成功!");
//返回文件名
returnData.put("filename",filename);
//返回文件url
returnData.put("url",ENDPOINT + "/" + BUCKET_NAME + "/" + objectName);
return returnData;
} catch (Exception e) {
e.printStackTrace();
System.out.println("上传发生错误!");
}
return returnData;
}
/**
* 创建存储桶的访问策略,设置为只读权限
*/
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();
}
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public JSONObject delete(@RequestParam("objectName") String objectName) {
JSONObject returnData = new JSONObject();
try {
MinioClient minioClient = MinioClient.builder()
.endpoint(ENDPOINT)
.credentials(ACCESS_KEY,SECRET_KEY)
.build();
minioClient.removeObject(RemoveObjectArgs.builder().bucket(BUCKET_NAME).object(objectName).build());
returnData.put("msg","成功");
return returnData;
} catch (Exception e) {
e.printStackTrace();
}
returnData.put("msg","失败");
return returnData;
}
}
BucketPolicyConfigDto 访问策略配置类
/**
* Minio Bucket访问策略配置
*/
@Data
@EqualsAndHashCode
@Builder
public class BucketPolicyConfigDto {
private String Version;
private List<Statement> Statement;
@Data
@EqualsAndHashCode
@Builder
public static class Statement {
private String Effect;
private String Principal;
private String Action;
private String Resource;
}
}