SpringBoot EC2 & S3

wangjh789·2022년 9월 10일
0
  • EC2 인바운드 규칙

http 에 대한 IPv4, IPv6 접근 허가 + 외부 아이피로 접근하기 위한 SSH

  • RDS 인바운드 규칙

첫번째는 EC2의 내부통신, 두번째는 테스트를 위한 외부통신

# application.properties
# mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://<엔드포인트>:3306/<DB이름>?serverTimezone=Asia/Seoul&characterEncoding=UTF-8
spring.datasource.username=<ID>
spring.datasource.password=<Password>

# jpa
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
# aws 설정
cloud.aws.stack.auto=false
cloud.aws.credentials.access-key=<IAM 사용자 accessKey>
cloud.aws.credentials.secret-key=<IAM 사용자 secretKey>

cloud.aws.region.static=<지역 이름>
cloud.aws.s3.bucket=<버킷 이름>
@Service
public class S3Service {
    private AmazonS3 s3Client;

    @Value("${cloud.aws.credentials.access-key}")
    private String accessKey;

    @Value("${cloud.aws.credentials.secret-key}")
    private String secretKey;

    @Value("${cloud.aws.s3.bucket}")
    private String bucket;

    @Value("${cloud.aws.region.static}")
    private String region;

    @PostConstruct
    public void setS3Client() {
        AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);

        s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withRegion(this.region)
                .build();
    }

    public String upload(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLanguage(String.valueOf(file.getBytes().length));

        s3Client.putObject(new PutObjectRequest(bucket, fileName, file.getInputStream(), metadata)
                .withCannedAcl(CannedAccessControlList.PublicRead));
        return s3Client.getUrl(bucket, fileName).toString();
    }
}
profile
기록

0개의 댓글