-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore : S3 ์์กด์ฑ ์ถ๊ฐ * feat : Amazon S3 Component ์ถ๊ฐ * feat : S3 ์ ์ ๋ก๋ ๋ Image ์ ์ด๋ฆ์ ์ค์ ํด์ฃผ๋ ImageName ๊ณผ UploadFile ์ถ๊ฐ * feat : S3 ์ ์ ๋ก๋ ๋ Image ์ ์ด๋ฆ์ ์ค์ ํด์ฃผ๋ ImageNae ๊ณผ UploadFile ์ถ๊ฐ * feat : ํ์ผ์ ์ ๋ก๋ํ๊ณ ํด๋นํ๋ URL ์ ๋ฐํํ๋ Service ๊ตฌํ * chore : s3 ํ๊ฒฝ ์ค์ ์ถ๊ฐ * feat : String image -> MultipartFile image ๋ก ๋ณ๊ฒฝ * feat : @RequestPart ์ ์ฉ ๋ฐ S3 upload ๋ก์ง ์ถ๊ฐ * chore : Test ์ Profile ์ค์ * test : ํ ์คํธ ์ S3 ์ ์ ๊ทผํ์ง ์๋๋ก ํ๊ธฐ ์ํด Profile ๋ณ๋ก S3Service Bean ๊ตฌ๋ถ * test : ์ถ๊ฐ๋ Image ์ ์ฅ ๊ธฐ๋ฅ์ ๋ง์ถฐ ์ผ๋ถ Test ์์ * chore : S3, CloudFront ํ๊ฒฝ์ค์ ์ ์ฉ * test : RestDocs ์์ ์ค
- Loading branch information
Showing
28 changed files
with
492 additions
and
53 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
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/com/mapbefine/mapbefine/common/config/S3Config.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.mapbefine.mapbefine.common.config; | ||
|
||
import com.amazonaws.regions.Regions; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class S3Config { | ||
|
||
@Bean | ||
public AmazonS3 amazonS3() { | ||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(Regions.AP_NORTHEAST_2) | ||
.build(); | ||
} | ||
|
||
} |
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
4 changes: 3 additions & 1 deletion
4
backend/src/main/java/com/mapbefine/mapbefine/pin/dto/request/PinImageCreateRequest.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,7 +1,9 @@ | ||
package com.mapbefine.mapbefine.pin.dto.request; | ||
|
||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public record PinImageCreateRequest( | ||
Long pinId, | ||
String imageUrl | ||
MultipartFile image | ||
) { | ||
} |
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
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/com/mapbefine/mapbefine/s3/application/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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.mapbefine.mapbefine.s3.application; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Service | ||
public interface S3Service { | ||
|
||
String upload(MultipartFile multipartFile); | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
backend/src/main/java/com/mapbefine/mapbefine/s3/application/S3ServiceImpl.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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.mapbefine.mapbefine.s3.application; | ||
|
||
import com.mapbefine.mapbefine.s3.domain.S3Client; | ||
import com.mapbefine.mapbefine.s3.domain.UploadFile; | ||
import java.io.IOException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Service | ||
@Profile("!test") | ||
public class S3ServiceImpl implements S3Service { | ||
|
||
@Value("${prefix.upload.path}") | ||
private String prefixUploadPath; | ||
private final S3Client s3Client; | ||
|
||
public S3ServiceImpl(S3Client s3Client) { | ||
this.s3Client = s3Client; | ||
} | ||
|
||
@Override | ||
public String upload(MultipartFile multipartFile) { | ||
try { | ||
UploadFile uploadFile = UploadFile.of(multipartFile); | ||
s3Client.upload(uploadFile); | ||
return getUploadPath(uploadFile); | ||
} catch (IOException exception) { | ||
throw new RuntimeException(exception); | ||
} | ||
} | ||
|
||
private String getUploadPath(final UploadFile uploadFile) { | ||
return String.join( | ||
"/", | ||
prefixUploadPath, | ||
uploadFile.getOriginalFilename() | ||
); | ||
} | ||
|
||
} | ||
|
34 changes: 34 additions & 0 deletions
34
backend/src/main/java/com/mapbefine/mapbefine/s3/domain/ImageName.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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.mapbefine.mapbefine.s3.domain; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class ImageName { | ||
|
||
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSSSS"); | ||
private static final String EXTENSION_DELIMITER = "."; | ||
|
||
private final String fileName; | ||
|
||
private ImageName(String fileName) { | ||
this.fileName = fileName; | ||
} | ||
|
||
public static ImageName from(String originalFileName) { | ||
String fileName = FORMATTER.format(LocalDateTime.now()); | ||
String extension = getExtension(originalFileName); | ||
|
||
return new ImageName(fileName + extension); | ||
} | ||
|
||
private static String getExtension(String originalFileName) { | ||
return originalFileName.substring( | ||
originalFileName.lastIndexOf(EXTENSION_DELIMITER) | ||
); | ||
} | ||
|
||
public String getFileName() { | ||
return fileName; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
backend/src/main/java/com/mapbefine/mapbefine/s3/domain/S3Client.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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.mapbefine.mapbefine.s3.domain; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.DeleteObjectRequest; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Component | ||
public class S3Client { | ||
|
||
@Value("${s3.bucket}") | ||
private String bucket; | ||
private final AmazonS3 amazonS3; | ||
|
||
public S3Client(AmazonS3 amazonS3) { | ||
this.amazonS3 = amazonS3; | ||
} | ||
|
||
public void upload(MultipartFile multipartFile) { | ||
File tempFile = null; | ||
|
||
try { | ||
tempFile = File.createTempFile("upload_", ".tmp"); | ||
multipartFile.transferTo(tempFile); | ||
amazonS3.putObject(new PutObjectRequest(bucket, multipartFile.getOriginalFilename(), tempFile)); | ||
} catch (IOException e) { // TODO: 2023/09/07 Exception ์ ์์ | ||
throw new RuntimeException(e); | ||
} finally { | ||
removeTempFileIfExists(tempFile); | ||
} | ||
} | ||
|
||
private void removeTempFileIfExists(final File tempFile) { | ||
if (tempFile != null && tempFile.exists()) { | ||
tempFile.delete(); | ||
} | ||
} | ||
|
||
public void delete(String key) { | ||
amazonS3.deleteObject(new DeleteObjectRequest(bucket, key)); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
backend/src/main/java/com/mapbefine/mapbefine/s3/domain/UploadFile.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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.mapbefine.mapbefine.s3.domain; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public class UploadFile implements MultipartFile { | ||
|
||
private final String fileName; | ||
private final byte[] bytes; | ||
|
||
private UploadFile( | ||
String fileName, | ||
byte[] bytes | ||
) { | ||
this.fileName = fileName; | ||
this.bytes = bytes; | ||
} | ||
|
||
public static UploadFile of( | ||
MultipartFile multipartFile | ||
) throws IOException { | ||
ImageName imageName = ImageName.from(multipartFile.getOriginalFilename()); | ||
byte[] multipartFileBytes = multipartFile.getBytes(); | ||
|
||
return new UploadFile(imageName.getFileName(), multipartFileBytes); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return fileName; | ||
} | ||
|
||
@Override | ||
public String getOriginalFilename() { | ||
return fileName; | ||
} | ||
|
||
@Override | ||
public String getContentType() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public long getSize() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public byte[] getBytes() throws IOException { | ||
return bytes; | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() throws IOException { | ||
return new ByteArrayInputStream(bytes); | ||
} | ||
|
||
@Override | ||
public Resource getResource() { | ||
return MultipartFile.super | ||
.getResource(); | ||
} | ||
|
||
@Override | ||
public void transferTo(File dest) throws IOException, IllegalStateException { | ||
try (FileOutputStream fileOutputStream = new FileOutputStream(dest)) { | ||
fileOutputStream.write(bytes); | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.