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
| @Data @AllArgsConstructor @Slf4j public class MinioUtil {
private String endpoint; private String accessKey; private String secretKey; private String bucketName;
public String upload(MultipartFile file, String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException { MinioClient client = MinioClient.builder() .endpoint(this.endpoint) .credentials(this.accessKey, this.secretKey) .build();
InputStream inputStream = null; try { inputStream = file.getInputStream(); } catch (IOException ex) { throw new RuntimeException(ex); } client.putObject(PutObjectArgs.builder() .bucket(this.bucketName) .object(objectName) .stream(inputStream, file.getSize(), -1) .contentType(file.getContentType()) .build());
return this.endpoint + "/" + this.bucketName + "/" + objectName; }
}
|