mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-07-23 09:39:33 +08:00
minio配置优化
This commit is contained in:
parent
43a80c26de
commit
549f761fa9
@ -177,6 +177,55 @@ public class AppConfigCheck implements ApplicationRunner {
|
||||
}
|
||||
},
|
||||
});
|
||||
put(
|
||||
"MinIO",
|
||||
new AppConfig[] {
|
||||
new AppConfig() {
|
||||
{
|
||||
setName("AccessKey");
|
||||
setSort(10);
|
||||
setFieldType(BackendConstant.APP_CONFIG_FIELD_TYPE_TEXT);
|
||||
setKeyName(CConfig.MINIO_ACCESS_KEY);
|
||||
setKeyValue("");
|
||||
}
|
||||
},
|
||||
new AppConfig() {
|
||||
{
|
||||
setName("SecretKey");
|
||||
setSort(20);
|
||||
setFieldType(BackendConstant.APP_CONFIG_FIELD_TYPE_TEXT);
|
||||
setKeyName(CConfig.MINIO_SECRET_KEY);
|
||||
setKeyValue("");
|
||||
}
|
||||
},
|
||||
new AppConfig() {
|
||||
{
|
||||
setName("Bucket");
|
||||
setSort(30);
|
||||
setFieldType(BackendConstant.APP_CONFIG_FIELD_TYPE_TEXT);
|
||||
setKeyName(CConfig.MINIO_BUCKET);
|
||||
setKeyValue("");
|
||||
}
|
||||
},
|
||||
new AppConfig() {
|
||||
{
|
||||
setName("Endpoint");
|
||||
setSort(40);
|
||||
setFieldType(BackendConstant.APP_CONFIG_FIELD_TYPE_TEXT);
|
||||
setKeyName(CConfig.MINIO_ENDPOINT);
|
||||
setKeyValue("");
|
||||
}
|
||||
},
|
||||
new AppConfig() {
|
||||
{
|
||||
setName("Domain");
|
||||
setSort(50);
|
||||
setFieldType(BackendConstant.APP_CONFIG_FIELD_TYPE_TEXT);
|
||||
setKeyName(CConfig.MINIO_DOMAIN);
|
||||
setKeyValue("");
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023 杭州白书科技有限公司
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package xyz.playedu.api.config;
|
||||
|
||||
import io.minio.MinioAsyncClient;
|
||||
import io.minio.MinioClient;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import xyz.playedu.api.vendor.PlayEduMinioClient;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
*
|
||||
* @create 2023/2/28 16:38
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
public class MinioConfig {
|
||||
|
||||
@Value("${minio.domain}")
|
||||
private String domain;
|
||||
|
||||
@Value("${minio.bucket}")
|
||||
private String bucket;
|
||||
|
||||
@Value("${minio.access-key}")
|
||||
private String accessKey;
|
||||
|
||||
@Value("${minio.secret-key}")
|
||||
private String secretKey;
|
||||
|
||||
@Value("${minio.end-point}")
|
||||
private String endPoint;
|
||||
|
||||
@Bean
|
||||
public MinioClient getMinioClient() {
|
||||
return MinioClient.builder()
|
||||
.endpoint(this.endPoint)
|
||||
.credentials(this.accessKey, this.secretKey)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlayEduMinioClient getPlayEduMinioClient() {
|
||||
MinioAsyncClient client =
|
||||
PlayEduMinioClient.builder()
|
||||
.endpoint(this.endPoint)
|
||||
.credentials(this.accessKey, this.secretKey)
|
||||
.build();
|
||||
return new PlayEduMinioClient(client);
|
||||
}
|
||||
}
|
@ -29,4 +29,10 @@ public class CConfig {
|
||||
public static final String SYSTEM_H5_URL = "system.h5_url";
|
||||
|
||||
public static final String MEMBER_DEFAULT_AVATAR = "member.default_avatar";
|
||||
|
||||
public static final String MINIO_ACCESS_KEY = "minio.access_key";
|
||||
public static final String MINIO_SECRET_KEY = "minio.secret_key";
|
||||
public static final String MINIO_BUCKET = "minio.bucket";
|
||||
public static final String MINIO_ENDPOINT = "minio.endpoint";
|
||||
public static final String MINIO_DOMAIN = "minio.domain";
|
||||
}
|
||||
|
@ -36,11 +36,6 @@ import xyz.playedu.api.util.HelperUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
*
|
||||
* @create 2023/2/28 16:26
|
||||
*/
|
||||
@RestController
|
||||
@Slf4j
|
||||
@RequestMapping("/backend/v1/upload")
|
||||
|
@ -18,16 +18,12 @@ package xyz.playedu.api.service;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import xyz.playedu.api.domain.AppConfig;
|
||||
import xyz.playedu.api.types.config.MinioConfig;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【app_config】的数据库操作Service
|
||||
* @createDate 2023-03-09 11:13:33
|
||||
*/
|
||||
public interface AppConfigService extends IService<AppConfig> {
|
||||
|
||||
Map<String, Long> allKeys();
|
||||
@ -37,4 +33,6 @@ public interface AppConfigService extends IService<AppConfig> {
|
||||
void saveFromMap(HashMap<String, String> data);
|
||||
|
||||
Map<String, String> keyValues();
|
||||
|
||||
MinioConfig getMinioConfig();
|
||||
}
|
||||
|
@ -19,9 +19,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import xyz.playedu.api.constant.CConfig;
|
||||
import xyz.playedu.api.domain.AppConfig;
|
||||
import xyz.playedu.api.mapper.AppConfigMapper;
|
||||
import xyz.playedu.api.service.AppConfigService;
|
||||
import xyz.playedu.api.types.config.MinioConfig;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@ -89,4 +91,16 @@ public class AppConfigServiceImpl extends ServiceImpl<AppConfigMapper, AppConfig
|
||||
return list(query().getWrapper().eq("is_hidden", 0)).stream()
|
||||
.collect(Collectors.toMap(AppConfig::getKeyName, AppConfig::getKeyValue));
|
||||
}
|
||||
|
||||
@Override
|
||||
public MinioConfig getMinioConfig() {
|
||||
MinioConfig minioConfig = new MinioConfig();
|
||||
Map<String, String> config = keyValues();
|
||||
minioConfig.setAccessKey(config.get(CConfig.MINIO_ACCESS_KEY));
|
||||
minioConfig.setSecretKey(config.get(CConfig.MINIO_SECRET_KEY));
|
||||
minioConfig.setBucket(config.get(CConfig.MINIO_BUCKET));
|
||||
minioConfig.setEndpoint(config.get(CConfig.MINIO_ENDPOINT));
|
||||
minioConfig.setDomain(config.get(CConfig.MINIO_DOMAIN));
|
||||
return minioConfig;
|
||||
}
|
||||
}
|
||||
|
@ -15,10 +15,7 @@
|
||||
*/
|
||||
package xyz.playedu.api.service.impl;
|
||||
|
||||
import io.minio.GetPresignedObjectUrlArgs;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import io.minio.RemoveObjectArgs;
|
||||
import io.minio.*;
|
||||
import io.minio.http.Method;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
@ -27,8 +24,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import xyz.playedu.api.config.MinioConfig;
|
||||
import xyz.playedu.api.exception.ServiceException;
|
||||
import xyz.playedu.api.service.AppConfigService;
|
||||
import xyz.playedu.api.service.MinioService;
|
||||
import xyz.playedu.api.types.config.MinioConfig;
|
||||
import xyz.playedu.api.vendor.PlayEduMinioClient;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@ -36,22 +35,57 @@ import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
*
|
||||
* @create 2023/3/7 13:29
|
||||
*/
|
||||
@Service
|
||||
public class MinioServiceImpl implements MinioService {
|
||||
|
||||
@Autowired private MinioConfig c;
|
||||
private MinioConfig minioConfig = null;
|
||||
|
||||
@Autowired private MinioClient client;
|
||||
@Autowired private AppConfigService appConfigService;
|
||||
|
||||
@Autowired private PlayEduMinioClient playEduMinioClient;
|
||||
@SneakyThrows
|
||||
private MinioConfig getMinioConfig() {
|
||||
if (minioConfig == null) {
|
||||
minioConfig = appConfigService.getMinioConfig();
|
||||
if (minioConfig.getAccessKey().isBlank()
|
||||
|| minioConfig.getSecretKey().isBlank()
|
||||
|| minioConfig.getBucket().isBlank()
|
||||
|| minioConfig.getDomain().isBlank()
|
||||
|| minioConfig.getEndpoint().isBlank()) {
|
||||
throw new ServiceException("MinIO服务未配置");
|
||||
}
|
||||
}
|
||||
return minioConfig;
|
||||
}
|
||||
|
||||
private String bucket() {
|
||||
return getMinioConfig().getBucket();
|
||||
}
|
||||
|
||||
public MinioClient getMinioClient() {
|
||||
MinioConfig c = getMinioConfig();
|
||||
|
||||
return MinioClient.builder()
|
||||
.endpoint(c.getEndpoint())
|
||||
.credentials(c.getAccessKey(), c.getSecretKey())
|
||||
.build();
|
||||
}
|
||||
|
||||
public PlayEduMinioClient getPlayEduMinioClient() {
|
||||
MinioConfig c = getMinioConfig();
|
||||
|
||||
MinioAsyncClient client =
|
||||
PlayEduMinioClient.builder()
|
||||
.endpoint(c.getEndpoint())
|
||||
.credentials(c.getAccessKey(), c.getSecretKey())
|
||||
.build();
|
||||
|
||||
return new PlayEduMinioClient(client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String url(String path) {
|
||||
MinioConfig c = getMinioConfig();
|
||||
|
||||
return c.getDomain()
|
||||
+ (c.getDomain().endsWith("/") ? "" : "/")
|
||||
+ c.getBucket()
|
||||
@ -63,47 +97,49 @@ public class MinioServiceImpl implements MinioService {
|
||||
@SneakyThrows
|
||||
public String saveFile(MultipartFile file, String savePath, String contentType) {
|
||||
PutObjectArgs objectArgs =
|
||||
PutObjectArgs.builder().bucket(c.getBucket()).object(savePath).stream(
|
||||
PutObjectArgs.builder().bucket(bucket()).object(savePath).stream(
|
||||
file.getInputStream(), file.getSize(), -1)
|
||||
.contentType(contentType)
|
||||
.build();
|
||||
client.putObject(objectArgs);
|
||||
getMinioClient().putObject(objectArgs);
|
||||
|
||||
return url(savePath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadId(String path) {
|
||||
return playEduMinioClient.uploadId(c.getBucket(), path);
|
||||
return getPlayEduMinioClient().uploadId(bucket(), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public String chunkPreSignUrl(String filename, String partNumber, String uploadId) {
|
||||
Map<String, String> extraQueryParams = new HashMap<>();
|
||||
extraQueryParams.put("partNumber", partNumber + "");
|
||||
extraQueryParams.put("partNumber", partNumber);
|
||||
extraQueryParams.put("uploadId", uploadId);
|
||||
|
||||
return client.getPresignedObjectUrl(
|
||||
GetPresignedObjectUrlArgs.builder()
|
||||
.bucket(c.getBucket())
|
||||
.object(filename)
|
||||
.method(Method.PUT)
|
||||
.expiry(60 * 60 * 24)
|
||||
.extraQueryParams(extraQueryParams)
|
||||
.build());
|
||||
return getMinioClient()
|
||||
.getPresignedObjectUrl(
|
||||
GetPresignedObjectUrlArgs.builder()
|
||||
.bucket(bucket())
|
||||
.object(filename)
|
||||
.method(Method.PUT)
|
||||
.expiry(60 * 60 * 24)
|
||||
.extraQueryParams(extraQueryParams)
|
||||
.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String merge(String filename, String uploadId) {
|
||||
playEduMinioClient.merge(c.getBucket(), filename, uploadId);
|
||||
getPlayEduMinioClient().merge(bucket(), filename, uploadId);
|
||||
return url(filename);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public void removeByPath(String path) {
|
||||
client.removeObject(RemoveObjectArgs.builder().bucket(c.getBucket()).object(path).build());
|
||||
getMinioClient()
|
||||
.removeObject(RemoveObjectArgs.builder().bucket(bucket()).object(path).build());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -112,12 +148,12 @@ public class MinioServiceImpl implements MinioService {
|
||||
InputStream inputStream = new ByteArrayInputStream(file);
|
||||
|
||||
PutObjectArgs objectArgs =
|
||||
PutObjectArgs.builder().bucket(c.getBucket()).object(savePath).stream(
|
||||
PutObjectArgs.builder().bucket(bucket()).object(savePath).stream(
|
||||
inputStream, file.length, -1)
|
||||
.contentType(contentType)
|
||||
.build();
|
||||
|
||||
client.putObject(objectArgs);
|
||||
getMinioClient().putObject(objectArgs);
|
||||
|
||||
return url(savePath);
|
||||
}
|
||||
|
27
src/main/java/xyz/playedu/api/types/config/MinioConfig.java
Normal file
27
src/main/java/xyz/playedu/api/types/config/MinioConfig.java
Normal file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2023 杭州白书科技有限公司
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package xyz.playedu.api.types.config;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class MinioConfig {
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
private String bucket;
|
||||
private String endpoint;
|
||||
private String domain;
|
||||
}
|
@ -47,14 +47,6 @@ spring:
|
||||
await-termination: true
|
||||
thread-name-prefix: "playedu-default-thread"
|
||||
|
||||
# Minio
|
||||
minio:
|
||||
access-key: ""
|
||||
secret-key: ""
|
||||
end-point: ""
|
||||
bucket: ""
|
||||
domain: ""
|
||||
|
||||
mybatis:
|
||||
mapper-locations: classpath:mapper/*.xml
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user