【若依分离版整合minio第三方服务器实现上传下载功能】
若依分离版整合minio第三方服务器实现上传下载功能前言:本文章只做整合相关内容 至于前期服务器搭建网上有很多详细方法并且很简单 不再赘述1.yml文件配置#文件服务器minio:url: http://192.168.1.***:9000服务器ipaccess-key: minioadmin账号secret-key: minioadmin密码bucket-name: test桶名称2.在RuoY
前言:本文章只做整合相关内容 至于前期服务器搭建网上有很多详细方法 这里不再赘述
1.yml文件配置
#文件服务器
minio:
url: http://192.168.1.***:9000 服务器ip
access-key: minioadmin 账号
secret-key: minioadmin 密码
bucket-name: test 桶名称
2.在RuoYi通用请求类CommonController里调用
/** 获取yml配置类里的桶名称*/
@Value("${minio.bucket-name}")
private String bucketName;
/** 自定义上传Minio服务器方法*/
@PostMapping("/common/uploadMinio")
public AjaxResult uploadFileMinio(MultipartFile file) throws Exception
{
try
{
// 调用上传工具类定义方法,并返回路径名称
String fileName = FileUploadUtils.uploadMinio(file,this.bucketName);
AjaxResult ajax = AjaxResult.success();
ajax.put("fileName", fileName);
return ajax;
}
catch (Exception e)
{
return AjaxResult.error(e.getMessage());
}
}
3.在RuYi上传工具类FileUploadUtils里添加Minio上传方法
/** 上传到Minio服务器*/
public static final String uploadMinio(MultipartFile file,String Name) throws IOException
{
try
{
String bucketName= Name;
return uploadMinino(bucketName,file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
}
catch (Exception e)
{
throw new IOException(e.getMessage(), e);
}
}
4.上传前检验方法
/** 上传前校验方法*/
private static final String uploadMinino(String bucketName, MultipartFile file, String[] allowedExtension)
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
InvalidExtensionException
{
int fileNamelength = file.getOriginalFilename().length();
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
{
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
}
assertAllowed(file, allowedExtension);
try {
String fileName = extractFilename(file);
//调用自定义MinioUtil类方法
String pathFileName = MinioUtil.uploadFile(bucketName, fileName, file);
return pathFileName;
} catch (Exception e){
e.printStackTrace();
}
return "";
}
4.自定义MinioUtil工具类被文件上传时调用
/** 初始化了连接方法,及桶权限获取*/
public class MinioUtil {
/**
* @param bucketName 桶名称
* @param fileName
* @throws IOException
*/
public static String uploadFile(String bucketName,String fileName,MultipartFile multipartFile) throws IOException
{
String url = "";
MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
try (InputStream inputStream = multipartFile.getInputStream()) {
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!found)
{
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
/**
* bucket权限-读写
*/
String READ_WRITE = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\",\"s3:ListBucketMultipartUploads\"],\"Resource\":[\"arn:aws:s3:::" + bucketName + "\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:DeleteObject\",\"s3:GetObject\",\"s3:ListMultipartUploadParts\",\"s3:PutObject\",\"s3:AbortMultipartUpload\"],\"Resource\":[\"arn:aws:s3:::" + bucketName + "/*\"]}]}";
minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(READ_WRITE).build());
}
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName)
.object(fileName)
.stream(inputStream, multipartFile.getSize(), -1)
.contentType(multipartFile.getContentType()).build()
);
//路径获取
url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).
method(Method.GET).build());
url = url.substring(0,url.indexOf('?'));
//常规访问路径获取
return url;
} catch (Exception e) {
throw new IOException(e.getMessage(), e);
}
}
}
5.前端在组件里指定action方法
//上传组件 指定action
<el-upload v-model="form.fileurl"
class="upload-demo"
:action="field101Action"
:on-success="handleUploadSuccess"
:on-error="handleUploadError"
:file-list="fileurlList"
multiple
:limit="1"
:headers="headers"
:on-remove="handleRemove">
<!-- 上传按钮 -->
<el-button size="mini"
type="primary">点击上传</el-button>
</el-upload>
6.指定后端通用请求路径
(这里只需要在RuoYi前端调用后端公共类方法时,换成自己定义的方法即可,不在指向本地路径)
7.上传成功回调
(由于返回的filename格式为服务器名称+日期+文件名
例如:http://192.168.1.***:9000/test/2022/04/12/11shh6h4gmqt0j4prfo3vfmn7e.docx
可根据情况进行截取字符串保存到数据库
这里截取的是000后边的内容 即:/test/2022/04/12/11shh6h4gmqt0j4prfo3vfmn7e.docx
访问时可通过后端读取的Minio服务器名称拼接即可
)
8.下载
(指定url即可,这里采取的是后端返回的服务器ip+字段名称
例如:http://192.168.1.***:9000 + /test/2022/04/12/11shh6h4gmqt0j4prfo3vfmn7e.docx
最后就形成了访问链接,同理图片预览也是一样,
)
注:如出现无法上传的问题,请先确定桶是否开启了public权限
更多推荐
所有评论(0)