-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#18 feat: S3 API 구현 #23
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 32 additions & 0 deletions
32
place/src/main/java/com/umc/place/common/config/AWSConfig.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,32 @@ | ||
package com.umc.place.common.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
|
||
@Configuration | ||
public class AWSConfig { | ||
|
||
@Value("${cloud.aws.credentials.accessKey}") | ||
private String accessKey; | ||
|
||
@Value("${cloud.aws.credentials.secretKey}") | ||
private String secretKey; | ||
|
||
@Value("${cloud.aws.region.static}") | ||
private String region; | ||
|
||
@Bean | ||
public AmazonS3Client amazonS3Client() { | ||
BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
return (AmazonS3Client) AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)) | ||
.build(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
place/src/main/java/com/umc/place/common/controller/FileUploadController.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,35 @@ | ||
package com.umc.place.common.controller; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.umc.place.common.service.S3Upload; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class FileUploadController { | ||
|
||
private final S3Upload s3Upload; | ||
|
||
@PostMapping("/upload") | ||
public ResponseEntity<Object> uploadFile(MultipartFile[] multipartFileLis) throws IOException { | ||
return ResponseEntity.ok( | ||
s3Upload.upload(multipartFileLis) | ||
); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
place/src/main/java/com/umc/place/common/service/S3Upload.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,79 @@ | ||
package com.umc.place.common.service; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class S3Upload { | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
; | ||
|
||
//https://jforj.tistory.com/261 | ||
public List<String> upload(MultipartFile[] multipartFileList) throws IOException { | ||
List<String> imagePathList = new ArrayList<>(); | ||
|
||
for (MultipartFile multipartFile : multipartFileList) { | ||
String originalName = multipartFile.getOriginalFilename(); // 파일 이름 | ||
long size = multipartFile.getSize(); // 파일 크기 | ||
|
||
ObjectMetadata objectMetaData = new ObjectMetadata(); | ||
objectMetaData.setContentType(multipartFile.getContentType()); | ||
objectMetaData.setContentLength(size); | ||
|
||
// S3에 업로드 | ||
amazonS3Client.putObject( | ||
new PutObjectRequest(bucket, originalName, multipartFile.getInputStream(), objectMetaData) | ||
.withCannedAcl(CannedAccessControlList.PublicRead) | ||
); | ||
|
||
String imagePath = amazonS3Client.getUrl(bucket, originalName).toString(); // 접근가능한 URL 가져오기 | ||
imagePathList.add(imagePath); | ||
} | ||
return imagePathList; | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DB에 이미지 url 저장하는 부분이 추가되어야 할 것 같긴 합니다! |
||
// String s3FileName = UUID.randomUUID() + "-" + multipartFile.getOriginalFilename(); | ||
// | ||
// ObjectMetadata objMeta = new ObjectMetadata(); | ||
// objMeta.setContentLength(multipartFile.getInputStream().available()); | ||
// | ||
// amazonS3.putObject(bucket, s3FileName, multipartFile.getInputStream(), objMeta); | ||
// | ||
// return amazonS3.getUrl(bucket, s3FileName).toString(); | ||
// for(MultipartFile multipartFile: multipartFileList) { | ||
// String originalName = multipartFile.getOriginalFilename(); // 파일 이름 | ||
// long size = multipartFile.getSize(); // 파일 크기 | ||
// | ||
// ObjectMetadata objectMetaData = new ObjectMetadata(); | ||
// objectMetaData.setContentType(multipartFile.getContentType()); | ||
// objectMetaData.setContentLength(size); | ||
// | ||
// // S3에 업로드 | ||
// amazonS3Client.putObject( | ||
// new PutObjectRequest(S3Bucket, originalName, multipartFile.getInputStream(), objectMetaData) | ||
// .withCannedAcl(CannedAccessControlList.PublicRead) | ||
// ); | ||
// | ||
// String imagePath = amazonS3Client.getUrl(S3Bucket, originalName).toString(); // 접근가능한 URL 가져오기 | ||
// imagePathList.add(imagePath); | ||
// } | ||
// | ||
// return new ResponseEntity<Object>(imagePathList, HttpStatus.OK); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저희는 이미지가 여러장이 아니라 그냥 String으로 반환해도 될 것 같아요!