-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from JNU-econovation/feat/64/s3-image
feat: S3 이미지 url 조회
- Loading branch information
Showing
6 changed files
with
129 additions
and
4 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
27 changes: 27 additions & 0 deletions
27
BE/error/src/main/java/com/example/demo/image/ImageController.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,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
54
BE/error/src/main/java/com/example/demo/image/ImageService.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,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
33
BE/error/src/main/java/com/example/demo/image/StorageConfig.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,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(); | ||
} | ||
|
||
} |
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