Skip to content

Commit

Permalink
feat: S3 오브젝트 벌크 삭제 시 1000건 단위로 삭제되도록 변경 (#153)
Browse files Browse the repository at this point in the history
* feat: List 파티셔닝을 위한 Guava 라이브러리 추가

mvn: https://mvnrepository.com/artifact/com.google.guava/guava/32.1.3-jre

* feat: S3 오브젝트 삭제 시, 1000건 단위로 삭제하도록 기능 구현

---------

Co-authored-by: 김태현 <[email protected]>
  • Loading branch information
kth990303 and kth990303-woowahan authored Jan 14, 2024
1 parent 9684d78 commit 22d0115
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
implementation 'com.slack.api:slack-api-client:1.29.0'
implementation platform("org.springframework.cloud:spring-cloud-dependencies:2021.0.5")
implementation "org.springframework.cloud:spring-cloud-starter-openfeign"
implementation 'com.google.guava:guava:32.1.3-jre'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/mocacong/server/support/AwsS3Uploader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package mocacong.server.support;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
import com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion;
import com.amazonaws.services.s3.model.DeleteObjectsResult;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
Expand All @@ -23,6 +29,8 @@
public class AwsS3Uploader {

private static final String S3_BUCKET_DIRECTORY_NAME = "static";
private static final int IMAGE_CHUNK_SIZE = 1000;


private final AmazonS3Client amazonS3Client;

Expand All @@ -36,7 +44,8 @@ public String uploadImage(MultipartFile multipartFile) {
objectMetadata.setContentType(multipartFile.getContentType());
objectMetadata.setContentLength(multipartFile.getSize());

String fileName = S3_BUCKET_DIRECTORY_NAME + "/" + UUID.randomUUID() + "." + multipartFile.getOriginalFilename();
String fileName =
S3_BUCKET_DIRECTORY_NAME + "/" + UUID.randomUUID() + "." + multipartFile.getOriginalFilename();

try (InputStream inputStream = multipartFile.getInputStream()) {
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata)
Expand All @@ -58,10 +67,15 @@ private void checkInvalidUploadFile(MultipartFile multipartFile) {
@EventListener
public void deleteImages(DeleteNotUsedImagesEvent event) {
List<String> imgUrls = event.getImgUrls();
Lists.partition(imgUrls, IMAGE_CHUNK_SIZE)
.forEach(this::deleteImagesByChunk);
}

private void deleteImagesByChunk(List<String> imgUrls) {
List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>();
for (String imgUrl : imgUrls) {
String fileName = S3_BUCKET_DIRECTORY_NAME + imgUrl.substring(imgUrl.lastIndexOf("/"));
keys.add(new DeleteObjectsRequest.KeyVersion(fileName));
keys.add(new KeyVersion(fileName));
}
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket).withKeys(keys);
DeleteObjectsResult result = amazonS3Client.deleteObjects(deleteObjectsRequest);
Expand Down

0 comments on commit 22d0115

Please sign in to comment.