File tree Expand file tree Collapse file tree 3 files changed +33
-4
lines changed
src/main/java/cmf/commitField Expand file tree Collapse file tree 3 files changed +33
-4
lines changed Original file line number Diff line number Diff line change 33import cmf .commitField .domain .pet .entity .Pet ;
44import cmf .commitField .domain .pet .service .PetService ;
55import lombok .RequiredArgsConstructor ;
6+ import org .springframework .http .MediaType ;
67import org .springframework .web .bind .annotation .*;
8+ import org .springframework .web .multipart .MultipartFile ;
79
810import java .util .List ;
911
@@ -15,9 +17,12 @@ public class PetController {
1517 private final PetService petService ;
1618
1719 // ์๋ก์ด ํซ ์ถ๊ฐ
18- @ PostMapping
19- public Pet createPet (@ RequestParam String name , @ RequestParam String imageUrl ) {
20- return petService .createPet (name , imageUrl );
20+ @ PostMapping (consumes = MediaType .MULTIPART_FORM_DATA_VALUE )
21+ public Pet createPet (
22+ @ RequestParam String name ,
23+ @ RequestPart (value = "imageFile" ) MultipartFile imageFile
24+ ) throws Exception {
25+ return petService .createPet (name , imageFile );
2126 }
2227
2328 // ๋ชจ๋ ํซ ์กฐํ
Original file line number Diff line number Diff line change 22
33import cmf .commitField .domain .pet .entity .Pet ;
44import cmf .commitField .domain .pet .repository .PetRepository ;
5+ import cmf .commitField .global .aws .s3 .S3Service ;
56import lombok .RequiredArgsConstructor ;
67import org .springframework .stereotype .Service ;
8+ import org .springframework .web .multipart .MultipartFile ;
79
10+ import java .io .IOException ;
811import java .util .List ;
912import java .util .Optional ;
1013
1316public class PetService {
1417
1518 private final PetRepository petRepository ;
19+ private final S3Service s3Service ;
1620
1721 // ์๋ก์ด ํซ ์์ฑ
18- public Pet createPet (String name , String imageUrl ) {
22+ public Pet createPet (String name , MultipartFile imageFile ) throws IOException {
23+
24+ // โ
S3 ์
๋ก๋ ๋ก์ง ์ถ๊ฐ
25+ String imageUrl = null ;
26+ if (imageFile != null && !imageFile .isEmpty ()) {
27+ imageUrl = s3Service .uploadFile (imageFile , "pet-images" );
28+ }
29+
1930 Pet pet = new Pet (name , imageUrl );
2031 return petRepository .save (pet );
2132 }
Original file line number Diff line number Diff line change 11package cmf .commitField .global .aws .s3 ;
22
3+ import org .springframework .beans .factory .annotation .Value ;
34import org .springframework .context .annotation .Bean ;
45import org .springframework .context .annotation .Configuration ;
6+ import software .amazon .awssdk .auth .credentials .AwsBasicCredentials ;
7+ import software .amazon .awssdk .auth .credentials .StaticCredentialsProvider ;
58import software .amazon .awssdk .regions .Region ;
69import software .amazon .awssdk .services .s3 .S3Client ;
710
811@ Configuration
912public class S3Config {
13+
14+ @ Value ("${aws.access-key-id}" )
15+ private String accessKeyId ;
16+
17+ @ Value ("${aws.secret-access-key}" )
18+ private String secretAccessKey ;
19+
1020 @ Bean
1121 public S3Client s3Client () {
1222 return S3Client .builder ()
1323 .region (Region .AP_NORTHEAST_2 )
24+ .credentialsProvider (StaticCredentialsProvider .create (
25+ AwsBasicCredentials .create (accessKeyId , secretAccessKey )
26+ ))
1427 .build ();
1528 }
1629}
You canโt perform that action at this time.
0 commit comments