Skip to content

Commit

Permalink
Merge pull request #8 from UMC-7-Time-Lapse/feature/#4
Browse files Browse the repository at this point in the history
feat: S3 테스트 완료
  • Loading branch information
oxdjww authored Jul 4, 2024
2 parents 76b57cb + fae48ff commit 032cf42
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ jobs:
spring.datasource.url: ${{ secrets.DB_URL }}
spring.datasource.username: ${{ secrets.DB_USER }}
spring.datasource.password: ${{ secrets.DB_PW }}
cloud.aws.s3.bucket: ${{ secrets.S3_BUCKET }}
cloud.aws.s3.region.static: ${{ secrets.S3_REGION }}
cloud.aws.s3.credentials.accessKey: ${{ secrets.S3_ACCESS_KEY }}
cloud.aws.s3.credentials.secretKey: ${{ secrets.S3_PRIVATE_KEY }}


- name: 4) Grant permission
Expand Down
53 changes: 28 additions & 25 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.7'
id 'io.spring.dependency-management' version '1.1.5'
id 'java'
id 'org.springframework.boot' version '3.2.7'
id 'io.spring.dependency-management' version '1.1.5'
}

group = 'com.hackathon'
version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
// core
implementation 'org.springframework.boot:spring-boot-starter-web'
// core
implementation 'org.springframework.boot:spring-boot-starter-web'

// jpa
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.mysql:mysql-connector-j'
// jpa
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.mysql:mysql-connector-j'

// lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
// lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.4'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-api:2.0.4'
// swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.4'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-api:2.0.4'

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

tasks.named('test') {
useJUnitPlatform()
useJUnitPlatform()
}
30 changes: 30 additions & 0 deletions src/main/java/com/hackathon/TimeLapse/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.hackathon.TimeLapse.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

@Configuration
public class S3Config {
@Value("${cloud.aws.credentials.access-key}")
private String accessKey;
@Value("${cloud.aws.credentials.secret-key}")
private String secretKey;
@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

return (AmazonS3Client)AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(region)
.build();
}
}
38 changes: 37 additions & 1 deletion src/main/java/com/hackathon/TimeLapse/test/TestController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
package com.hackathon.TimeLapse.test;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@Tag(name = "API", description = "Test API 입니다.")
@RequiredArgsConstructor
@RequestMapping("/test")
@RestController
public class TestController {

private final AmazonS3Client amazonS3Client;

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

@GetMapping
@Tag(name = "Test API", description = "Test API 입니다.")
public String test() {
return "test123";
}

@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Tag(name = "S3 Test API", description = "S3 Test API 입니다.")
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
try {
String fileName = file.getOriginalFilename();
String fileUrl = "https://" + bucket + "/test" + fileName;
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(file.getContentType());
metadata.setContentLength(file.getSize());
amazonS3Client.putObject(bucket, fileName, file.getInputStream(), metadata);
return ResponseEntity.ok(fileUrl);
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
}
10 changes: 10 additions & 0 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ spring:
show-sql: true
hibernate:
ddl-auto: create-drop

cloud:
aws:
s3:
bucket: NONE
stack.auto: false
region.static: NONE
credentials:
accessKey: NONE
secretKey: NONE
10 changes: 10 additions & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ spring:
show-sql: true
hibernate:
ddl-auto: create # 첫 배포 이후 validate 으로 수정

cloud:
aws:
s3:
bucket: {bucketname}
stack.auto: false
region.static: {region}
credentials:
accessKey: {accesskey}
secretKey: {secretkey}

0 comments on commit 032cf42

Please sign in to comment.