Skip to content

Commit

Permalink
Merge pull request #76 from Team-UMC/feature/#75/large-data-upload
Browse files Browse the repository at this point in the history
[S3] λŒ€μš©λŸ‰ 파일 처리 μ„€μ •
  • Loading branch information
junseokkim authored Feb 15, 2024
2 parents d728a0e + eb64052 commit 1312252
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
@AllArgsConstructor
public enum S3ErrorCode implements ErrorCodeInterface {
// Image
FAILED_UPLOAD_S3_IMAGE(HttpStatus.INTERNAL_SERVER_ERROR, "IMAGE001", "이미지 μ €μž₯에 μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€."),
FAILED_UPLOAD_S3_IMAGE(HttpStatus.INTERNAL_SERVER_ERROR, "IMAGE001", "S3에 이미지 μ €μž₯에 μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€."),
FALIED_READ_IMAGE(HttpStatus.BAD_REQUEST, "IMAGE002","이미지 νŒŒμΌμ„ μ½λŠ” 쀑 λ¬Έμ œκ°€ λ°œμƒν•˜μ˜€μŠ΅λ‹ˆλ‹€."),

// File
FAILED_UPLOAD_S3_FILE(HttpStatus.INTERNAL_SERVER_ERROR, "FILE001", "파일 μ €μž₯에 μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€."),
FAILED_UPLOAD_S3_FILE(HttpStatus.INTERNAL_SERVER_ERROR, "FILE001", "S3에 파일 μ €μž₯에 μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€."),
FALIED_READ_FILE(HttpStatus.BAD_REQUEST, "FILE002","νŒŒμΌμ„ μ½λŠ” 쀑 λ¬Έμ œκ°€ λ°œμƒν•˜μ˜€μŠ΅λ‹ˆλ‹€."),

;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.umc.networkingService.global.utils;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.*;
import com.umc.networkingService.global.common.exception.RestApiException;
import com.umc.networkingService.global.common.exception.code.S3ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;
Expand All @@ -30,17 +28,39 @@ public String uploadFile(String category, MultipartFile multipartFile) {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(multipartFile.getContentType());

// S3에 μ—…λ‘œλ“œ
// 파일 νƒ€μž… 확인
boolean isImage = multipartFile.getContentType() != null && multipartFile.getContentType().startsWith("image");

// λ°”μ΄νŠΈ λ°°μ—΄λ‘œ 파일 λ‚΄μš© 읽기
byte[] bytes;
try {
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, multipartFile.getInputStream(), objectMetadata)
.withCannedAcl(CannedAccessControlList.PublicRead));
bytes = multipartFile.getBytes();
} catch (IOException e) {
// 파일 μ—…λ‘œλ“œ μ‹€νŒ¨ μ‹œ μ—λŸ¬ μ½”λ“œ λΆ„κΈ°
if (isImage) {
throw new RestApiException(S3ErrorCode.FALIED_READ_IMAGE);
} else {
throw new RestApiException(S3ErrorCode.FALIED_READ_FILE);
}
}

// Content-Length μ„€μ •
objectMetadata.setContentLength(bytes.length);

if (multipartFile.getContentType().startsWith("image"))
// ByteArrayInputStream 생성
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

// S3에 μ—…λ‘œλ“œ
try {
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, byteArrayInputStream, objectMetadata)
.withCannedAcl(CannedAccessControlList.PublicRead));
} catch (AmazonS3Exception e) {
// S3 μ—…λ‘œλ“œ μ‹€νŒ¨ μ‹œ μ—λŸ¬ μ½”λ“œ λΆ„κΈ°
if (isImage) {
throw new RestApiException(S3ErrorCode.FAILED_UPLOAD_S3_IMAGE);
else
} else {
throw new RestApiException(S3ErrorCode.FAILED_UPLOAD_S3_FILE);

}
}

return amazonS3Client.getUrl(bucket, fileName).toString();
Expand Down

0 comments on commit 1312252

Please sign in to comment.