Skip to content

Commit

Permalink
Merge pull request #54 from 7th-UMC/feature/53
Browse files Browse the repository at this point in the history
Feat : 주기적으로 s3 이미지 삭제, 데이터베이스 삭제 기능 추가
  • Loading branch information
hyeonda02 authored Aug 31, 2024
2 parents 1534c27 + a14667f commit 6a8bb50
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/hsu/umc/server/ServerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import io.swagger.v3.oas.annotations.servers.Server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@OpenAPIDefinition(
servers = {
@Server(url = "/",description = "Dev Server URL")
}
)
@EnableScheduling
@SpringBootApplication
public class ServerApplication {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/hsu/umc/server/aws/s3/AmazonS3Manager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hsu.umc.server.aws.s3;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import hsu.umc.server.config.AmazonConfig;
Expand All @@ -25,6 +26,9 @@ public String uploadFile(String keyName, MultipartFile file)throws IOException {
amazonS3.putObject(new PutObjectRequest(amazonConfig.getBucket(), keyName, file.getInputStream(), metadata));
return amazonS3.getUrl(amazonConfig.getBucket(),keyName).toString();
}
public void deleteFile(String photoUrl){
amazonS3.deleteObject(new DeleteObjectRequest(amazonConfig.getBucket(),photoUrl));
}
public String generatePhotoKeyName(Uuid uuid) {
return amazonConfig.getPhotoPath() + '/' + uuid.getUuid() + ".png";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface UuidRepository extends JpaRepository<Uuid, Long> {
Uuid findByUuid(String uuidUrl);
}
4 changes: 4 additions & 0 deletions src/main/java/hsu/umc/server/service/SchedulerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package hsu.umc.server.service;

public interface SchedulerService {
}
47 changes: 47 additions & 0 deletions src/main/java/hsu/umc/server/service/SchedulerServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package hsu.umc.server.service;

import hsu.umc.server.aws.s3.AmazonS3Manager;
import hsu.umc.server.entity.Photo;
import hsu.umc.server.entity.Uuid;
import hsu.umc.server.repository.PhotoRepository;
import hsu.umc.server.repository.UuidRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class SchedulerServiceImpl implements SchedulerService{
private final PhotoRepository photoRepository;
private final UuidRepository uuidRepository;
private final AmazonS3Manager s3Manager;
@Scheduled(cron = "${cloud.aws.cron}")
@Transactional
public void deletePhoto() {
List<Photo> photoList = photoRepository.findAll();
photoList.stream()
.filter(photo -> Duration.between(photo.getCreatedAt(), LocalDateTime.now()).toHours()>=2)
.forEach(photo -> {
s3Manager.deleteFile(photo.getPhotoUrl());

String uuidUrl = extractUuidFromPhotoUrl(photo.getPhotoUrl());
Uuid uuid = uuidRepository.findByUuid(uuidUrl);

photoRepository.delete(photo);
uuidRepository.delete(uuid);
});
}
private String extractUuidFromPhotoUrl(String photoUrl){
String[] parts = photoUrl.split("/");
String uuid = parts[parts.length-1];
System.out.println(uuid);
return uuid.replace(".png","");
}

}
1 change: 1 addition & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ cloud:
static: ap-northeast-2
stack:
auto: false
cron: "0 0 */1 * * *"
credentials:
accessKey: ${aws.access.key.id}
secretKey: ${aws.secret.access.key}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cloud:
static: ap-northeast-2
stack:
auto: false
cron: "0 */2 * * * *"
credentials:
accessKey: ${aws.access.key.id}
secretKey: ${aws.secret.access.key}

0 comments on commit 6a8bb50

Please sign in to comment.