-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: S3 테스트 완료
- Loading branch information
Showing
6 changed files
with
119 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/main/java/com/hackathon/TimeLapse/config/S3Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
src/main/java/com/hackathon/TimeLapse/test/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters