使用minio高性能存储图片
在服务器中安装minio
使用docker在服务器中安装minio,命令如下docker pull minio/minio
创建minio的存储文件mkdir -p ~/minio/data
创建容器并运行
docker run -p 9001:9000 -p 9090:9090 --name minio -v ~/minio/data:/data -e "MINIO_ROOT_USER=admin" -e "MINIO_ROOT_PASSWORD=admin123456" -d quay.io/minio/minio server /data --console-address ":9090"
登录账号为admin,密码为admin123456
使用element-plus插入上传图片的代码
<template>
<el-upload
class="avatar-uploader"
action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload"
:headers="headers"
>
<img v-if="imageUrl" :src="imageUrl" class="avatar" />
<el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
</el-upload>
</template>
注意:action路径要改为自己的后端接口,还需要加上请求头属性
import { useApp } from '@/pinia/modules/app'
const headers=ref({
token:useApp().authorization.token
})
后端实现接口
引入依赖
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>8.5.2</version> </dependency>
controller
public class FileUploadController {
@Autowired
FileUploadService fileUploadService;
@PostMapping("fileUpload")
public Result fileUpload(@RequestParam(value = "file")MultipartFile multipartFile){
String fileUrl=fileUploadService.fileUpload(multipartFile);
return Result.ok(fileUrl);
}
}
Service
@Service
public class FileUploadServiceImpl implements FileUploadService {
@Autowired
MinioProperties minioProperties;
@SneakyThrows
@Override
public String fileUpload(MultipartFile multipartFile) {
//客户端
MinioClient minioClient= MinioClient.builder()
.endpoint(minioProperties.getEndpointUrl())
.credentials(minioProperties.getAccessKey(), minioProperties.getSecreKey())
.build();
//判断桶是否存在,如果不存在就创建
boolean found= minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
if (!found){//桶不存在,则创建
minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
}else {
System.out.println("minio桶存在");
}
//设置存储对象
String extFileName= FileNameUtils.getExtension(multipartFile.getOriginalFilename());
String fileName=new SimpleDateFormat("yyyy/MM/dd/")
.format(new Date())+ UUID.randomUUID().toString().replace("-","")+"."+extFileName;
//参数
PutObjectArgs putObjectArgs= PutObjectArgs.builder()
.bucket(minioProperties.getBucketName())
.stream(multipartFile.getInputStream(), multipartFile.getSize(), -1)
.object("avatar/"+fileName)
.build();
//上传
minioClient.putObject(putObjectArgs);
return minioProperties.getEndpointUrl()+"/"+minioProperties.getBucketName()+"/"+"avatar/"+fileName;
}
}
在这里使用了配置文件当中的参数,MinioProperties 读取配置文件当中的参数
@Data
@ConfigurationProperties(prefix = "spzx.minio")
@Component// 前缀不能使用驼峰命名
public class MinioProperties {
private String endpointUrl;
private String accessKey;
private String secreKey;
private String bucketName;
}
#minio参数
spzx:
minio:
endpointUrl: http://192.168.159.30:9001
accessKey: admin
secreKey: admin123456
bucketName: spzx
前端实现上传成功后的回显
//处理上传头像成功
function handleAvatarSuccess(response,file){
sysUser.value.avatar=response.data
//alert(sysUser.value.avatar)
}