AWS Lambda - events solely on root of S3 bucket

╄→гoц情女王★ 提交于 2019-12-24 14:15:45

问题


I'm writing an automated image resizing tool for S3 using Lambda. I'd like to create thumbnails in a subfolder of the bucket upon a PUT operation but if I put a file anywhere in the bucket the event is fired.

Can some tell me how to make the event on fire on just actions in the root of the specified bucket but no in its subfolders?


回答1:


It doesn't look like you can configure the S3 notification to fire only on certain paths, so the best option is to implement this logic in your Lambda function:

var key = event.Records[0].s3.object.key;
if (key.indexOf('/') === -1) {
    // No slash in key name, must be in root of bucket, do stuff here
}

This will work because if the object is in a subfolder and not the root, the key will have a slash in the name.




回答2:


Use a prefix filter. For example, configure your trigger with a prefix filter of uploads/. It will fire when uploads/thumb1.jpg is created but not when any_other_prefix/thumb1.jpg is created. Then, just make sure when you PUT your thumbnails you use prefix in the key.



来源:https://stackoverflow.com/questions/29809473/aws-lambda-events-solely-on-root-of-s3-bucket

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!