가장 쉬운 방법은 다른 람다 함수를 사용하는 것입니다. 오브젝트가 버킷에서 삭제되면 실행을 트리거 할 수 있습니다.
그런 다음 함수는 삭제 된 개체가 루트 디렉터리에 있는지 분석하고, 그렇다면 하위 폴더에서 축소판을 삭제합니다. 여기
당신이 사용할 수있는 기능입니다 : 개체 삭제에 람다를 실행하려면, 당신은 AWS 콘솔에서 트리거를 설정해야
const AWS = require("aws-sdk");
const s3 = new AWS.S3();
const thumbnailFolders = ["100x100", "1920x720", "300x300"];
exports.handler = function(event, context, callback) {
const bucketName = event.Records[0].s3.bucket.name;
const deletedFileKey = event.Records[0].s3.object.key;
// If there are no forward slashes the file was in the root folder.
const wasInRootDirectory = !deletedFileKey.includes("/");
if (!wasInRootDirectory) {
// If if was not in the root foler, ignore it.
return;
}
const thumbnailsToDelete = thumbnailFolders.map(f => {
return { Key: `${f}/${deletedFileKey}` };
});
const params = {
Bucket: bucketName,
Delete: {
Objects: thumbnailsToDelete
}
};
s3.deleteObjects(params, (err, data) => {
if (err) {
console.log(err, err.stack);
callback(err);
return;
}
//Deleted successfully
callback();
});
};
(로 이동하여 람다 -> 구성) : 그
은 또한 당신이 확인해야합니다 귀하의 Lamda의 역할에는 버킷에서 객체를 삭제할 수있는 정책이 있습니다. 내 경우
나는 다음과 같은 인라인 정책을 추가 :
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "s3:DeleteObject",
"Resource": [
"arn:aws:s3:::thumbnails-bucket-123",
"arn:aws:s3:::*/*"
]
}
]
}
가 도움이 될 수 있습니다, 확인하시기 바랍니다 :
은 그래서 다음과 같은 정책을 생성합니다. https://stackoverflow.com/questions/42715682/delete-aws-s3-object-using-nodejs-lambda-function –