Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: S3 개선 #345

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/core-infra-s3/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
implementation 'com.sksamuel.scrimage:scrimage-core:4.1.1'
implementation 'com.sksamuel.scrimage:scrimage-webp:4.1.1'
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.524'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
}

bootJar { enabled = false }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package com.sponus.coreinfras3;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.reactive.function.client.WebClient;

import com.amazonaws.HttpMethod;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.sponus.coreinfras3.convert.webp.WebpConvertService;
import com.sponus.coreinfras3.util.FileUtils;

Expand Down Expand Up @@ -43,19 +52,6 @@ public String uploadFile(MultipartFile multipartFile) {
return uploadFile(file);
}

public String uploadFile(File file) {
String fileName = getFileNamePrefix() + file.getName();

amazonS3.putObject(new PutObjectRequest(s3Config.getBucket(), s3Config.getFolder() + fileName, file));
try {
Files.delete(file.toPath());
} catch (IOException e) {
throw new IllegalArgumentException("Failed to delete file", e);
}

return amazonS3.getUrl(s3Config.getBucket(), s3Config.getFolder() + fileName).toString();
}

public String deleteFile(String image) {
try {
amazonS3.deleteObject(s3Config.getBucket(), s3Config.getFolder() + image);
Expand All @@ -74,4 +70,59 @@ public List<String> uploadFiles(List<MultipartFile> files) {
private String getFileNamePrefix() {
return UUID.randomUUID().toString().substring(0, 5) + "-";
}

public String generatePreSignUrl(String fileName) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MINUTE, 10); //validfy of 10 minutes
return amazonS3.generatePresignedUrl(s3Config.getBucket(), fileName, calendar.getTime(), HttpMethod.PUT)
.toString();
}

public String uploadFile(File file) {
String fileName = s3Config.getFolder() + getFileNamePrefix() + file.getName();
String preSignedUrl = generatePreSignUrl(fileName);

// log.info("preSignedUrl : {}", preSignedUrl);
try {
uploadFileToS3UsingPreSignedUrl(preSignedUrl, file);
} catch (IOException e) {
throw new IllegalArgumentException("Failed to upload file", e);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Failed to parsing uri");
}

// 로컬 파일 삭제
try {
Files.delete(file.toPath());
} catch (IOException e) {
throw new IllegalArgumentException("Failed to delete file", e);
}

return amazonS3.getUrl(s3Config.getBucket(), fileName).toString();
}

private void uploadFileToS3UsingPreSignedUrl(String preSignedUrl, File file) throws URISyntaxException,
IOException {

URI uri = new URI(preSignedUrl);
WebClient webClient = WebClient.create();

byte[] fileBytes = readFileToBytes(file);

webClient.put()
.uri(uri)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.bodyValue(fileBytes)
.retrieve().toBodilessEntity().block();
}

private byte[] readFileToBytes(File file) throws IOException {
try (FileInputStream fileInputStream = new FileInputStream(file);
FileChannel fileChannel = fileInputStream.getChannel()) {
ByteBuffer byteBuffer = ByteBuffer.allocate((int)fileChannel.size());
fileChannel.read(byteBuffer);
return byteBuffer.array();
}
}
}
Loading