본문 바로가기

Language/JAVA

Spring Boot DynamoDb연동하기

728x90

Spring Boot DynamoDb연동하기

 

ServerModule.java

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

public abstract class ServerModule {
    @Value("$")
    String awsRegion;

    public abstract AWSCredentialsProvider getCredentialsProvider();

    @Bean
    public AmazonDynamoDB dynamoDB(AWSCredentialsProvider credentialsProvider) {
        return AmazonDynamoDBClientBuilder
                .standard()
                .withCredentials(credentialsProvider)
                .withRegion(this.awsRegion)
                .build();
    }
}
​

 

 

LocalServerModule.java

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.*;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@Profile({"local"})
@Slf4j
@Configuration
@EnableAsync
@EnableScheduling
public class LocalServerModule extends ServerModule {

    @Bean
    @Override
    public AWSCredentialsProvider getCredentialsProvider() {
        log.warn("[DefaultAWSCredentialsProviderChain]");
        return DefaultAWSCredentialsProviderChain.getInstance();
    }
}
 
 
application-local.yml
1
2
aws:
  region: us-west-2
cs
 
 
AwsDynamoDbTest.java
@ActiveProfiles("local")
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class AwsDynamoDbTest {

    @Autowired
    AmazonDynamoDB amazonDynamoDB;

    @Autowired
    AWSCredentialsProvider awsCredentialsProvider;

    private Worker worker;

    @Test
    public void name() {

        String tableName = "<table_name>";
        DescribeTableResult describeTableResult = amazonDynamoDB.describeTable(tableName);
        System.out.println(describeTableResult);

        String streamArn = describeTableResult.getTable().getLatestStreamArn();
        log.info("streamArn:{}",streamArn);
    }
}
​
 
end.
 
 

 

728x90
블로그 주인장입니다. 원하시는 정보는 얻으셨나요? 이 포스트에서 추가로 필요한 정보가 있으시면 여기에 남겨주세요.