2

저는 미디어 목적으로 AWS S3 서비스를 사용하고 있으며 엄지 관련 이미지에 AWS Lamda 서비스를 사용하고 있습니다.이미지가 S3 버킷 루트에서 삭제 된 경우 서브 디렉토리에서 해당 축소판을 제거하는 방법은 무엇입니까?

다음 문제가 발생했습니다. 기본 디렉토리에서 람다가 생성 한 축소판보다 미디어를 제거하는 경우 하위 디렉토리에 여전히 존재합니다.

예를 들어

: 나는 또한 "100 × 100", "1920x720"와 "최대 300x300"디렉토리에서 삭제하는 같은 파일을 필요 이상으로 나는 Node.js를 코드에서 "1513928090496_juCQtDAt6ylr.jpg"파일을 제거합니다. (동일한 파일 "1513928090496_juCQtDAt6ylr.jpg"도 포함)

자세한 내용은 enter image description here 업로드 이미지를 참조하십시오.

+0

가 도움이 될 수 있습니다, 확인하시기 바랍니다 :

enter image description here

은 그래서 다음과 같은 정책을 생성합니다. https://stackoverflow.com/questions/42715682/delete-aws-s3-object-using-nodejs-lambda-function –

답변

2

가장 쉬운 방법은 다른 람다 함수를 사용하는 것입니다. 오브젝트가 버킷에서 삭제되면 실행을 트리거 할 수 있습니다.

그런 다음 함수는 삭제 된 개체가 루트 디렉터리에 있는지 분석하고, 그렇다면 하위 폴더에서 축소판을 삭제합니다. 여기

당신이 사용할 수있는 기능입니다 : 개체 삭제에 람다를 실행하려면, 당신은 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(); 
 
    }); 
 
};

(로 이동하여 람다 -> 구성) : 그

enter image description here

은 또한 당신이 확인해야합니다 귀하의 Lamda의 역할에는 버킷에서 객체를 삭제할 수있는 정책이 있습니다. 내 경우

나는 다음과 같은 인라인 정책을 추가 :

{ 
    "Version": "2012-10-17", 
    "Statement": [ 
     { 
      "Sid": "VisualEditor0", 
      "Effect": "Allow", 
      "Action": "s3:DeleteObject", 
      "Resource": [ 
       "arn:aws:s3:::thumbnails-bucket-123", 
       "arn:aws:s3:::*/*" 
      ] 
     } 
    ] 
} 
관련 문제