Skip to content

Commit

Permalink
feat: 서버에서 이미지 s3 업로드 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
wugawuga committed Sep 11, 2023
1 parent b2e79d9 commit d2ee1e4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.funeat.common.exception;

import com.funeat.exception.CommonErrorCode;
import com.funeat.exception.ErrorCode;
import com.funeat.exception.GlobalException;
import org.springframework.http.HttpStatus;

public class CommonException extends GlobalException {

public CommonException(final HttpStatus status, final ErrorCode errorCode) {
super(status, errorCode);
}

public static class NotAllowedFileExtensionException extends CommonException {
public NotAllowedFileExtensionException(final CommonErrorCode errorCode, final String extension) {
super(errorCode.getStatus(), new ErrorCode<>(errorCode.getCode(), errorCode.getMessage(), extension));
}
}

public static class S3UploadFailException extends CommonException {
public S3UploadFailException(final CommonErrorCode errorCode) {
super(errorCode.getStatus(), new ErrorCode<>(errorCode.getCode(), errorCode.getMessage()));
}
}
}
50 changes: 49 additions & 1 deletion backend/src/main/java/com/funeat/common/s3/S3Uploader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package com.funeat.common.s3;

import static com.funeat.exception.CommonErrorCode.IMAGE_EXTENSION_ERROR_CODE;
import static com.funeat.exception.CommonErrorCode.UNKNOWN_SERVER_ERROR_CODE;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.funeat.common.ImageUploader;
import com.funeat.common.exception.CommonException.NotAllowedFileExtensionException;
import com.funeat.common.exception.CommonException.S3UploadFailException;
import java.io.IOException;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
Expand All @@ -11,6 +21,9 @@
@Profile("!test")
public class S3Uploader implements ImageUploader {

public static final String JPEG = "image/jpeg";
public static final String PNG = "image/png";

@Value("${cloud.aws.s3.bucket}")
private String bucket;

Expand All @@ -25,7 +38,42 @@ public S3Uploader(final AmazonS3 amazonS3) {

@Override
public String upload(final MultipartFile image) {
validateExtension(image);
final ObjectMetadata metadata = getMetadata(image);
final String key = getKey(image);
try {
amazonS3.putObject(getPutObjectRequest(image, key, metadata));
return amazonS3.getUrl(bucket, key).toString();
} catch (IOException e) {
throw new S3UploadFailException(UNKNOWN_SERVER_ERROR_CODE);
}
}

private void validateExtension(final MultipartFile image) {
final String contentType = image.getContentType();
if (!contentType.equals(JPEG) && !contentType.equals(PNG)) {
throw new NotAllowedFileExtensionException(IMAGE_EXTENSION_ERROR_CODE, contentType);
}
}

private String getKey(final MultipartFile image) {
return folder + getRandomImageName(image);
}

private String getRandomImageName(final MultipartFile image) {
return UUID.randomUUID() + image.getOriginalFilename();
}

private ObjectMetadata getMetadata(final MultipartFile image) {
final ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(image.getContentType());
metadata.setContentLength(image.getSize());
return metadata;
}

return null;
private PutObjectRequest getPutObjectRequest(final MultipartFile image, final String key,
final ObjectMetadata metadata) throws IOException {
return new PutObjectRequest(bucket, key, image.getInputStream(), metadata)
.withCannedAcl(CannedAccessControlList.PublicRead);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum CommonErrorCode {

UNKNOWN_SERVER_ERROR_CODE(HttpStatus.INTERNAL_SERVER_ERROR, "알 수 없는 에러입니다.", "0000"),
REQUEST_VALID_ERROR_CODE(HttpStatus.BAD_REQUEST, "요청을 다시 확인해주세요.", "0001"),
IMAGE_EXTENSION_ERROR_CODE(HttpStatus.BAD_REQUEST, "파일 확장자를 확인해주세요.", "0002"),
;

private final HttpStatus status;
Expand Down

0 comments on commit d2ee1e4

Please sign in to comment.