Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

[#40] AWS S3 상에 업로드하는 서비스 구현 #42

Merged
merged 7 commits into from
Apr 7, 2021

Conversation

zeroooooowest
Copy link
Collaborator

@zeroooooowest zeroooooowest commented Apr 5, 2021

보안을 위해 aws.yaml 파일을 따로 만들고 gitignore 설정하였습니다.
yaml 파일에 포함된 해당 credentials 를 가진 aws 사용자에 AmazonS3FullAccess 정책을 주었고
만든 버킷은 퍼블릭 차단을 비활성화 하였습니다.

MultipartFile을 받는 간단한 API인 다음 코드(커밋X)를 작성 후
포스트맨 활용한 테스트를 통해 정상적으로 S3 상에 업로드 됨을 확인하였습니다.

package dabang.star.cafe.api;

import dabang.star.cafe.api.aop.LoginCheck;
import dabang.star.cafe.domain.manager.Role;
import dabang.star.cafe.domain.service.UploadService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequiredArgsConstructor
@RequestMapping("/products/images")
public class ProductImageAdminApi {

    private static final String PRODUCT = "product";
    private final UploadService uploadService;

    @LoginCheck(role = Role.ADMIN)
    @PostMapping
    public String upload(@RequestPart MultipartFile file) throws IOException {
        return uploadService.upload(file, PRODUCT);
    }

}

1. spring-cloud-starter-aws
2. jaxb-api
우선 MultipartFile 을 전달 받아 File로 전환한 뒤
S3에 Public 읽기 권한으로 업로드하고 로컬에 생성된 파일 삭제 후
S3 객체 URL을 반환합니다.
@zeroooooowest zeroooooowest self-assigned this Apr 5, 2021
@zeroooooowest zeroooooowest mentioned this pull request Apr 5, 2021
@zeroooooowest zeroooooowest changed the title AWS S3 상에 업로드하는 서비스 구현 [#40] AWS S3 상에 업로드하는 서비스 구현 Apr 5, 2021
@EnableAspectJAutoProxy
@SpringBootApplication
public class CafeApplication {

private static final String SPRING_CONFIG_LOCATION = "spring.config.location";
private static final String YAMLS = "classpath:/application.yaml,classpath:/aws.yaml";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

application.yaml에 spring-profiles-include를 이용하면 어떨까요🙂

Copy link
Collaborator Author

@zeroooooowest zeroooooowest Apr 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spring.profile.include는 스프링 부트 2.4부터 동작하지 않습니다. spring.config.use-legacy-processing 을 true로 설정하면 사용할 수 있으나 추후 2.5버전부터는 deprecated 될 예정이라 합니다. 링크

제 방식이 아니라면 2.4부터 추가된 spring.config.import 를 고려해볼 수 있을 거 같고 실제 배포 환경에서는 .gitignore에 선언된 파일들은 배포되지 않기 때문에 그 부분을 감안해서라도 조금 수정해보겠습니다.

aws.yaml 파일에 있던 설정 중
cloud.aws.s3.bucket을 application.yaml에 추가

기존에 스프링 실행시 Map 으로 주던 방식에서
spring.config.import=optioanl:aws.yaml추가하는 방식으로 변경

import java.io.IOException;

public interface UploadService {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


private final AmazonS3Client s3Client;

@Value("${cloud.aws.s3.bucket}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Value("${cloud.aws.s3.bucket}")도 생성자 주입이 가능하긴 합니다~

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자를 수동으로 만들면 정상적으로 주입되었으나 롬복 사용해서 생성자 주입을 하니 Value로 받아오지 못하고 Bean을 찾아 주입받으려는 현상이 있었습니다 (https://namocom.tistory.com/663). 이 부분은 일단은 그대로 진행해보겠습니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 롬복을 못쓰긴 합니다 ㅎㅎ 직접 명시하는것도 한 번 고려해보시면 될 것 같네요~

.withCannedAcl(CannedAccessControlList.PublicRead)
);

log.info(uploadFile.delete() ? "파일이 삭제되었습니다." : "파일이 삭제되지 않았습니다.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

단순 성공여부를 남기는 로그는 debug 레벨로 찍는게 나을 것 같네요~

@zeroooooowest zeroooooowest added this to Review in progress in star-dabang Apr 6, 2021
@zeroooooowest zeroooooowest linked an issue Apr 6, 2021 that may be closed by this pull request
star-dabang automation moved this from Review in progress to Reviewer approved Apr 7, 2021
@zeroooooowest zeroooooowest changed the base branch from feature/36 to develop April 7, 2021 15:25
@zeroooooowest zeroooooowest merged commit 02e5e13 into develop Apr 7, 2021
star-dabang automation moved this from Reviewer approved to Done Apr 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

S3 설정
3 participants