본문 바로가기

인프라

Linode Objects Storage 구성 및 Spring Cloud aws와 연동

출처: https://www.linode.com/products/object-storage/

Akamai사에서 Linode를 인수하며, Akamai에서 AWS S3와 같은 Object Storage를 서비스하고 있다.

Naver Cloud도 S3와 호환되는 저장소를 제공하고 Linode도 S3와 호환되는 Object Storage서비스를 제공한다.

 

S3와 호환되는 저장소라면, AWS SDK로 연결이 가능할 터, 연결해 보자..

 


1. Linode Object 만들어보자

 

1. AWS, GCP, NCP 모두 비슷한 구조를 가진 듯하다. Linode도 버킷이라는 명칭을 사용하며 생성할 수 있는 버튼이 존재하니 누르자.

 

2. 버킷의 명칭을 label에 적고, 리전을 골라주면 된다. 2023년 7월 기준 4개 중에서 선택가능하다.

 

3. 버킷을 만드려 하니, 안내문구가 나온다. 요금에 대한 설명이다. aws s3와는 다르게 1TB까지 네트워크비용이 기본요금에 포함되어 있다.

 

4. 버킷에 접글할 때 필요한 액세스키와 시크릿 키를 만들자.

 

5. Create Access Key를 누르면 key를 만들어서 보여준다. 따로 잘 저장해 두자.

 


2. Spring Cloud AWS와 연결

 

Spring Cloud for Amazon Web Services

Spring Cloud for Amazon Web Services is a community-run project. The website is https://awspring.io/ and the source repository is located at https://github.com/awspring/spring-cloud-aws. Spring Cloud for Amazon Web Services, eases the integration with host

spring.io

먼저, 본 글에서 사용하는 spring cloud aws는 amazon sdk를 이용해 spring 환경에서 편하게 사용할 수 있게 도와주는 프로젝트다. AWS의 crendential을 yml설정을 통해 자동으로 bean설정을 도와준다.

단, 해당 프로젝트는 community-run project라고 spring 공식홈페이지에서 공지한다. 3 버전부터는 aws 내 일부 서비스만 사용가능한 점이 아쉬운 점으로 꼽힌다.

 

aws sdk로 linode obect storage연결은 너무 쉽다. s3연결 생성 시, endpoint만 linode로 변경하면 끝나니, 본글에서는 spring cloud aws기반으로 연결해보려 한다.

 

추가로 본문에서의 예시는 spring boot 2.7.12(java 17), gradle 7.6.1을 기반으로 한 코드임을 알린다.

 


1. spring cloud aws를 추가한다.

implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

 

2. application.yml을 세팅한다.

- 예제는 EC2가 아닌 환경에서 구동할 예정임으로 cloud.aws.stack.auto는 false처리하여 자동으로 spring cloud aws 가 AWS CloudFormation를 세팅하는 것을 막는다.

- cloud.aws.region.static은 없을 경우 spring cloud aws가 에러를 유발하기에 linode에서 사용할 기본 리전으로 세팅한다.

cloud:
  aws:
    stack:
      auto: false
    region:
      static: {리전코드}
  linode:
    bucket: {버킷명}
    region: {리전코드}
    credential:
      accessKey: {accessKey}
      secretKey: {secretKey}

 

3. yml에 추가한 설정값을 Java class에 바인딩한다.

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "cloud.linode")
public class LinodeProperties {
    private String bucket;
    private String region;
    private Credentials credentials;
}
public record Credentials(
        String accessKey,
        String secretKey
) {
}

 

4. 바인딩 한 설정값으로 AmazonS3 Bean을 재 정의한다.

@Configuration
@RequiredArgsConstructor
public class LinodeConfig {
    private final LinodeProperties linodeProperties;

    @Bean
    public AmazonS3 amazonS3() {
        AWSCredentials credentials = new BasicAWSCredentials(
                linodeProperties.getCredentials().accessKey(),
                linodeProperties.getCredentials().secretKey()
        );

        return AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("https://ap-south-1.linodeobjects.com", linodeProperties.getRegion()))
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .build();
    }
}

 

5. 원하는 곳에서 AmazonS3을 주입받아서 사용한다.

@Slf4j
@Component
@RequiredArgsConstructor
public class AwsS3 {
    private final AmazonS3 amazonS3;

    /**
     * 단일 파일을 가져온다.
     *
     * @param key
     * @return
     */
    public S3Object getObject(String bucket, String key) {
        return amazonS3.getObject(bucket, key);
    }

}

 


3. 마치며..

AWS와 호환되는 클라우드 환경이면 AWS SDK로 편하게 사용할 수 있다. 

AWS SDK기반으로 개발하면, 어떤 클라우드 환경의 버킷도 대응할 수 있다.

 

AWS SDK가 만능인 건가...