Skip to content

Commit

Permalink
hotfix: S3 버킷 내 오브젝트 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin0o0 committed Nov 14, 2023
1 parent a814b00 commit ae85f90
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class LearningMaterial extends BaseEntity {
@Enumerated(EnumType.STRING)
private MediaFileType mediaFileType;

@Column(unique = true)
@Column
private String fileName;

@Column(unique = true)
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/com/teamh/khumon/util/AmazonS3Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

Expand All @@ -33,7 +36,7 @@ public String uploadS3Object(String username, MultipartFile object, Long learnin
String filename = username + File.separator + learningId + File.separator + generateFileName;
log.info(filename);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(object.getContentType());
objectMetadata.setContentType(object.getContentType() + ";charset=utf-8");
objectMetadata.setContentEncoding("UTF-8");
objectMetadata.setContentLength(object.getInputStream().available());

Expand All @@ -44,11 +47,21 @@ public String uploadS3Object(String username, MultipartFile object, Long learnin
}

public void deleteFile(String uploadFilePath) {
List<String> splitPath = Arrays.stream(uploadFilePath.split("/")).toList().subList(3, 6);
String oAuth2Id = splitPath.get(0);
String postId = splitPath.get(1);
String fileName = splitPath.get(2);
String s3Key = oAuth2Id + "/" + postId + "/" + fileName;
log.info(s3Key);

try {
boolean isObjectExist = amazonS3Client.doesObjectExist(bucket, uploadFilePath);
boolean isObjectExist = amazonS3Client.doesObjectExist(bucket, s3Key);
if (isObjectExist) {
amazonS3Client.deleteObject(bucket, uploadFilePath);
throw new Exception("예외 발생");
log.info("존재함");
amazonS3Client.deleteObject(bucket, s3Key);
}
else{
log.info("존재 안함");
}
} catch (Exception e) {
log.info(e.getMessage());
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/teamh/khumon/util/MediaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,15 @@ public String postToLLMforVideo(MultipartFile multipartFile) throws Exception {

HttpEntity<MultiValueMap<String, Object>> requestHttpEntity =
new HttpEntity<>(body, httpHeaders);
String response = null;
try {
response = restTemplate.postForObject(postUrl, requestHttpEntity, String.class);
log.info(response);
deleteMediaFile(destinationPathString);

String response = restTemplate.postForObject(postUrl, requestHttpEntity, String.class);
log.info(response);
deleteMediaFile(destinationPathString);
}catch (Exception E){
log.info(E.getMessage());
}
return response;
}

Expand Down

0 comments on commit ae85f90

Please sign in to comment.