官网:https://www.minio.org.cn/
开源的分布式对象存储服务器
Window安装
用户名和密码相同
创建bucket,并且将策略改成public
一、添加依赖
二、代码
public class FileUploadTest{public static void main(String[] args) throws Exception{//创建一个Minio客户端对象(链接地址和用户名密码)MinioClient minioClient = MinioClient.builder().endpoint("http://127.0.0.1:9000").credentials("minioadmin","minioadmin").build();boolean found = monioClient.bucketExists(BucketExistsArgs.builder().bucket("spzx-bucket").build());//如果不存在,那么此时就创建一个新的桶if(!found){minioClient.makeBucket(MakeBucketArgs.builder().bucket("spzx-bucket").build());}else{//如果存在打印信息System.out.println("Bucket 'spzx-bucket' already exists");}FileInputStream fis = new FileInputStream("D://01.jpg");PubObjectArgs putObjectArgs = PutObjectArgs.builder().bucket("spzx-bucket").stream(fis,fis.available(),-1).object("01.jpg").build();minioClient.putObject(putObjectArgs);//构建fileUrlString fileUrl = "http://127.0.0.1:9000/spzx-bucket/01.jpg";System.out.println(fileUrl);}
}
实战应用
上传一个文件到服务器
一、Cotroller/Service/Mapper
@RestController
@RequestMapping("/admin/system")
public class FileUploadController{@Autowiredprivate FileUploadService fileUploadService;@PostMapping(value="/fileUpload")public Result<String> fileuploadService(@RequestParam(value="file") MultipartFile multipartFile){String fileUrl = fileUploadService.fileUpload(multipartFile);return Result.build(fileUrl,ResultCodeEnum.SUCCESS);}
}
@Service
public class FileUploadServiceImpl implements FileUploadService{@Overridepublic String upload(MultipartFile file){try{//创建MinioClient对象MinioClient minioClient = MinioClient.builder().endpoint("http://127.0.0.1:9000").credentials("minioadmin",minioadmin).build();//创建bucketboolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("spzx-bucket")).build();if(!found){minioClient.makeBucket(MakeBucketArgs.builder().bucket("spzx-bucket")).build();}else{System.out.println("Bucket 'asiatrip' already exists");}//获取上传文件名称String filename = file.getOriginalFilename();//开始上传minioClient.putObject(PubObjectArgs.builder().bucket("spzx-bucket").object("filename").stream(file.getInputStream(),file.getSize(),-1).build());//获取文件在服务器的路径String url = "http://127.0.0.1:9000/spzx-bucket" + filename;return url;}catch(Exception e){e.printStackTrace();throw new GuiguiException(ResultCodeEnum.SYSTEM_ERROR);}}}
代码优化:
①、将固定的ip端口等放到配置文件中
spzx:minio:endpointUrl: http://127.0.0.1:9000accessKey: minioadminsecreKey: minioadminbucketName: spzx-bucket
②、创建一个类读取配置文件
@Data
@ConfigurationProperties(prefix = "spzx.minio")
public class MinioProperties{//其中成员变量名必须和配置文件中的名一致private String endpointUrl;private Strin accessKey;private String secreKey;private String bucketName;
}
③、启动类中添加注解@EnableConfigurationProperties(value={MinioProperties.class})
④、使用
@Autowired
private MinioProperties minioProperties;//通过get方法获取属性值
上传文件名称唯一性优化,根据当前日期对上传文件进行分:
//通过工具类DateUtil生成对应格式的目录
String dateDir = DateUtil.format(new Date(),"yyyyMMdd");
String uuid = UUID.randomUUID().toString().replaceAll("-","");String filename = dateDir +"/"+uuid+file.getOriginalFilename();
前端
<el-uploadclass="avatar-upload"action="http://localhost:8501/admin/system/fileUpload":show-file-list="false":on-success="handleAvatarSuccess":headers="headers"></el-upload><script setup>import {useApp} from '@/pinia/modules/app'const headers = {token:useApp().authprization.token //从pinia中获取token,在进行文件上传时将token设置到请求头中}//图像上传成功以后的事件处理函数const handleAvatarSuccess = (response.uploadFile) => {sysUser.value.avatar = response.data}
</script>