栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

minio 配置和java调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

minio 配置和java调用

文章目录
  • 资源访问
  • 用户和权限
    • 策略
    • 用户
    • Service Accounts
  • java 连接 minio

k8s 部署 minio

资源访问

某些资源例如图片,需要可以直接访问

新建桶

上传一张图片上去

点击桶设置

设置 Access Policy 为 public

通过 bitnami/minio 创建的 ingress,默认会指向 minio service,不会指向 9000 端口

需要自己创建新的 ingress 指向 minio service 的 9000 端口才能访问资源

通过 http://shareminio.saas.api.gd-xxx.com/fat/docker.png 可图片资源

 

用户和权限 策略

新建策略

填写策略内容,主要修改 Resource,配置对应桶的访问权限

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:ListAllMyBuckets",
                "s3:ListBucket",
                "s3:PutObject",
                "s3:DeleteObject",
                "s3:GetBucketLocation"
            ],
            "Resource": [
                "arn:aws:s3:::fat",
                "arn:aws:s3:::fat
 
public String getDownloadUrl(String repository, String path, String fileName, int expireSeconrd) {
    try {
        return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                .expiry(expireSeconrd)
                .method(Method.GET)
                .bucket(repository)
                .object(path + fileName).build());
    } catch (Exception e) {
        throw new BaseException("GET_DOWNLOAD_URL_ERROR", "获取下载链接错误!", e);
    }
}

minio 复制文件

 
public String copy(String repository, String path, String fileName, String srcRepository, String srcPath,
                   String srcFileName) {
    try {
        ObjectWriteResponse writeResponse = minioClient.copyObject(CopyObjectArgs.builder()
                .source(CopySource.builder().bucket(srcRepository).object(srcPath + srcFileName).build())
                .bucket(repository)
                .object(path + fileName).build());
        return writeResponse.etag();
    } catch (Exception e) {
        throw new BaseException("COPY_TO_MINIO_FAILED", "上传至Minio出错!", e);
    }
}

minio 删除文件

public void remove(String repository, String path, String fileName) {
    try {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(repository).object(path + fileName).build());
    } catch (Exception e) {
        throw new BaseException("REMOVE_FROM_MINIO_FAILED", "删除Minio文件出错!", e);
    }
}

minio 获取 StatObjectResponse

    public StatObjectResponse getObjectStat(String repository, String path, String fileName) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
        return minioClient.statObject(StatObjectArgs.builder().bucket(repository).object(path + fileName).build()
        );
    }

controller 上传示例

    @ApiOperation(value = "minio", notes = "使用原文件名,相同文件名会覆盖")
    @PostMapping(value = "/minio")
    NjcResponseEntity uploadFile(MultipartFile file) throws Exception {
        storageService.upload(REPOSITORY, "/fat1/", file.getOriginalFilename(), file);
        return super.success("插入成功");
    }

自动生成 /fat1 文件夹,注意:相同文件名会覆盖

controller 下载示例

 @ApiOperation(value = "minio", notes = "获取文件流")
 @GetMapping(value = "/minio")
 void downloadStream(@RequestParam(value = "name") String name,HttpServletResponse response) throws Exception {
     try {
         InputStream inputStream = storageService.download(REPOSITORY,"/", name);
         StatObjectResponse objectStat = storageService.getObjectStat(REPOSITORY,"/", name);

         response.setContentType(objectStat.contentType());
         response.setCharacterEncoding("UTF-8");
         response.setHeader("Content-Disposition",
                 "attachment;filename=" + URLEncoder.encode(name, "UTF-8"));

         IOUtils.copy(inputStream, response.getOutputStream());
         inputStream.close();
     } catch (BaseException bd) {
         log.error("BaseException:", bd);
         response.sendError(500, bd.getMessage());
     }
 }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/949134.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号