-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 31 additions & 162 deletions
193
src/main/java/Journey/Together/external/aws/S3Service.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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을 뿌려준다!! 사진 제대로 안 올라갔으니까 다시 올려라 ... | ||
} | ||
} | ||
} | ||
} |