注意点1. 能不能做成模版类
2.minio 安装及简单使用。官网文档的查阅
3.MultipartFile. 知识补充
- minio 准备
2023-12-30 minio 安装及简单使用
2.依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.2</version>
</dependency>
3.编写配置类 官网上参考
https://min.io/docs/minio/linux/developers/java/minio-java.html
4.MultipartFile. 知识补充
https://blog.csdn.net/weixin_44874132/article/details/125471482
5。后端代码
1.controller
@RestController
@RequestMapping("/admin/system")
public class FileUploadController {
@Autowired
private FileUploadService fileUploadService;
/**
//<input type="file" name="file" /> java html 中的基础知识 name="file"前端用的框架固定是 file 所以别改
* */
@PostMapping("/fileUpload")
public Result fileUpload(@RequestParam("file") MultipartFile multipartFile){
String url = fileUploadService.fileUpload( multipartFile);
return Result.build(url, ResultCodeEnum.SUCCESS);
}
}
2.server
String fileUpload(MultipartFile multipartFile);
2.1 serverImpl
@Service
public class FileUploadServiceImpl implements FileUploadService {
@Autowired
private MinioProperties minioProperties ;
@Override
public String fileUpload(MultipartFile multipartFile) {
/**
1。// 创建一个Minio的客户端对象 MinioClient minioClient
2。// 判断桶是否存在 if (!found)
// 如果不存在,那么此时就创建一个新的桶
// 如果存在打印信息
3。// 设置存储对象名称 //20230801/443e1e772bef482c95be28704bec58a901.jpg
* */
try {
//1
MinioClient minioClient = MinioClient.builder()
.endpoint(minioProperties.getEndpointUrl())
.credentials(minioProperties.getAccessKey(), minioProperties.getSecreKey())
.build();
//2
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
if (!found) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
} else {
System.out.println("Bucket 'spzx-bucket' already exists.");
}
//3
String dateDir = DateUtil.format(new Date(), "yyyyMMdd");
String uuid = UUID.randomUUID().toString().replace("-", "");
String fileName = dateDir+"/"+uuid+multipartFile.getOriginalFilename();
System.out.println(fileName);
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(minioProperties.getBucketName())
.stream(multipartFile.getInputStream(), multipartFile.getSize(), -1)
.object(fileName)
.build();
minioClient.putObject(putObjectArgs) ;
return minioProperties.getEndpointUrl() + "/" + minioProperties.getBucketName() + "/" + fileName ;
} catch (Exception e) {
throw new RuntimeException(e);
}
/** 官网代码
try {
// 创建一个Minio的客户端对象
MinioClient minioClient = MinioClient.builder()
.endpoint("http://127.0.0.1:9000")
.credentials("minioadmin", "minioadmin")
.build();
boolean found = minioClient.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") ;
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket("spzx-bucket")
.stream(fis, fis.available(), -1)
.object("01.jpg")
.build();
minioClient.putObject(putObjectArgs) ;
// 构建fileUrl
String fileUrl = "http://127.0.0.1:9000/spzx-bucket/01.jpg" ;
System.out.println(fileUrl);
return fileUrl;
} catch (Exception e) {
throw new RuntimeException(e);
}
* */
}
}
3.yaml 配置
spzx:
minio:
endpointUrl: http://127.0.0.1:9000
accessKey: minioadmin
secreKey: minioadmin
bucketName: spzx-bucket
网友评论