-
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
1 parent
60b9bab
commit 0668d2b
Showing
10 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
Api/src/main/java/tify/server/api/image/controller/ImageController.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,29 @@ | ||
package tify.server.api.image.controller; | ||
|
||
|
||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import javax.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import tify.server.api.config.security.SecurityUtils; | ||
import tify.server.api.image.model.request.PreSignedUrlRequest; | ||
import tify.server.infrastructure.outer.s3.S3Service; | ||
import tify.server.infrastructure.outer.s3.dto.PreSignedDTO; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Tag(name = "8. [이미지]") | ||
@RequestMapping(value = "/images") | ||
public class ImageController { | ||
|
||
private final S3Service s3Service; | ||
|
||
@PostMapping | ||
public PreSignedDTO getPreSignedUrl(@RequestBody @Valid PreSignedUrlRequest request) { | ||
return s3Service.getPreSignedUrl( | ||
SecurityUtils.getCurrentUserId(), request.getFileExtension()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Api/src/main/java/tify/server/api/image/model/request/PreSignedUrlRequest.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,17 @@ | ||
package tify.server.api.image.model.request; | ||
|
||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import javax.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import tify.server.core.consts.FileExtension; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class PreSignedUrlRequest { | ||
|
||
@Schema(description = "파일의 확장자입니다.", implementation = FileExtension.class) | ||
@NotNull(message = "파일의 확장자를 입력하세요.") | ||
private FileExtension fileExtension; | ||
} |
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
16 changes: 16 additions & 0 deletions
16
Core/src/main/java/tify/server/core/consts/FileExtension.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,16 @@ | ||
package tify.server.core.consts; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum FileExtension { | ||
PNG(".png"), | ||
JPG(".jpg"), | ||
JPEG(".jpeg"), | ||
; | ||
|
||
final String value; | ||
} |
20 changes: 20 additions & 0 deletions
20
Core/src/main/java/tify/server/core/properties/S3Properties.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,20 @@ | ||
package tify.server.core.properties; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@ConfigurationProperties(prefix = "s3") | ||
public class S3Properties { | ||
|
||
private String accessKey; | ||
|
||
private String secretKey; | ||
|
||
private String region; | ||
|
||
private String bucketName; | ||
} |
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
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
52 changes: 52 additions & 0 deletions
52
Infrastructure/src/main/java/tify/server/infrastructure/outer/s3/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,52 @@ | ||
package tify.server.infrastructure.outer.s3; | ||
|
||
import static com.amazonaws.HttpMethod.PUT; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.Headers; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; | ||
import java.net.URL; | ||
import java.util.Date; | ||
import java.util.UUID; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import tify.server.core.consts.FileExtension; | ||
import tify.server.core.properties.S3Properties; | ||
import tify.server.infrastructure.exception.FeignException; | ||
import tify.server.infrastructure.outer.s3.dto.PreSignedDTO; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class S3Service { | ||
|
||
private final AmazonS3 amazonS3Client; | ||
|
||
private final S3Properties s3Properties; | ||
|
||
public PreSignedDTO getPreSignedUrl(Long userId, FileExtension fileExtension) { | ||
String uuidString = UUID.randomUUID().toString(); | ||
String fileName = userId + "-" + uuidString + fileExtension.getValue(); | ||
return generatePreSignedUrl(fileName); | ||
} | ||
|
||
private PreSignedDTO generatePreSignedUrl(String fileName) { | ||
Date date = new Date(); | ||
long time = date.getTime(); | ||
time += 1000 * 60 * 30; | ||
date.setTime(time); | ||
String bucket = s3Properties.getBucketName(); | ||
try { | ||
GeneratePresignedUrlRequest generatePresignedUrlRequest = | ||
new GeneratePresignedUrlRequest(bucket, fileName) | ||
.withMethod(PUT) | ||
.withExpiration(date); | ||
generatePresignedUrlRequest.addRequestParameter( | ||
Headers.S3_CANNED_ACL, CannedAccessControlList.PublicRead.toString()); | ||
URL url = amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest); | ||
return new PreSignedDTO(url.toString(), fileName); | ||
} catch (NullPointerException e) { | ||
throw FeignException.EXCEPTION; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Infrastructure/src/main/java/tify/server/infrastructure/outer/s3/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,29 @@ | ||
package tify.server.infrastructure.outer.s3.config; | ||
|
||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import tify.server.core.properties.S3Properties; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class S3Config { | ||
|
||
private final S3Properties s3Properties; | ||
|
||
@Bean | ||
public AmazonS3Client amazonS3Client() { | ||
BasicAWSCredentials awsCredentials = | ||
new BasicAWSCredentials(s3Properties.getAccessKey(), s3Properties.getSecretKey()); | ||
return (AmazonS3Client) | ||
AmazonS3ClientBuilder.standard() | ||
.withRegion(s3Properties.getRegion()) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.build(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Infrastructure/src/main/java/tify/server/infrastructure/outer/s3/dto/PreSignedDTO.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,14 @@ | ||
package tify.server.infrastructure.outer.s3.dto; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class PreSignedDTO { | ||
|
||
private String imageUrl; | ||
|
||
private String name; | ||
} |