KKFileView访问Minio中private的bucket
需求:需要使用KKFileView预览Minio中被private修饰的bucket
方案:使用MinioClient提供的getPresignedObjectUrl方法来申请object的权限
提前在项目中引入minio的包并且进行配置
修改cn.keking.web.controller.OnlinePreviewController中的onlinePreview方法
@GetMapping("/onlinePreview")
public String onlinePreview(String url, Model model, HttpServletRequest req) throws ServerException,
InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException,
InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
String fileUrl;
try {
// 解析路径
fileUrl = WebUtils.decodeUrl(url);
// 修改部分 start
if (fileUrl.contains(endpoint)) {
fileUrl = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
.bucket(bucket)
// 设置访问期限
.expiry(expiry)
// 获取到object
.object(fileUrl.replace("http://" + endpoint + ":" + port + "/" + bucket + "/", ""))
.method(Method.GET)
.build());
}
// 修改部分 end
} catch (Exception ex) {
String errorMsg = String.format(BASE64_DECODE_ERROR_MSG, "url");
return otherFilePreview.notSupportedFile(model, errorMsg);
}
FileAttribute fileAttribute = fileHandlerService.getFileAttribute(fileUrl, req);
model.addAttribute("file", fileAttribute);
// 获取对应文件的预览实现,通过fillAttribute中的type
FilePreview filePreview = previewFactory.get(fileAttribute);
logger.info("预览文件url:{},previewType:{}", fileUrl, fileAttribute.getType());
return filePreview.filePreviewHandle(fileUrl, model, fileAttribute);
}
在properties中添加配置方便在docker中启动时动态配置
#桶名
file.bucket-name=${MINIO_BUCKET_NAME:bucket_name}
#访问地址
file.server.endpoint=${MINIO_SERVER_ENDPOINT:0.0.0.0}
#端口
file.server.port=${MINIO_SERVER_PORT:9000}
#minio用户名
file.server.accessKey=${MINIO_SERVER_ACCESSKEY:username}
#minio密码
file.server.secretKey=${MINIO_SERVER_SECRETKEY:password}
#访问有效期
minio.expiry=${MINIO_EXPIRY:3600}
KKFileView源代码地址:https://gitee.com/kekingcn/file-online-preview?_from=gitee_search