Skip to content

Commit

Permalink
Merge pull request #124 from JNU-econovation/feat/64/s3-image
Browse files Browse the repository at this point in the history
feat: S3 이미지 url 조회
  • Loading branch information
capDoYeonLee authored Jul 23, 2024
2 parents 6e40968 + 3a848a7 commit f992ab3
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 4 deletions.
5 changes: 5 additions & 0 deletions BE/error/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.3'
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
implementation 'software.amazon.awssdk:s3:2.17.28'
implementation "me.paulschwarz:spring-dotenv:4.0.0"
implementation "com.amazonaws:aws-java-sdk-s3:1.12.281"
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
Expand Down
6 changes: 3 additions & 3 deletions BE/error/src/main/java/com/example/demo/ErrorApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableJpaRepositories

@SpringBootApplication
@EnableJpaAuditing
@EnableFeignClients(basePackages = "com.example.demo.auth.infra.oauth.slack.client")
public class ErrorApplication {

public static void main(String[] args) {
SpringApplication.run(ErrorApplication.class, args);
}
}
}

27 changes: 27 additions & 0 deletions BE/error/src/main/java/com/example/demo/image/ImageController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.demo.image;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/images")
public class ImageController {

private final ImageService imageService;

public ImageController(ImageService imageService) {
this.imageService = imageService;
}

@GetMapping
public List<Map<String, Object>> getProfileImages (){
List<Map<String, Object>> profileImageList = imageService.getFileList("styleganresult2");
return profileImageList;
}
}
54 changes: 54 additions & 0 deletions BE/error/src/main/java/com/example/demo/image/ImageService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example.demo.image;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ListObjectsV2Request;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@RequiredArgsConstructor
@Service
public class ImageService {

private final AmazonS3Client amazonS3Client;

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

@Value("${cloud.aws.region.static}")
private String region;

public List<Map<String, Object>> getFileList(String directory) {
List<Map<String, Object>> fileList = new ArrayList<>();

ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request()
.withBucketName(bucketName)
.withPrefix(directory + "/");

ListObjectsV2Result result = amazonS3Client.listObjectsV2(listObjectsV2Request);
List<S3ObjectSummary> objectSummaries = result.getObjectSummaries();

int id = 1;
for (S3ObjectSummary objectSummary : objectSummaries) {
String key = objectSummary.getKey();
if (!key.equals(directory + "/")) {
Map<String, Object> fileInfo = new HashMap<>();
fileInfo.put("id", id++);
fileInfo.put("url", "https://" + bucketName + ".s3." + region + ".amazonaws.com/" + key);
fileList.add(fileInfo);
}
}

Collections.shuffle(fileList);
return fileList.size() > 16 ? fileList.subList(0, 16) : fileList;
}
}
33 changes: 33 additions & 0 deletions BE/error/src/main/java/com/example/demo/image/StorageConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.demo.image;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;

@Configuration
public class StorageConfig {

@Value("${cloud.aws.credentials.accessKey}")
private String accessKey;

@Value("${cloud.aws.credentials.secretKey}")
private String secretKey;

@Value("${cloud.aws.region.static}")
private String region;

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

return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();
}

}
8 changes: 7 additions & 1 deletion BE/error/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# AWS Configuration
cloud.aws.region.static=ap-northeast-2
cloud.aws.stack.auto=false
cloud.aws.credentials.accessKey=${ACCESS_KEY}
cloud.aws.credentials.secretKey=${SECRET_KEY}
cloud.aws.s3.bucket=zepimage0

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/error?serverTimezone=Asia/Seoul&characterEncoding=UTF-8
spring.datasource.username=root
Expand All @@ -11,4 +18,3 @@ spring.data.redis.host=redis
spring.data.redis.port=6379



0 comments on commit f992ab3

Please sign in to comment.