KKFileView访问Minio中private的bucket

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值