Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: S3 오브젝트 벌크 삭제 시 1000건 단위로 삭제되도록 변경 #153

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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