|
@@ -3,17 +3,10 @@ package com.slodon.b2b2c.upload;
|
|
import com.slodon.b2b2c.core.config.DomainUrlUtil;
|
|
import com.slodon.b2b2c.core.config.DomainUrlUtil;
|
|
import com.slodon.b2b2c.core.exception.MallException;
|
|
import com.slodon.b2b2c.core.exception.MallException;
|
|
import com.slodon.b2b2c.upload.base.Upload;
|
|
import com.slodon.b2b2c.upload.base.Upload;
|
|
-import io.minio.MinioClient;
|
|
|
|
-import io.minio.errors.*;
|
|
|
|
-import io.minio.policy.PolicyType;
|
|
|
|
|
|
+import io.minio.*;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
-import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
-
|
|
|
|
-import java.io.IOException;
|
|
|
|
-import java.security.InvalidKeyException;
|
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* minio上传文件
|
|
* minio上传文件
|
|
@@ -36,32 +29,48 @@ public class MinioUpload extends Upload {
|
|
//获取配置
|
|
//获取配置
|
|
String minioUrl = stringRedisTemplate.opsForValue().get("minio_url");//minio地址
|
|
String minioUrl = stringRedisTemplate.opsForValue().get("minio_url");//minio地址
|
|
int port = Integer.parseInt(stringRedisTemplate.opsForValue().get("minio_port"));//minio端口
|
|
int port = Integer.parseInt(stringRedisTemplate.opsForValue().get("minio_port"));//minio端口
|
|
|
|
+ String minioS3Region = stringRedisTemplate.opsForValue().get("minio_s3_region");//兼容S3时需要设置
|
|
String accessKey = stringRedisTemplate.opsForValue().get("minio_access_key");
|
|
String accessKey = stringRedisTemplate.opsForValue().get("minio_access_key");
|
|
String secretKey = stringRedisTemplate.opsForValue().get("minio_secret_key");
|
|
String secretKey = stringRedisTemplate.opsForValue().get("minio_secret_key");
|
|
|
|
+ String bucketName = stringRedisTemplate.opsForValue().get("minio_bucket_name");//存储桶名称
|
|
|
|
|
|
//从fileName中获取bucketName和objectName
|
|
//从fileName中获取bucketName和objectName
|
|
- String reg = "/(\\S*?)(/\\S*)";
|
|
|
|
- String bucketName = fileName.replaceFirst(reg, "$1");
|
|
|
|
- String objectName = fileName.replaceFirst(reg, "$2");
|
|
|
|
|
|
+// String reg = "/(\\S*?)(/\\S*)";
|
|
|
|
+// String folderName = fileName.replaceFirst(reg, "$1");
|
|
|
|
+// //String objectName = fileName.replaceFirst(reg, "$2");
|
|
|
|
|
|
- //调用api上传文件
|
|
|
|
- MinioClient minioClient = null;
|
|
|
|
try {
|
|
try {
|
|
- minioClient = new MinioClient(minioUrl + ":" + port, accessKey, secretKey);
|
|
|
|
|
|
+ // 1. 配置 MinIO/S3 客户端
|
|
|
|
+ MinioClient minioClient = MinioClient.builder()
|
|
|
|
+ .endpoint(minioUrl, port, true)
|
|
|
|
+ // 替换为您的MinIO服务器地址
|
|
|
|
+ .credentials(accessKey, secretKey) // 您的访问密钥和秘密密钥
|
|
|
|
+ .region(minioS3Region) // 设置区域
|
|
|
|
+ .build();
|
|
//查询桶是否已存在,不存在则创建桶
|
|
//查询桶是否已存在,不存在则创建桶
|
|
- boolean bucketExists = minioClient.bucketExists(bucketName);
|
|
|
|
- if (!bucketExists) {
|
|
|
|
- minioClient.makeBucket(bucketName);
|
|
|
|
- //创建安全级别
|
|
|
|
- minioClient.setBucketPolicy(bucketName, "*", PolicyType.READ_ONLY);
|
|
|
|
|
|
+ // 2. 检查桶是否存在,不存在则创建
|
|
|
|
+ boolean found = minioClient.bucketExists(BucketExistsArgs.builder()
|
|
|
|
+ .bucket(bucketName)
|
|
|
|
+ .build());
|
|
|
|
+
|
|
|
|
+ if (!found) {
|
|
|
|
+ log.info("Bucket does not exist, creating new bucket:{}", bucketName);
|
|
|
|
+ minioClient.makeBucket(MakeBucketArgs.builder()
|
|
|
|
+ .bucket(bucketName)
|
|
|
|
+ .region(minioS3Region)
|
|
|
|
+ .build());
|
|
}
|
|
}
|
|
|
|
+
|
|
//上传文件
|
|
//上传文件
|
|
- minioClient.putObject(bucketName, objectName, file.getInputStream(), file.getContentType());
|
|
|
|
- } catch (InvalidEndpointException | InvalidPortException | InvalidObjectPrefixException |
|
|
|
|
- InvalidKeyException | NoSuchAlgorithmException | NoResponseException |
|
|
|
|
- XmlPullParserException | InvalidBucketNameException | InvalidArgumentException |
|
|
|
|
- RegionConflictException | InsufficientDataException | ErrorResponseException |
|
|
|
|
- InternalException | IOException e) {
|
|
|
|
|
|
+ log.info("Uploading file to bucket...");
|
|
|
|
+ minioClient.putObject(PutObjectArgs.builder()
|
|
|
|
+ .bucket(bucketName)
|
|
|
|
+ .object(fileName)
|
|
|
|
+ .stream(file.getInputStream(), file.getSize(), -1)
|
|
|
|
+ .contentType(file.getContentType())
|
|
|
|
+ .build());
|
|
|
|
+ log.info("File uploaded successfully");
|
|
|
|
+ } catch (Exception e) {
|
|
log.error("上传文件失败", e);
|
|
log.error("上传文件失败", e);
|
|
throw new MallException("上传文件失败");
|
|
throw new MallException("上传文件失败");
|
|
}
|
|
}
|