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

[CICD] 로컬 환경에선 LocalStack을 통해 S3를 모킹한다 #101

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build/
!**/src/main/**/build/
!**/src/test/**/build/
.http

localstack/volume
### STS ###
.apt_generated
.classpath
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {
implementation 'org.flywaydb:flyway-mysql:9.16.3'

implementation 'com.amazonaws:aws-java-sdk-s3:1.12.528'
implementation 'org.testcontainers:localstack:1.19.1'

// Querydsl
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
Expand Down
18 changes: 18 additions & 0 deletions localstack/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: "3.8"

services:
localstack:
container_name: "dbq_localstack"
image: localstack/localstack
ports:
- "127.0.0.1:4566:4566" # LocalStack Gateway
- "127.0.0.1:4510-4559:4510-4559" # external services port range
environment:
- DEBUG=${DEBUG-}
- DOCKER_HOST=unix:///var/run/docker.sock
- AWS_ACCESS_KEY_ID=access_key
- AWS_SECRET_ACCESS_KEY=secret_key
- AWS_DEFAULT_REGION=us-east-1
volumes:
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
51 changes: 51 additions & 0 deletions src/main/java/daybyquest/global/config/LocalS3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package daybyquest.global.config;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Profile("local")
@Component
public class LocalS3Config {

private final String accessKey;

private final String secretKey;

private final String region;

private final String bucket;

private final String endpoint;

public LocalS3Config(@Value("${aws.credentials.access-key}") final String accessKey,
@Value("${aws.credentials.secret-key}") final String secretKey,
@Value("${aws.region}") final String region,
@Value("${aws.bucket}") final String bucket, @Value("${aws.endpoint}") final String endpoint) {
this.accessKey = accessKey;
this.secretKey = secretKey;
this.region = region;
this.bucket = bucket;
this.endpoint = endpoint;
}

@Bean
public AmazonS3 amazonS3() {
final EndpointConfiguration endpoint = new EndpointConfiguration(this.endpoint, region);
final AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
final AmazonS3 amazonS3 = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(endpoint)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
amazonS3.createBucket(bucket);
return amazonS3;
}
}
2 changes: 2 additions & 0 deletions src/main/java/daybyquest/global/config/S3Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Profile("!local")
@Configuration
public class S3Config {

Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/application-local-aws.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
aws:
credentials:
access-key: access_key
secret-key: secret_key
region: us-east-1
bucket: test-bucket
endpoint: http://127.0.0.1:4566
4 changes: 2 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ spring:
local:
- data
- web
- aws
- local-aws
prod:
- prod-data
- web
- aws
- prod-aws
performance:
default: local
Loading