package com.rft.syshopmanage.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import io.minio.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 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;
}
}
![avatar](https://profile-avatar.csdnimg.cn/4fa0b84501f94ccf80dbd2327bb2d23e_weixin_48025574.jpg!1)
大角鹿
- 粉丝: 9
- 资源: 2
最新资源
- SSA-Catboost麻雀搜索算法优化Catboost分类预测,优化前后对比(Matlab完整源码和数据)
- 事件触发控制代码,每个代码有对应参考文献 1.多智能体中基于事件触发的协议 2.多智能体分布式系统的事件触发控制 3.基于观测器
- Python实现 ChatOps 的最简单最受欢迎的聊天机器人
- Python GraphQL 框架
- Emacs Python 开发环境
- 如何在Windows 11未检测到耳机时修复它
- HBA-Catboost蜜獾优化算法优化Catboost分类预测,优化前后对比(Matlab完整源码和数据)
- 1517-基于51单片机的双机通信(数码管,数字,LED)proteus、原理图、流程图、物料清单、仿真图、源代码.zip
- 1516-基于51单片机的双机通信(键值,LCD)proteus,原理图、流程图、物料清单、仿真图、源代码.zip
- 鲸鱼算法算支持向量机SVM回归分析,鲸鱼算法优化支持向量机SVM沉降预测(代码完整,数据齐全)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)