Skip to content

Commit

Permalink
feat: S3 이미지 조회
Browse files Browse the repository at this point in the history
.env 파일은 환경변수 깃허브에 설정해 놓았습니다. 커밋하지 않았습니다.
  • Loading branch information
westzeroright committed Jul 31, 2024
1 parent 1400cd6 commit d29d402
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 3 deletions.
6 changes: 6 additions & 0 deletions BE/error/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ dependencies {
implementation 'org.hibernate:hibernate-validator:5.2.4.Final'

//implementation 'org.springframework.boot:spring-boot-starter-security'

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"
}

tasks.named('test') {
Expand Down
9 changes: 9 additions & 0 deletions BE/error/src/main/java/com/example/demo/config/EnvConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
@PropertySource("classpath:/.env")
public class EnvConfig {}
34 changes: 34 additions & 0 deletions BE/error/src/main/java/com/example/demo/config/StorageConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.demo.config;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
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("${SECRET_KEY}")
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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example.demo.image.application.service;

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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.demo.image.presentation;

import com.example.demo.image.application.service.ImageService;
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;
}
}
3 changes: 0 additions & 3 deletions BE/error/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ spring.config.activate.on-profile.oauth.provider.slack.clientId=437291124342.714
spring.config.activate.on-profile.oauth.provider.slack.cliientSecret=b7ea56364590159cd00916bd9e03bec3
spring.data.redis.host=redis
spring.data.redis.port=6379



33 changes: 33 additions & 0 deletions BE/error/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
spring:
application:
name: error

jpa:
properties:
hibernate:
format_sql: true
dialect: org.hibernate.dialect.MySQLDialect
show-sql: true
hibernate:
ddl-auto: create

logging:
level:
org:
hibernate:
type:
descriptor:
sql:
BasicBinder: TRACE

cloud:
aws:
region:
static: ap-northeast-2
stack:
auto: false
credentials:
accessKey: ${ACCESS_KEY}
secretKey: ${SECRET_KEY}
s3:
bucket: zepimage0

0 comments on commit d29d402

Please sign in to comment.