-
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.
Browse files
Browse the repository at this point in the history
[feat] s3 사진 등록
- Loading branch information
Showing
10 changed files
with
163 additions
and
7 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
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
39 changes: 39 additions & 0 deletions
39
src/main/java/com/umc/hackaton/snapspot/S3/S3Controller.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,39 @@ | ||
package com.umc.hackaton.snapspot.S3; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/s3") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class S3Controller { | ||
|
||
private final S3Service s3Service; | ||
|
||
@PostMapping("/upload") | ||
public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file) { | ||
try { | ||
String fileUrl = s3Service.saveFile(file); | ||
return ResponseEntity.ok(fileUrl); | ||
} catch (Exception e) { | ||
log.error(e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("S3 업로드에 실패했습니다."); | ||
} | ||
} | ||
|
||
@GetMapping("/file") | ||
public ResponseEntity<?> getFile(@RequestParam("fileName") String fileName) { | ||
try { | ||
String fileUrl = s3Service.getFile(fileName); | ||
return ResponseEntity.ok(fileUrl); | ||
} catch (Exception e) { | ||
log.error(e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("S3 파일 조회에 실패했습니다."); | ||
} | ||
} | ||
} |
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,64 @@ | ||
package com.umc.hackaton.snapspot.S3; | ||
|
||
|
||
import com.amazonaws.SdkClientException; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.AmazonS3Exception; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class S3Service { | ||
|
||
private final AmazonS3 amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
@Transactional | ||
public String saveFile(MultipartFile multipartFile) { | ||
|
||
String originalFilename = multipartFile.getOriginalFilename(); | ||
try { | ||
|
||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentLength(multipartFile.getSize()); | ||
metadata.setContentType(multipartFile.getContentType()); | ||
|
||
amazonS3Client.putObject(new PutObjectRequest(bucket, originalFilename, multipartFile.getInputStream(), metadata)); | ||
return amazonS3Client.getUrl(bucket, originalFilename).toString(); | ||
} catch (IOException e) { | ||
// 파일 입출력 예외 처리 | ||
e.printStackTrace(); | ||
return "File upload failed due to an IO error: " + e.getMessage(); | ||
} catch (AmazonS3Exception e) { | ||
// AWS S3 관련 예외 처리 | ||
e.printStackTrace(); | ||
return "File upload failed due to an Amazon S3 error: " + e.getMessage(); | ||
} catch (SdkClientException e) { | ||
// AWS SDK 관련 클라이언트 예외 처리 | ||
e.printStackTrace(); | ||
return "File upload failed due to an SDK client error: " + e.getMessage(); | ||
} catch (Exception e) { | ||
// 기타 모든 예외 처리 | ||
e.printStackTrace(); | ||
return "File upload failed due to an unexpected error: " + e.getMessage(); | ||
} | ||
} | ||
@Transactional | ||
public String getFile(String fileName) { | ||
return amazonS3Client.getUrl(bucket, fileName).toString(); | ||
|
||
|
||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/umc/hackaton/snapspot/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,36 @@ | ||
package com.umc.hackaton.snapspot.config; | ||
|
||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Slf4j | ||
@Configuration | ||
public class S3Config { | ||
|
||
@Value("${cloud.aws.credentials.access-key}") | ||
private String accessKey; | ||
|
||
@Value("${cloud.aws.credentials.secret-key}") | ||
private String secretKey; | ||
|
||
@Value("${cloud.aws.region.static}") | ||
private String region; | ||
|
||
@Bean | ||
public AmazonS3 amazonS3() { | ||
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
|
||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.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
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
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