S3 - woowa-turkey/miniprojects-2019 GitHub Wiki
-
https ํํ์ API๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ฑฐ๋ ์ถ์ถํ๊ฒ ํด์ฃผ๋ ์น์๋น์ค
-
์น ์์ ์์ญ(bucket)์ ์ฐ๋ฆฌ์ ๋ฐ์ดํฐ(๊ฐ์ฒด)๋ฅผ ์ ์ฅ
-
build.gradle
repositories { mavenCentral() maven { url 'https://repo.spring.io/snapshot' } } ext { set('springCloudVersion', "Greenwich.BUILD-SNAPSHOT") } dependencies { implementation 'org.springframework.cloud:spring-cloud-starter-aws' } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
-
application.yml
cloud: aws: stack: auto: false s3: bucket: woowa-turkey region: static: ap-northeast-2 credentials: accessKey: [๋ฐ๊ธ๋ฐ์ accessKey] secretKey: [๋ฐ๊ธ๋ฐ์ secretKey]
ํ๋ก์ ํธ ๋ฐฐํฌ์ ๊ธฐ๋ณธ์ผ๋ก CloudFormation ๊ตฌ์ฑ์ ์์ํ๊ธฐ ๋๋ฌธ์ ์ค์ ํ CloudFormation์ด ์์ผ๋ฉด ํ๋ก์ ํธ ์คํ์ด ๋์ง ์์. ํด๋น ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ง ์๋๋ก false๋ก ์ค์ . (์ถ์ฒ: https://private.tistory.com/76)
credentials ์ค์ ๊ฐ์ ๊ฒฝ์ฐ git ์ ์ฅ์์ ์ฌ๋ผ๊ฐ ๊ณต๊ฐ๋์ง ์๋๋ก ๋ณ๋์ ํ์ผ์ ์์ฑํ ๋ค์ .gitignore์ ๋ฑ๋กํ ๊ฒ
-
Configuration
@Configuration public class AwsConfig { @Value("${amazonProperties.accessKey}") private String accessKey; @Value("${amazonProperties.secretKey}") private String secretKey; @Value("${cloud.aws.region.static}") private String region; @Bean public BasicAWSCredentials basicAWSCredentials() { return new BasicAWSCredentials(accessKey, secretKey); } @Bean public AmazonS3 amazonS3Client() { return AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) .withRegion(region) .build(); } }
-
S3Uploader
@Component public class S3Uploader { private final AmazonS3Client amazonS3Client; @Value("${cloud.aws.s3.bucket}") private String bucket; private String upload(File uploadFile, String fileName) { amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, uploadFile).withCannedAcl(CannedAccessControlList.PublicRead)); return amazonS3Client.getUrl(bucket, fileName).toString(); } }
-
ํ ์คํธ๋ฅผ ํ ๋, ์ค์ S3 ์ ์ฅ์๋ฅผ ํ์ฉํ๋ ๊ฒ์ด ์๋ Mock์ ํ์ฉํด ํ ์คํธ
-
build.gradle
dependencies { testImplementation 'io.findify:s3mock_2.12:0.2.5' }
-
TestConfiguration
@TestConfiguration public class AwsMockConfig { public static final String S3MOCK_ENDPOINT = "http://localhost:8001"; @Value("${cloud.aws.region.static}") private String region; @Bean public S3Mock s3Mock() { return new S3Mock.Builder().withPort(8001).withInMemoryBackend().build(); } @Bean @Primary public AmazonS3 amazonS3(S3Mock s3Mock) { s3Mock.start(); AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration(S3MOCK_ENDPOINT, region); AmazonS3 client = AmazonS3ClientBuilder .standard() .withPathStyleAccessEnabled(true) .withEndpointConfiguration(endpoint) .withCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials())) .build(); client.createBucket(bucket); return client; } }
-
Example (S3UploaderTest)
@Import(AwsMockConfig.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class S3UploaderTest { @Autowired private S3Uploader s3Uploader; @Autowired private AmazonS3 mockS3Client; @Autowired private String bucket; private ByteArrayResource testFile = new ByteArrayResource(new byte[]{1, 2, 3, 4}) { @Override public String getFilename() { return "test_file.jpg"; } }; private String fileName = "savedFileName"; @Test void ํ์ผ_์ ๋ก๋_ํ ์คํธ() throws IOException { //when String savedUrl = s3Uploader.upload(testFile, fileName); // then String resourceUrl = String.format("%s/%s/%s", S3MOCK_ENDPOINT, bucket, fileName); assertThat(savedUrl).isEqualTo(resourceUrl); } }
https://jojoldu.tistory.com/300
<ํ ์คํธ ๊ด๋ จ>