一、minio配置信息,为了方便,这里不写在配置文件直接用枚举
import lombok.AllArgsConstructor; import lombok.Getter; /** * 作者:唐婉 * 时间:2022/8/15 11:28 * 描述:minio配置信息 */ @AllArgsConstructor @Getter public enum MinioEnum { ADDRESS("http://192.168.100.125:9001","minio地址"), BUCKET_NAME("smart-home","存储桶名称"), ACCESS_KEY("minioadmin","访问的key"), SECRET_KEY("minioadmin","访问的秘钥"), AVATAR("avatar","文件在桶中的存放路径"), ; final String value; final String remark; }
依赖:
<!-- https://mvnrepository.com/artifact/io.minio/minio --> <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>8.4.3</version> </dependency> <!-- 官方 miniodemo需要的依赖,没有的话启动会说找不到okhttp啥的;--> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.2</version> </dependency>
二、上传
/** * 作者: 唐婉 * 时间: 2022/8/15 11:18 * 描述: 头像上传 * 官方文档:https://docs.min.io/minio/baremetal/sdk/java/minio-java.html */ @PostMapping("/upLoadAvatar")public String upLoadAvatar(MultipartFile file) { try{ MinioClient minioClient = MinioClient.builder() .endpoint(MinioEnum.ADDRESS.getValue()) .credentials(MinioEnum.ACCESS_KEY.getValue(), MinioEnum.SECRET_KEY.getValue()) .build(); //判断存储桶是否已经存在,不存在的话创建 boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(MinioEnum.BUCKET_NAME.getValue()).build()); //如果存储桶不存在则要创建 if (!exists){ minioClient.makeBucket(MakeBucketArgs.builder().bucket(MinioEnum.BUCKET_NAME.getValue()).build()); } //System.out.println(file.getName()); //System.out.println(file.getOriginalFilename()); minioClient.putObject(PutObjectArgs.builder() .bucket(MinioEnum.BUCKET_NAME.getValue()) .object(MinioEnum.AVATAR.getValue()+"/"+file.getOriginalFilename()) .stream(file.getInputStream(),file.getSize(),-1) .contentType(file.getContentType()) .build());
//服务端需要返回一段json串给客户端,application/Json 非必须有默认的
Map<String, String> reqParams = new HashMap<>(); reqParams.put("response-content-type", "application/json"); //得到临时下载地址 String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() .bucket(MinioEnum.BUCKET_NAME .getValue()).object(MinioEnum.AVATAR.getValue()+"/"+ file.getOriginalFilename()) .method(Method.GET) .extraQueryParams(reqParams) /*非必须有默认的*/ .build());
//自己组装永久下载地址-需要设置桶的权限为ReadAndWrite或只读 String url2=MinioEnum.ADDRESS.getValue()+"/"+MinioEnum.BUCKET_NAME.getValue()+"/"+MinioEnum.AVATAR.getValue()+"/"+file.getOriginalFilename(); return "上传成功了"; }catch (Exception e){ e.printStackTrace(); return "异常了"; } }
三、下载、预览
下载的话可以直接访问上面的地址进行下载
/** * 作者: 唐婉 * 时间: 2022/8/15 11:18 * 描述: 文件下载、预览 * https://docs.min.io/minio/baremetal/sdk/java/minio-java.html */ @GetMapping("/downLoadAvatar") public void upLoadAvatar2(String name,HttpServletResponse response) throws IOException { InputStream in=null; try{ MinioClient minioClient = MinioClient.builder() .endpoint(MinioEnum.ADDRESS.getValue()) .credentials(MinioEnum.ACCESS_KEY.getValue(), MinioEnum.SECRET_KEY.getValue()) .build(); StatObjectResponse statObject = minioClient.statObject(StatObjectArgs.builder() .bucket(MinioEnum.BUCKET_NAME.getValue()) .object(MinioEnum.AVATAR.getValue()+"/"+name) .build()); /** * 描述: content-type 指示响应内容的格式 * content-disposition 指示如何处理响应内容。 * 一般有两种方式: * inline:直接在页面显示-预览 * attachment:以附件形式下载-下载 */ response.setContentType(statObject.contentType()); // response.setContentType("image/png"); response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(name, "UTF-8")); // response.setHeader("Content-Disposition", "attachment; filename=" + name); in = minioClient.getObject(GetObjectArgs.builder() .bucket(MinioEnum.BUCKET_NAME.getValue()) .object(MinioEnum.AVATAR.getValue()+"/"+name) .build()); IOUtils.copy(in,response.getOutputStream()); }catch (Exception e){ e.printStackTrace(); } finally { if (in != null) { in.close(); } } }
四、测试预览结果
标签:Minio,MinioEnum,NAME,minio,BUCKET,getValue,file,上传,SDK From: https://www.cnblogs.com/smiles365/p/16607307.html