minion相关

dev2.0
薛宇 2 years ago
parent 6b0a8cf8cc
commit 9b3d6927d1

@ -355,6 +355,13 @@
<artifactId>Java-WebSocket</artifactId>
<version>1.5.4</version>
</dependency>
<!--minio-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>3.0.10</version>
</dependency>
</dependencies>
<build>

@ -0,0 +1,51 @@
package com.glxp.api.config;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@RequiredArgsConstructor
public class MinioConfig {
@Data
@ConfigurationProperties("minio")
@Configuration
public class MinioProperties {
/**
*
*/
private String endpoint;
/**
*
*/
private String accessKey;
/**
*
*/
private String secretKey;
}
/**
*
*
* @return
*/
@Bean
public MinioClient minioClient(MinioProperties minioProperties) {
try {
return new MinioClient(minioProperties.getEndpoint(), minioProperties.getAccessKey(), minioProperties.getSecretKey());
} catch (InvalidEndpointException | InvalidPortException e) {
throw new RuntimeException(e);
}
}
}

@ -3,6 +3,7 @@ package com.glxp.api.upload;
import com.glxp.api.common.enums.ResultEnum;
import com.glxp.api.common.res.BaseResponse;
import com.glxp.api.common.util.ResultVOUtils;
import com.glxp.api.util.MinioUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
@ -25,6 +26,10 @@ public class uploadController {
@Value("${file_path}")
private String filePath;
@Value("${minio_path}")
private String minioPath;
@Value("${minio_url}")
private String minioUrl;
/**
* PDF
@ -116,21 +121,23 @@ public class uploadController {
return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传文件只能是 jpg,png,doc,pdf 格式");
}
String newName = UUID.randomUUID() + fileType;//生成新文件名
String savePath = filePath + "/register/file/" + type;
String savePath = minioPath + "/register/file/" + type;
String fileFullName = savePath + "/" + newName;
File file1 = new File(savePath);
if (!file1.exists()) {// 判断目录是否存在
file1.mkdirs();// 创建多层目录
}
file1 = new File(savePath + "/" + newName);
// File file1 = new File(savePath);
// if (!file1.exists()) {// 判断目录是否存在
// file1.mkdirs();// 创建多层目录
// }
// file1 = new File(savePath + "/" + newName);
try {
file.transferTo(file1);
// file.transferTo(file1);
MinioUtil.uploadFile(fileFullName, file);
Map<String, String> rMap = new HashMap<>();
rMap.put("msg", "上传成功");
rMap.put("name", newName);
rMap.put("name", minioUrl + fileFullName);
rMap.put("type", type);
return ResultVOUtils.success(rMap);
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
}
return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传失败");

@ -0,0 +1,156 @@
package com.glxp.api.util;
import cn.hutool.core.io.FileUtil;
import io.minio.MinioClient;
import io.minio.errors.*;
import io.minio.http.Method;
import io.minio.policy.PolicyType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* minio
*/
@Slf4j
@Component
public class MinioUtil {
private static MinioClient minioClient;
@Autowired
public void setMinioClient(MinioClient minioClient) {
MinioUtil.minioClient = minioClient;
}
/**
* bucket
*
* @return Boolean
*/
private static void makeBucket(String bucketName) {
try {
boolean exists = minioClient.bucketExists(bucketName);
if (!exists) {
minioClient.makeBucket(bucketName);
minioClient.setBucketPolicy(bucketName, "*", PolicyType.READ_ONLY);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
*
*
* @param fileFullName
* @param file
*/
public static void uploadFile(String fileFullName, MultipartFile file) {
try {
//从fileName中获取bucketName和objectName
String reg = "/(\\S*?)(/\\S*)";
String bucketName = fileFullName.replaceFirst(reg, "$1");
String objectName = fileFullName.replaceFirst(reg, "$2");
makeBucket(bucketName);
minioClient.putObject(bucketName, objectName, file.getInputStream(), FileUtil.getMimeType(fileFullName));
} catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
InvalidKeyException | ErrorResponseException | InternalException | InvalidArgumentException |
XmlPullParserException | NoResponseException e) {
throw new RuntimeException(e);
}
}
/**
*
*
* @param fileFullName
* @param file
* @param fileContentType contentType
* @throws Exception
*/
public static void uploadFile(String fileFullName, InputStream file, String fileContentType) throws InvalidArgumentException, XmlPullParserException, NoResponseException {
try {
//从fileName中获取bucketName和objectName
String reg = "/(\\S*?)(/\\S*)";
String bucketName = fileFullName.replaceFirst(reg, "$1");
String objectName = fileFullName.replaceFirst(reg, "$2");
makeBucket(bucketName);
minioClient.putObject(bucketName, objectName, file, fileContentType);
} catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
InvalidKeyException | ErrorResponseException |
InternalException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
try {
file.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
/**
*
*
* @param fileFullName
* @param expires
* @return 访
*/
public static String getPresignedObjectUrl(String fileFullName, Integer expires) {
String reg = "/(\\S*?)(/\\S*)";
String bucketName = fileFullName.replaceFirst(reg, "$1");
String objectName = fileFullName.replaceFirst(reg, "$2");
try {
return minioClient.getPresignedObjectUrl(Method.GET, bucketName, objectName, expires, null);
} catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException |
InternalException | InvalidExpiresRangeException e) {
throw new RuntimeException(e);
}
}
/**
*
*
* @param fileFullName
* @return inputStream
*/
public static InputStream getFileInputStream(String fileFullName) {
String reg = "/(\\S*?)(/\\S*)";
String bucketName = fileFullName.replaceFirst(reg, "$1");
String objectName = fileFullName.replaceFirst(reg, "$2");
try {
return minioClient.getObject(bucketName, objectName);
} catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException |
InternalException | InvalidArgumentException e) {
throw new RuntimeException(e);
}
}
public static void deleteFile(String fileFullName) {
String reg = "/(\\S*?)(/\\S*)";
String bucketName = fileFullName.replaceFirst(reg, "$1");
String objectName = fileFullName.replaceFirst(reg, "$2");
try {
minioClient.removeObject(bucketName, objectName);
} catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException |
InternalException e) {
// throw new RuntimeException(e);
}
}
}

@ -3,9 +3,9 @@ server:
spring:
datasource:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
jdbc-url: jdbc:p6spy:mysql://127.0.0.1:3306/udi_spms_pt?allowMultiQueries=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
jdbc-url: jdbc:p6spy:mysql://192.168.0.66:3364/udi_spms_px?allowMultiQueries=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: 123456
password: Glxp@6066
hikari:
connection-timeout: 60000
maximum-pool-size: 60
@ -37,6 +37,10 @@ ok:
write-timeout: 3000
max-idle-connections: 200
keep-alive-duration: 300
minio:
endpoint: http://139.9.219.60:9000
access-key: minioadmin
secret-key: minioadmin
logging:
@ -44,8 +48,10 @@ logging:
com.glxp.api.dao: debug
file_path: d:/udi/udiwms/udiwmsfile/
back_file_path: d:/share/udisps/back/
minio_path: /udi
minio_url: http://139.9.219.60:9000
err_path: d:/udi/udiwms/err/
back_file_path: d:/share/udisps/back/
UDI_KEY: 6b137c66-6286-46c6-8efa-c2f5dd9237df
UDI_SERVER_URL: https://www.udims.com/UDI_DL_Server_test
SPMS_KEY: lCOdWCBKS6Kw45wdnnqUTELXyuSKnXEs

Loading…
Cancel
Save