これを行う最も簡単な方法は、別のラムダ関数を使うことです。 オブジェクトがバケットから削除されたときに実行をトリガーできます。
この関数は、削除されたオブジェクトがルートディレクトリにあるかどうかを分析し、そうであれば、サブフォルダからサムネールを削除します。ここで
はあなたが使用できる機能です:オブジェクトの削除であなたのラムダを実行するためには
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();
});
};
、あなたに行く(AWSコンソールでトリガーを設定する必要がありますラムダ - >設定):
また、あなたは必ず、あなたのことを行う必要があります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 –