Skip to content

Commit

Permalink
feat : s3 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
bongsh0112 committed Dec 29, 2023
1 parent 60b9bab commit 0668d2b
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 2 deletions.
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());
}
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import org.springframework.context.annotation.Configuration;
import tify.server.core.properties.JwtProperties;
import tify.server.core.properties.OauthProperties;
import tify.server.core.properties.S3Properties;

@EnableConfigurationProperties({JwtProperties.class, OauthProperties.class})
@EnableConfigurationProperties({JwtProperties.class, OauthProperties.class, S3Properties.class})
@Configuration
public class ConfigurationPropertiesConfig {}
16 changes: 16 additions & 0 deletions Core/src/main/java/tify/server/core/consts/FileExtension.java
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 Core/src/main/java/tify/server/core/properties/S3Properties.java
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;
}
8 changes: 7 additions & 1 deletion Core/src/main/resources/application-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ oauth2:
log-in-key: ${APPLE_LOGIN_KEY}
redirect-url: ${APPLE_REDIRECT}
key-path: ${APPLE_KEY_PATH}
key: ${APPLE_KEY}
key: ${APPLE_KEY}

s3:
region: ${S3_REGION}
access-key: ${S3_ACCESS_KEY}
secret-key: ${S3_SECRET_KEY}
bucket-name: ${S3_BUCKET_NAME}
2 changes: 2 additions & 0 deletions Infrastructure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dependencies {
api 'org.springframework.boot:spring-boot-starter-data-redis'
api 'org.redisson:redisson:3.19.0'
api 'org.seleniumhq.selenium:selenium-java:4.1.2'
api 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
api 'com.amazonaws:aws-java-sdk-s3'
api project(':Core')
}

Expand Down
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;
}
}
}
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();
}
}
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;
}

0 comments on commit 0668d2b

Please sign in to comment.