@@ -23,8 +23,11 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@@ -49,6 +52,9 @@ public class CloudStorageService {
|
||||
@Value("${yandex.cloud.bucket}")
|
||||
private String bucket;
|
||||
|
||||
@Value("${yandex.cloud.bucket-cold}")
|
||||
private String bucketCold;
|
||||
|
||||
public void saveFilesToStorage(String mimeType, String extension, String sendToCloudFilePath) {
|
||||
AwsCredentials credentials = AwsBasicCredentials.create(key, secretKey);
|
||||
S3Client s3Client = S3Client.builder()
|
||||
@@ -77,6 +83,77 @@ public class CloudStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> saveSplitPartsToColdStorage(Long userId, Path file1Path, Path file2Path) {
|
||||
AwsCredentials credentials = AwsBasicCredentials.create(key, secretKey);
|
||||
S3Client s3Client = S3Client.builder()
|
||||
.httpClient(ApacheHttpClient.create())
|
||||
.region(Region.of(region))
|
||||
.endpointOverride(URI.create(s3endpoint))
|
||||
.credentialsProvider(StaticCredentialsProvider.create(credentials))
|
||||
.build();
|
||||
|
||||
String part1Key = uploadFileToBucket(s3Client, bucketCold, file1Path, userId, "1");
|
||||
String part2Key = uploadFileToBucket(s3Client, bucketCold, file2Path, userId, "2");
|
||||
|
||||
try {
|
||||
Files.deleteIfExists(file1Path);
|
||||
Files.deleteIfExists(file2Path);
|
||||
log.info("Deleted local split files for userId: {}", userId);
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to delete local split files for userId: {}", userId, e);
|
||||
}
|
||||
|
||||
return Map.of("part1", part1Key, "part2", part2Key);
|
||||
}
|
||||
|
||||
public String downloadPartFromBucket(String bucket, String cloudKey) {
|
||||
AwsCredentials credentials = AwsBasicCredentials.create(key, secretKey);
|
||||
S3Client s3Client = S3Client.builder()
|
||||
.httpClient(ApacheHttpClient.create())
|
||||
.region(Region.of(region))
|
||||
.endpointOverride(URI.create(s3endpoint))
|
||||
.credentialsProvider(StaticCredentialsProvider.create(credentials))
|
||||
.build();
|
||||
|
||||
try {
|
||||
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
|
||||
.bucket(bucket)
|
||||
.key(cloudKey)
|
||||
.build();
|
||||
|
||||
ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(getObjectRequest);
|
||||
String content = new String(objectBytes.asByteArray(), StandardCharsets.UTF_8);
|
||||
|
||||
log.info("Downloaded from cold bucket, key: {}, size: {} chars", cloudKey, content.length());
|
||||
return content;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to download from cold bucket, key: {}", cloudKey, e);
|
||||
throw new RuntimeException("Failed to download file from cold storage", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String uploadFileToBucket(S3Client s3Client, String bucket, Path filePath, Long userId, String partSuffix) {
|
||||
String cloudKey = String.format("passport/%d/part_%s.txt", userId, partSuffix);
|
||||
|
||||
try {
|
||||
byte[] bytes = Files.readAllBytes(filePath);
|
||||
|
||||
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
|
||||
.bucket(bucket)
|
||||
.key(cloudKey)
|
||||
.contentType("text/plain")
|
||||
.build();
|
||||
|
||||
s3Client.putObject(putObjectRequest, RequestBody.fromBytes(bytes));
|
||||
log.info("Uploaded part {} to cold bucket, key: {}", partSuffix, cloudKey);
|
||||
return cloudKey;
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to read file for upload: {}", filePath, e);
|
||||
throw new RuntimeException("Failed to upload split part to cold storage", e);
|
||||
}
|
||||
}
|
||||
|
||||
public File readFileFromStorageByPath(String filePath) throws IOException {
|
||||
GetObjectRequest objectRequest = GetObjectRequest.builder()
|
||||
.bucket(bucket)
|
||||
|
||||
Reference in New Issue
Block a user