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

[#1] Fix : S3 설정파일 변경 #3

Merged
merged 1 commit into from
May 22, 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
5 changes: 3 additions & 2 deletions src/main/java/Journey/Together/exception/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ public enum Error {
/**
* 404 NOT FOUND
*/
NOT_FOUND(HttpStatus.NOT_FOUND, "찾을 수 없습니다."),
NOT_FOUND_USER_EXCEPTION(HttpStatus.NOT_FOUND, "찾을 수 없는 유저입니다."),
NOT_FOUND_IMAGE_EXCEPTION(HttpStatus.NOT_FOUND, "s3 서비스에서 이미지를 찾을 수 없습니다."),

/**
* 400 BAD REQUEST EXCEPTION
*/
BAD_REQUEST_VALIDATION(HttpStatus.BAD_REQUEST, "유효한 값으로 요청을 다시 보내주세요."),
BAD_REQUEST_ID(HttpStatus.BAD_REQUEST, "잘못된 id값입니다."),


/**
Expand Down
193 changes: 31 additions & 162 deletions src/main/java/Journey/Together/external/aws/S3Service.java
Original file line number Diff line number Diff line change
@@ -1,188 +1,57 @@
package Journey.Together.external.aws;

import Journey.Together.exception.Error;
import Journey.Together.exception.model.BadRequestException;
import Journey.Together.exception.model.CustomException;
import Journey.Together.exception.model.NotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.GetUrlRequest;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest;

import java.io.IOException;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Component
@Slf4j
public class S3Service {
private final String bucketName;
private final AWSConfig awsConfig;

private static final Long MAX_FILE_SIZE = 5 * 1024 * 1024L;
private static final Long PRE_SIGNED_URL_EXPIRE_MINUTE = 1L;
private final String bucketName;
private final AWSConfig awsConfig;

public S3Service(@Value("${aws-property.s3-bucket-name}") final String bucketName, AWSConfig awsConfig) {
this.bucketName = bucketName;
this.awsConfig = awsConfig;
}


public S3Service(@Value("${aws-property.s3-bucket-name}") final String bucketName, AWSConfig awsConfig) {
this.bucketName = bucketName;
this.awsConfig = awsConfig;
}
public String uploadImage(String directoryPath, MultipartFile image) throws IOException {
final String key = directoryPath + generateImageFileName();
final S3Client s3Client = awsConfig.getS3Client();

public String uploadImage(MultipartFile multipartFile, String folder) {
final String key = folder + createFileName(multipartFile.getOriginalFilename());
final S3Client s3Client = awsConfig.getS3Client();
PutObjectRequest request = PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.contentType(image.getContentType())
.contentDisposition("inline")
.build();

validateFileSize(multipartFile);
RequestBody requestBody = RequestBody.fromBytes(image.getBytes());
s3Client.putObject(request, requestBody);
return key;
}

PutObjectRequest request = PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.contentType(multipartFile.getContentType())
.contentLength(multipartFile.getSize())
.contentDisposition("inline")
.build();
public void deleteImage(String key) throws IOException {
final S3Client s3Client = awsConfig.getS3Client();

try {
RequestBody requestBody = RequestBody.fromBytes(multipartFile.getBytes());
s3Client.putObject(request, requestBody);
return key;
} catch(IOException e) {
throw new NotFoundException(Error.NOT_FOUND_IMAGE_EXCEPTION, Error.NOT_FOUND_IMAGE_EXCEPTION.getMessage());
}
}
s3Client.deleteObject((DeleteObjectRequest.Builder builder) ->
builder.bucket(bucketName)
.key(key)
.build()
);
}


public List<String> uploadImages(List<MultipartFile> multipartFileList, String folder) {
final S3Client s3Client = awsConfig.getS3Client();
List<String> list = new ArrayList<>();
for (int i = 0; i < multipartFileList.size(); i++) {
String key = folder + createFileName(multipartFileList.get(i).getOriginalFilename());
PutObjectRequest request = PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.contentType(multipartFileList.get(i).getContentType())
.contentLength(multipartFileList.get(i).getSize())
.contentDisposition("inline")
.build();
private String generateImageFileName() {
return UUID.randomUUID().toString() + ".jpg";
}

try {
RequestBody requestBody = RequestBody.fromBytes(multipartFileList.get(i).getBytes());
s3Client.putObject(request, requestBody);
list.add(key);
} catch(IOException e) {
throw new NotFoundException(Error.NOT_FOUND_IMAGE_EXCEPTION, Error.NOT_FOUND_IMAGE_EXCEPTION.getMessage());
}
}
return list;
}

// 파일명 (중복 방지)
private String createFileName(String fileName) {
return UUID.randomUUID().toString().concat(getFileExtension(fileName));
}

// 파일 유효성 검사
private String getFileExtension(String fileName) {
if (fileName.length() == 0) {
throw new NotFoundException(Error.NOT_FOUND_IMAGE_EXCEPTION, Error.NOT_FOUND_IMAGE_EXCEPTION.getMessage());
}
ArrayList<String> fileValidate = new ArrayList<>();
fileValidate.add(".jpg");
fileValidate.add(".jpeg");
fileValidate.add(".png");
fileValidate.add(".JPG");
fileValidate.add(".JPEG");
fileValidate.add(".PNG");
System.out.println("여기여기기");
String idxFileName = fileName.substring(fileName.lastIndexOf("."));
if (!fileValidate.contains(idxFileName)) {
throw new BadRequestException(Error.BAD_REQUEST_FILE_EXTENSION, Error.BAD_REQUEST_FILE_EXTENSION.getMessage());
}
System.out.println("여기기기기기ㅣ기");
return fileName.substring(fileName.lastIndexOf("."));
}

// 이미지 삭제
public void deleteImage(String key) throws IOException {
final S3Client s3Client = awsConfig.getS3Client();

s3Client.deleteObject((DeleteObjectRequest.Builder builder) ->
builder.bucket(bucketName)
.key(key)
.build()
);
}

public void deleteImages(List<String> keys) throws IOException{
final S3Client s3Client = awsConfig.getS3Client();
for (String key : keys) {
s3Client.deleteObject((DeleteObjectRequest.Builder builder) ->
builder.bucket(bucketName)
.key(key)
.build()
);
}
}

private void validateFileSize(MultipartFile image) {
if (image.getSize() > MAX_FILE_SIZE) {
throw new BadRequestException(Error.BAD_REQUEST_FILE_SIZE, Error.BAD_REQUEST_FILE_SIZE.getMessage());
}
}

// 만료시간 1분

public PresignedUrlVO getUploadPreSignedUrl(final String filename, final String folder) {
final String fileName = createFileName(filename);
final String key = folder + fileName;
System.out.println("하이");
log.info(fileName);
log.info(key);

S3Presigner preSigner = awsConfig.getS3Presigner();

PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();
log.info("-----");
PutObjectPresignRequest preSignedUrlRequest = PutObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(PRE_SIGNED_URL_EXPIRE_MINUTE))
.putObjectRequest(putObjectRequest)
.build();
log.info("-----");
String url = preSigner.presignPutObject(preSignedUrlRequest).url().toString();

return PresignedUrlVO.of(fileName, url);
}

public String getURL(String keyName) {
final S3Client s3Client = awsConfig.getS3Client();
try {
GetUrlRequest request = GetUrlRequest.builder()
.bucket(bucketName)
.key(keyName)
.build();

URL url = s3Client.utilities().getUrl(request);
System.out.println("The URL for "+keyName +" is "+ url);
return "The URL for "+keyName +" is "+ url;

} catch (S3Exception e) {
throw new CustomException(Error.UNPROCESSABLE_PRESIGNEDURL_EXCEPTION, Error.UNPROCESSABLE_PRESIGNEDURL_EXCEPTION.getMessage());
// Exception을 뿌려준다!! 사진 제대로 안 올라갔으니까 다시 올려라 ...
}
}
}
}