Skip to content
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

[BE] 영수증 이미지 업로드 기능 #544

Open
3Juhwan opened this issue Sep 9, 2024 · 0 comments
Open

[BE] 영수증 이미지 업로드 기능 #544

3Juhwan opened this issue Sep 9, 2024 · 0 comments
Assignees
Labels
Milestone

Comments

@3Juhwan
Copy link
Contributor

3Juhwan commented Sep 9, 2024

📄 설명

관리자가 영수증을 첨부할 수 있는 기능을 구현합니다.
1차 목표: 우형 블로그의 방법 중 1가지 적용해서 S3에 영수증을 업로드한다.

🏁 할 일

1차 마일스톤

마일스톤 목표

  • 우형 블로그를 참고해서 가장 공수가 적게 드는 방법부터 적용한다.

⏰ 예상 시간

9월 9일 (1하루)

1차 마일스톤 진행 사항

문제

  • Spring 서버에서 S3로 이미지 업로드할 수 있는지 파악이 필요했다.

해결 방법

  • 로컬에서 AWS S3 SDK implementation 'software.amazon.awssdk:s3:2.25.27'를 사용해서, 서버 -> S3로 이미지를 업로드하는 애플리케이션을 구현했다.
  • GET /api/upload를 요청하면 로컬의 특정 파일이 S3의 /haeng-dong/s3-upload-test 에 업로드 된다.
@RestController
public class S3UploadController {

    private final S3UploadService s3UploadService;

    public S3UploadController(S3UploadService s3UploadService) {
        this.s3UploadService = s3UploadService;
    }

    @GetMapping("/api/upload")
    public String uploadFile() {
        String imageName = "test.png";
        s3UploadService.uploadImageToS3("techcourse-project-2024",
                "haeng-dong/s3-upload-test/test-upload.png",
                new File("/home/ubuntu/" + imageName));
        
        return imageName;
    }
}

@Component
public class S3UploadService {

    private final S3Client s3;

    public S3UploadService() {
        this.s3 = S3Client.builder()
                .region(Region.AP_NORTHEAST_2)
                .build();
    }

    public void uploadImageToS3(String bucketName, String key, File file) {
        PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .bucket(bucketName)
                .key(key)
                .build();

        s3.putObject(putObjectRequest, RequestBody.fromFile(file));
    }
}
  • 로컬에서 jar를 빌드해서 ec2에 올리고 9090포트로 서버를 올리고 테스트를 진행했다.
  • 여기에서 확인할 수 있다.

정리

  • EC2에서 S3에 접근할 수 있는 역할이 부여된 상태, aws agent를 통해서 S3에 접근할 수 있다. 이전에 CloudWatch를 설정할 때, 이미 세팅된 상태
  • S3 이미지를 외부에서 직접 접근할 수 없어서, CloudFront 배포를 이미지 경로로 직접 매핑한 상태

2차 마일스톤

목표

  • 우형의 앞에 2가지 방식 중 하나 적용해서 end-to-end로 파일 업로드되는지 확인해 보기
  • 추가적인 작업 리스트업하고 검토하기
@3Juhwan 3Juhwan added ⌨️ BE Backend ⚙️ feat feature labels Sep 9, 2024
@3Juhwan 3Juhwan added this to the lev4 milestone Sep 9, 2024
@Todari Todari modified the milestones: lev4, v2.0.0 Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: 🚀 In Progress
Development

No branches or pull requests

3 participants