1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| package com.manpower.util; import com.manpower.config.MinioConfig; import io.minio.*; import io.minio.http.Method; import io.minio.messages.Bucket; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import org.apache.commons.lang.StringUtils; import javax.annotation.Resource; import java.util.List; @Component @Slf4j public class MinioUtil { @Resource private MinioConfig prop; @Resource private MinioClient minioClient;
public Boolean makeBucket(String bucketName) { try { minioClient.makeBucket(MakeBucketArgs.builder() .bucket(bucketName) .build()); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
public Boolean removeBucket(String bucketName) { try { minioClient.removeBucket(RemoveBucketArgs.builder() .bucket(bucketName) .build()); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
public List<Bucket> getAllBuckets() { try { return minioClient.listBuckets(); } catch (Exception e) { e.printStackTrace(); } return null; }
public Boolean upload(MultipartFile file, String filename) { String originalFilename = file.getOriginalFilename(); if (StringUtils.isBlank(originalFilename)) { throw new RuntimeException(); } try { PutObjectArgs objectArgs = PutObjectArgs .builder() .bucket(prop.getBucketName()) .object(filename) .stream(file.getInputStream(), file.getSize(), -1) .contentType(file.getContentType()) .build(); minioClient.putObject(objectArgs); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
public String preview(String fileName) { GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs .builder() .bucket(prop.getBucketName()) .object(fileName) .method(Method.GET) .build(); try { return minioClient.getPresignedObjectUrl(build); } catch (Exception e) { e.printStackTrace(); } return null; } }
|