1、增加对象存储初始模块
2、优化出省流量统计展示。
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
<module>tongran-job</module>
|
||||
<module>tongran-file</module>
|
||||
<module>tongran-mtragent</module>
|
||||
<module>tongran-storage</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>tongran-modules</artifactId>
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.tongran</groupId>
|
||||
<artifactId>tongran-modules</artifactId>
|
||||
<version>3.6.6</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>tongran-modules-storage</artifactId>
|
||||
|
||||
<description>
|
||||
tongran-modules-storage对象存储
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos Config -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Sentinel -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot Actuator -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot Web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- FastDFS -->
|
||||
<dependency>
|
||||
<groupId>com.github.tobato</groupId>
|
||||
<artifactId>fastdfs-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Minio -->
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
<version>${minio.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- tongran Api System -->
|
||||
<dependency>
|
||||
<groupId>com.tongran</groupId>
|
||||
<artifactId>tongran-api-system</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>s3</artifactId>
|
||||
<version>2.25.27</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.tongran.storage;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
|
||||
/**
|
||||
* 对象存储服务
|
||||
*
|
||||
* @author tongran
|
||||
*/
|
||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
|
||||
public class TongRanStorageApplication
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
SpringApplication.run(TongRanStorageApplication.class, args);
|
||||
System.out.println("对象存储服务模块启动成功");
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.tongran.storage.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class S3Config {
|
||||
|
||||
private final StorageProperties storageProperties;
|
||||
|
||||
@Bean
|
||||
public S3Client s3Client() {
|
||||
return S3Client.builder()
|
||||
.endpointOverride(URI.create(storageProperties.getEndpoint()))
|
||||
.credentialsProvider(StaticCredentialsProvider.create(
|
||||
AwsBasicCredentials.create(
|
||||
storageProperties.getAccessKey(),
|
||||
storageProperties.getSecretKey()
|
||||
)
|
||||
))
|
||||
.region(Region.of(storageProperties.getRegion()))
|
||||
.build();
|
||||
}
|
||||
// 在您的配置类中添加
|
||||
@Bean
|
||||
public S3Presigner s3Presigner(StorageProperties properties) {
|
||||
return S3Presigner.builder()
|
||||
.endpointOverride(URI.create(properties.getEndpoint()))
|
||||
.region(Region.of(storageProperties.getRegion()))
|
||||
.credentialsProvider(StaticCredentialsProvider.create(
|
||||
AwsBasicCredentials.create(properties.getAccessKey(), properties.getSecretKey())))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.tongran.storage.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "storage.rustfs")
|
||||
public class StorageProperties {
|
||||
private String endpoint = "http://localhost:9000";
|
||||
private String accessKey = "minioadmin";
|
||||
private String secretKey = "minioadmin";
|
||||
private String bucketName = "tongran-bucket";
|
||||
private String region = "us-east-1";
|
||||
// 新增配置
|
||||
private DataSize maxTotalSize = DataSize.ofGigabytes(40); // 总空间限制
|
||||
private DataSize chunkSize = DataSize.ofMegabytes(10); // 分片大小
|
||||
private int maxConcurrentParts = 4; // 并发上传分片数
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.tongran.storage.controller;
|
||||
|
||||
import com.tongran.storage.dto.FileInfo;
|
||||
import com.tongran.storage.dto.UploadResult;
|
||||
import com.tongran.storage.service.StorageService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/storage")
|
||||
@RequiredArgsConstructor
|
||||
public class FileController {
|
||||
|
||||
private final StorageService storageService;
|
||||
|
||||
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public ResponseEntity<UploadResult> uploadFile(@RequestParam("file") MultipartFile file) {
|
||||
UploadResult result = storageService.uploadFile(file);
|
||||
if (result.isSuccess()) {
|
||||
return ResponseEntity.ok(result);
|
||||
} else {
|
||||
return ResponseEntity.badRequest().body(result);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/download/{fileName}")
|
||||
public ResponseEntity<byte[]> downloadFile(@PathVariable String fileName) {
|
||||
byte[] fileBytes = storageService.downloadFile(fileName);
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||
"attachment; filename=\"" + fileName + "\"")
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.body(fileBytes);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{fileName}")
|
||||
public ResponseEntity<String> deleteFile(@PathVariable String fileName) {
|
||||
storageService.deleteFile(fileName);
|
||||
return ResponseEntity.ok("文件删除成功: " + fileName);
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
public ResponseEntity<List<FileInfo>> listFiles() {
|
||||
List<FileInfo> files = storageService.listFiles();
|
||||
return ResponseEntity.ok(files);
|
||||
}
|
||||
@GetMapping("/createBucket")
|
||||
public ResponseEntity<String> createBucket(String bucketName) {
|
||||
boolean success = storageService.createBucket(bucketName);
|
||||
if(success){
|
||||
return ResponseEntity.ok("创建成功");
|
||||
}
|
||||
return ResponseEntity.ok("存储桶已存在");
|
||||
}
|
||||
@GetMapping("/generateTempUrl")
|
||||
public ResponseEntity<String> generateTempUrl(String fileName) {
|
||||
String url = storageService.generateTempUrl(fileName);
|
||||
return ResponseEntity.ok(url);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.tongran.storage.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import java.time.Instant;
|
||||
|
||||
@Data
|
||||
public class FileInfo {
|
||||
private String fileName;
|
||||
private Long fileSize;
|
||||
private Instant lastModified;
|
||||
private String etag;
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.tongran.storage.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UploadResult {
|
||||
private boolean success;
|
||||
private String fileName;
|
||||
private String fileUrl;
|
||||
private Long fileSize;
|
||||
private String message;
|
||||
|
||||
public static UploadResult success(String fileName, String fileUrl, Long fileSize) {
|
||||
UploadResult result = new UploadResult();
|
||||
result.setSuccess(true);
|
||||
result.setFileName(fileName);
|
||||
result.setFileUrl(fileUrl);
|
||||
result.setFileSize(fileSize);
|
||||
result.setMessage("上传成功");
|
||||
return result;
|
||||
}
|
||||
|
||||
public static UploadResult error(String message) {
|
||||
UploadResult result = new UploadResult();
|
||||
result.setSuccess(false);
|
||||
result.setMessage(message);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.tongran.storage.service;
|
||||
|
||||
import com.tongran.storage.dto.FileInfo;
|
||||
import com.tongran.storage.dto.UploadResult;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface StorageService {
|
||||
UploadResult uploadFile(MultipartFile file);
|
||||
byte[] downloadFile(String fileName);
|
||||
void deleteFile(String fileName);
|
||||
List<FileInfo> listFiles();
|
||||
public String generateTempUrl(String fileName);
|
||||
public boolean createBucket(String bucketName);
|
||||
}
|
||||
+325
@@ -0,0 +1,325 @@
|
||||
// S3StorageServiceImpl.java
|
||||
package com.tongran.storage.service.impl;
|
||||
|
||||
import com.tongran.storage.config.StorageProperties;
|
||||
import com.tongran.storage.dto.FileInfo;
|
||||
import com.tongran.storage.dto.UploadResult;
|
||||
import com.tongran.storage.service.StorageService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.*;
|
||||
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
|
||||
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;
|
||||
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class S3StorageServiceImpl implements StorageService {
|
||||
|
||||
private final S3Client s3Client;
|
||||
private final StorageProperties storageProperties;
|
||||
private final S3Presigner s3Presigner;
|
||||
|
||||
@Override
|
||||
public UploadResult uploadFile(MultipartFile file) {
|
||||
try {
|
||||
// 1. 生成文件名
|
||||
String originalName = file.getOriginalFilename();
|
||||
String extension = getFileExtension(originalName);
|
||||
String fileName = UUID.randomUUID().toString() +
|
||||
(extension != null ? "." + extension : "");
|
||||
|
||||
// 2. 自动判断:小文件直接传,大文件自动分片
|
||||
if (file.getSize() <= storageProperties.getChunkSize().toBytes()) {
|
||||
// 小文件:直接上传
|
||||
return uploadDirect(file, fileName);
|
||||
} else {
|
||||
// 大文件:自动分片上传
|
||||
return uploadWithChunks(file, fileName);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("文件上传失败", e);
|
||||
return UploadResult.error("上传失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 小文件直接上传
|
||||
private UploadResult uploadDirect(MultipartFile file, String fileName) throws IOException {
|
||||
s3Client.putObject(b -> b
|
||||
.bucket(storageProperties.getBucketName())
|
||||
.key(fileName)
|
||||
.contentType(file.getContentType()),
|
||||
RequestBody.fromBytes(file.getBytes())
|
||||
);
|
||||
|
||||
String fileUrl = buildFileUrl(fileName);
|
||||
log.info("小文件直接上传成功: {} ({} bytes)", fileName, file.getSize());
|
||||
|
||||
return UploadResult.success(fileName, fileUrl, file.getSize());
|
||||
}
|
||||
|
||||
// 大文件自动分片上传
|
||||
private UploadResult uploadWithChunks(MultipartFile file, String fileName) throws IOException {
|
||||
long fileSize = file.getSize();
|
||||
long chunkSize = storageProperties.getChunkSize().toBytes(); // 比如10MB
|
||||
String contentType = file.getContentType();
|
||||
String bucket = storageProperties.getBucketName();
|
||||
|
||||
// 1. 自动初始化分片上传
|
||||
String uploadId = s3Client.createMultipartUpload(b -> b
|
||||
.bucket(bucket)
|
||||
.key(fileName)
|
||||
.contentType(contentType)
|
||||
).uploadId();
|
||||
|
||||
log.info("开始分片上传: {} ({} bytes, 分片大小: {} bytes)",
|
||||
fileName, fileSize, chunkSize);
|
||||
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
List<CompletedPart> completedParts = new ArrayList<>();
|
||||
int partNumber = 1;
|
||||
long totalUploaded = 0;
|
||||
|
||||
// 2. 自动读取分片并上传
|
||||
byte[] buffer = new byte[(int) chunkSize];
|
||||
int bytesRead;
|
||||
|
||||
while ((bytesRead = inputStream.read(buffer)) > 0) {
|
||||
byte[] chunkBytes = Arrays.copyOf(buffer, bytesRead);
|
||||
|
||||
// 上传单个分片
|
||||
String etag = uploadPart(uploadId, fileName, partNumber, chunkBytes, bucket);
|
||||
|
||||
completedParts.add(CompletedPart.builder()
|
||||
.partNumber(partNumber)
|
||||
.eTag(etag)
|
||||
.build());
|
||||
|
||||
totalUploaded += bytesRead;
|
||||
partNumber++;
|
||||
|
||||
// 可以在这里记录进度
|
||||
log.debug("上传进度: {}/{} bytes ({}%)",
|
||||
totalUploaded, fileSize,
|
||||
(totalUploaded * 100 / fileSize));
|
||||
}
|
||||
|
||||
// 3. 自动完成上传
|
||||
s3Client.completeMultipartUpload(b -> b
|
||||
.bucket(bucket)
|
||||
.key(fileName)
|
||||
.uploadId(uploadId)
|
||||
.multipartUpload(CompletedMultipartUpload.builder()
|
||||
.parts(completedParts)
|
||||
.build())
|
||||
);
|
||||
|
||||
String fileUrl = buildFileUrl(fileName);
|
||||
log.info("分片上传完成: {} (共{}个分片)", fileName, completedParts.size());
|
||||
|
||||
return UploadResult.success(fileName, fileUrl, fileSize);
|
||||
|
||||
} catch (Exception e) {
|
||||
// 出错时自动取消上传
|
||||
try {
|
||||
s3Client.abortMultipartUpload(b -> b
|
||||
.bucket(bucket)
|
||||
.key(fileName)
|
||||
.uploadId(uploadId)
|
||||
);
|
||||
log.warn("上传失败,已取消分片上传: {}", uploadId);
|
||||
} catch (Exception ex) {
|
||||
log.error("取消分片上传失败", ex);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// 上传单个分片(辅助方法)
|
||||
private String uploadPart(String uploadId, String fileName,
|
||||
int partNumber, byte[] chunkData, String bucket) {
|
||||
UploadPartRequest uploadRequest = UploadPartRequest.builder()
|
||||
.bucket(bucket)
|
||||
.key(fileName)
|
||||
.uploadId(uploadId)
|
||||
.partNumber(partNumber)
|
||||
.build();
|
||||
|
||||
UploadPartResponse response = s3Client.uploadPart(
|
||||
uploadRequest,
|
||||
RequestBody.fromBytes(chunkData)
|
||||
);
|
||||
|
||||
return response.eTag();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] downloadFile(String fileName) {
|
||||
try {
|
||||
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
|
||||
.bucket(storageProperties.getBucketName())
|
||||
.key(fileName)
|
||||
.build();
|
||||
|
||||
return s3Client.getObjectAsBytes(getObjectRequest).asByteArray();
|
||||
|
||||
} catch (S3Exception e) {
|
||||
log.error("下载文件失败: {}", fileName, e);
|
||||
throw new RuntimeException("下载文件失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFile(String fileName) {
|
||||
try {
|
||||
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder()
|
||||
.bucket(storageProperties.getBucketName())
|
||||
.key(fileName)
|
||||
.build();
|
||||
|
||||
s3Client.deleteObject(deleteObjectRequest);
|
||||
log.info("文件删除成功: {}", fileName);
|
||||
|
||||
} catch (S3Exception e) {
|
||||
log.error("删除文件失败: {}", fileName, e);
|
||||
throw new RuntimeException("删除文件失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FileInfo> listFiles() {
|
||||
try {
|
||||
ListObjectsV2Request request = ListObjectsV2Request.builder()
|
||||
.bucket(storageProperties.getBucketName())
|
||||
.build();
|
||||
|
||||
ListObjectsV2Response response = s3Client.listObjectsV2(request);
|
||||
|
||||
return response.contents().stream()
|
||||
.map(s3Object -> {
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
fileInfo.setFileName(s3Object.key());
|
||||
fileInfo.setFileSize(s3Object.size());
|
||||
fileInfo.setLastModified(s3Object.lastModified());
|
||||
fileInfo.setEtag(s3Object.eTag());
|
||||
return fileInfo;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
} catch (S3Exception e) {
|
||||
log.error("获取文件列表失败", e);
|
||||
throw new RuntimeException("获取文件列表失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private String getFileExtension(String fileName) {
|
||||
if (fileName == null || !fileName.contains(".")) {
|
||||
return null;
|
||||
}
|
||||
return fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
}
|
||||
private String buildFileUrl(String fileName) {
|
||||
return String.format("%s/%s/%s",
|
||||
storageProperties.getEndpoint(),
|
||||
storageProperties.getBucketName(),
|
||||
fileName);
|
||||
}
|
||||
/**
|
||||
* 生成24小时有效的临时下载链接
|
||||
* @param fileName 文件名
|
||||
* @return 临时访问URL
|
||||
*/
|
||||
@Override
|
||||
public String generateTempUrl(String fileName) {
|
||||
try {
|
||||
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
|
||||
.bucket(storageProperties.getBucketName())
|
||||
.key(fileName)
|
||||
.build();
|
||||
|
||||
GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder()
|
||||
.getObjectRequest(getObjectRequest)
|
||||
.signatureDuration(Duration.ofHours(24)) // 24小时有效
|
||||
.build();
|
||||
|
||||
PresignedGetObjectRequest presignedRequest = s3Presigner.presignGetObject(presignRequest);
|
||||
String url = presignedRequest.url().toString();
|
||||
|
||||
log.info("生成临时访问链接: {} (24小时有效)", fileName);
|
||||
return url;
|
||||
|
||||
} catch (S3Exception e) {
|
||||
log.error("生成临时链接失败: {}", fileName, e);
|
||||
throw new RuntimeException("生成临时链接失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新的存储桶
|
||||
* @param bucketName 存储桶名称
|
||||
* @return 是否创建成功
|
||||
*/
|
||||
@Override
|
||||
public boolean createBucket(String bucketName) {
|
||||
try {
|
||||
// 检查存储桶是否已存在
|
||||
if (isBucketExist(bucketName)) {
|
||||
log.warn("存储桶已存在: {}", bucketName);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 创建存储桶
|
||||
CreateBucketRequest createBucketRequest = CreateBucketRequest.builder()
|
||||
.bucket(bucketName)
|
||||
.createBucketConfiguration(b -> b
|
||||
.locationConstraint(Region.of(storageProperties.getRegion()).id())
|
||||
)
|
||||
.build();
|
||||
|
||||
s3Client.createBucket(createBucketRequest);
|
||||
log.info("存储桶创建成功: {}", bucketName);
|
||||
return true;
|
||||
|
||||
} catch (S3Exception e) {
|
||||
log.error("创建存储桶失败: {}", bucketName, e);
|
||||
throw new RuntimeException("创建存储桶失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查存储桶是否存在
|
||||
* @param bucketName 存储桶名称
|
||||
* @return 是否存在
|
||||
*/
|
||||
private boolean isBucketExist(String bucketName) {
|
||||
try {
|
||||
HeadBucketRequest headBucketRequest = HeadBucketRequest.builder()
|
||||
.bucket(bucketName)
|
||||
.build();
|
||||
|
||||
s3Client.headBucket(headBucketRequest);
|
||||
return true;
|
||||
} catch (NoSuchBucketException e) {
|
||||
return false;
|
||||
} catch (S3Exception e) {
|
||||
log.error("检查存储桶状态失败: {}", bucketName, e);
|
||||
throw new RuntimeException("检查存储桶状态失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
Spring Boot Version: ${spring-boot.version}
|
||||
Spring Application Name: ${spring.application.name}
|
||||
@@ -0,0 +1,33 @@
|
||||
# Tomcat
|
||||
server:
|
||||
port: 9305
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
application:
|
||||
# 应用名称
|
||||
name: tongran-storage
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: ${spring.cloud.nacos.config.server-addr}
|
||||
namespace: ${spring.cloud.nacos.config.namespace}
|
||||
username: ${spring.cloud.nacos.config.username}
|
||||
password: ${spring.cloud.nacos.config.password}
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 172.16.15.52:8848
|
||||
# server-addr: 172.16.15.103:8848
|
||||
# namespace: public
|
||||
namespace: saas-prod
|
||||
username: nacos
|
||||
password: nacos
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="60 seconds" debug="false">
|
||||
<!-- 日志存放路径 -->
|
||||
<property name="log.path" value="logs/tongran-storage" />
|
||||
<!-- 日志输出格式 -->
|
||||
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
|
||||
|
||||
<!-- 控制台输出 -->
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 系统日志输出 -->
|
||||
<appender name="storage_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/info.log</file>
|
||||
<!-- 循环政策:基于时间创建日志文件 -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 日志文件名格式 -->
|
||||
<fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
<!-- 日志最大的历史 60天 -->
|
||||
<maxHistory>60</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<!-- 过滤的级别 -->
|
||||
<level>INFO</level>
|
||||
<!-- 匹配时的操作:接收(记录) -->
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<!-- 不匹配时的操作:拒绝(不记录) -->
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<appender name="storage_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/error.log</file>
|
||||
<!-- 循环政策:基于时间创建日志文件 -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 日志文件名格式 -->
|
||||
<fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
<!-- 日志最大的历史 60天 -->
|
||||
<maxHistory>60</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<!-- 过滤的级别 -->
|
||||
<level>ERROR</level>
|
||||
<!-- 匹配时的操作:接收(记录) -->
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<!-- 不匹配时的操作:拒绝(不记录) -->
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<!-- 系统模块日志级别控制 -->
|
||||
<logger name="com.tongran" level="info" />
|
||||
<!-- Spring日志级别控制 -->
|
||||
<logger name="org.springframework" level="warn" />
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
<!--系统操作日志-->
|
||||
<root level="info">
|
||||
<appender-ref ref="storage_info" />
|
||||
<appender-ref ref="storage_error" />
|
||||
</root>
|
||||
</configuration>
|
||||
+2
@@ -142,6 +142,7 @@ public class RmResourceRegistrationServiceImpl implements IRmResourceRegistratio
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
if(!childData.isEmpty()){
|
||||
batchSetNetWorkMsg(childData);
|
||||
filteredList.addAll(childData);
|
||||
}
|
||||
// 按 clientId 去重(保留第一个出现的元素)
|
||||
@@ -237,6 +238,7 @@ public class RmResourceRegistrationServiceImpl implements IRmResourceRegistratio
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
if(!childData.isEmpty()){
|
||||
batchSetNetWorkMsg(childData);
|
||||
filteredList.addAll(childData);
|
||||
}
|
||||
// 按 clientId 去重(保留第一个出现的元素)
|
||||
|
||||
Reference in New Issue
Block a user