// Find and delete images
const images = await Images.findAll({ where: { postId: post.postId } });
const imagePath = new URL(images).pathname
console.log('imagePath', imagePath)
const s3DeletePromises = imagePath.map((image) => {
return deleteImage(image)
})
await Promise.all(s3DeletePromises)
images 변수가 Sequelize 인스턴스 배열이므로 URL로 바로 반환할 수 없다.
대신 각 인스턴스에서 이미지 경로를 추출하고, 해당 경로를 사용하여 S3에서 이미지를 삭제하는 것이
올바른 접근 방법이다.
const images = await Images.findAll({ where: { postId: post.postId } });
const s3DeletePromises = images.map((image) => {
const imagePath = new URL(image.dataValues.url).pathname.substr(1)
return deleteS3(imagePath)
})
await Promise.all(s3DeletePromises)