From b3a347d5c1244bd787f236f1e286d77980d2cc85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E6=B2=90?= Date: Wed, 24 Aug 2022 11:04:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eminio=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../storage/properties/MinioStorageProperties.java | 16 +++++++ .../maku/storage/service/MinioStorageService.java | 56 ++++++++++++++++++++++ fast-server/src/main/resources/application.yml | 6 +-- 3 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 fast-boot-system/src/main/java/net/maku/storage/properties/MinioStorageProperties.java create mode 100644 fast-boot-system/src/main/java/net/maku/storage/service/MinioStorageService.java diff --git a/fast-boot-system/src/main/java/net/maku/storage/properties/MinioStorageProperties.java b/fast-boot-system/src/main/java/net/maku/storage/properties/MinioStorageProperties.java new file mode 100644 index 0000000..80d4f90 --- /dev/null +++ b/fast-boot-system/src/main/java/net/maku/storage/properties/MinioStorageProperties.java @@ -0,0 +1,16 @@ +package net.maku.storage.properties; + +import lombok.Data; + +/** + * Minio存储配置项 + * + * @author 阿沐 babamu@126.com + */ +@Data +public class MinioStorageProperties { + private String endPoint; + private String accessKey; + private String secretKey; + private String bucketName; +} diff --git a/fast-boot-system/src/main/java/net/maku/storage/service/MinioStorageService.java b/fast-boot-system/src/main/java/net/maku/storage/service/MinioStorageService.java new file mode 100644 index 0000000..2f5aacb --- /dev/null +++ b/fast-boot-system/src/main/java/net/maku/storage/service/MinioStorageService.java @@ -0,0 +1,56 @@ +package net.maku.storage.service; + +import io.minio.BucketExistsArgs; +import io.minio.MakeBucketArgs; +import io.minio.MinioClient; +import io.minio.PutObjectArgs; +import net.maku.framework.common.exception.FastException; +import net.maku.storage.properties.StorageProperties; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +/** + * Minio存储 + * + * @author 阿沐 babamu@126.com + */ +public class MinioStorageService extends StorageService { + private final MinioClient minioClient; + + public MinioStorageService(StorageProperties properties) { + this.properties = properties; + + minioClient = MinioClient.builder().endpoint(properties.getMinio().getEndPoint()) + .credentials(properties.getMinio().getAccessKey(), properties.getMinio().getSecretKey()).build(); + } + + @Override + public String upload(byte[] data, String path) { + return upload(new ByteArrayInputStream(data), path); + } + + @Override + public String upload(InputStream inputStream, String path) { + try { + //如果BucketName不存在,则创建 + boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(properties.getMinio().getBucketName()).build()); + if (!found) { + minioClient.makeBucket(MakeBucketArgs.builder().bucket(properties.getMinio().getBucketName()).build()); + } + + minioClient.putObject( + PutObjectArgs.builder() + .bucket(properties.getMinio().getBucketName()) + .object(path) + .stream(inputStream, inputStream.available(), -1) + .build() + ); + + } catch (Exception e) { + throw new FastException("上传文件失败:", e); + } + + return properties.getMinio().getEndPoint() + "/" + properties.getMinio().getBucketName() + "/" + path; + } +} diff --git a/fast-server/src/main/resources/application.yml b/fast-server/src/main/resources/application.yml index 71aa526..d727585 100644 --- a/fast-server/src/main/resources/application.yml +++ b/fast-server/src/main/resources/application.yml @@ -24,14 +24,10 @@ spring: storage: enabled: true config: - # 存储类型:local、aliyun + # 存储类型:local、aliyun、tencent、qiniu、huawei、minio type: local - # 访问域名 domain: http://localhost:8080 - # 配置访问前缀 - prefix: local: - # 本地上传路径 path: D://upload mybatis-plus: