问题描述
在S3 Simple event definition之后,我创建了无服务器部署
mycompany.example.com
但是在部署时,我遇到以下错误
functions:
convert:
handler: handler.convert
events:
- s3:
bucket: dev.domain.com
event: s3:ObjectCreated:*
rules:
- prefix: assets/images/
- suffix: .jpg
existing: true
- s3:
bucket: dev.domain.com
event: s3:ObjectCreated:*
rules:
- prefix: assets/images/
- suffix: .png
existing: true
知道我在这里缺少什么吗?
此外,我应该在这里使用An error occurred: ConvertCustomS31 - Failed to create resource. Configuration is ambiguously defined. Cannot have overlapping suffixes in two rules if the prefixes are overlapping for the same event type. See details in CloudWatch Log: 2020/08/25/[$LATEST]47c58153b46f4aaf9ec08eacabbac2b2.
,如果我只想为S3 PUT事件触发。我尝试了event: s3:ObjectCreated:*
,它给了我
event: s3:ObjectCreatedByPut:*
解决方法
问题在于S3不允许事件通知具有重叠的前缀或后缀。来自S3 documentation about notifications:
使用过滤器的通知配置不能定义前缀重叠,后缀重叠或前缀和后缀重叠的过滤规则。
这意味着您需要在其中一个过滤器中调整前缀(例如,使用assets/images/jpg
和assets/images/png
作为前缀),或者,如果您不想使用其他前缀,则可以只需对前缀assets/images
使用一个通知配置,并过滤Lambda函数中的后缀。这可以通过检查S3对象数据的键来完成。当然,后一个选项的缺点是无论后缀如何,始终调用Lambda函数。这可能会对您的费用产生负面影响。
我建议您通读various notification examples in the S3 documentation,也许会启发您如何处理自己的情况。
对于事件名称,如果要过滤s3:ObjectCreated:Put
个事件,则必须使用PUT
。您可以查看overview section of the S3 notification documentation的解释位置:
新的对象创建事件-Amazon S3支持多种API创建对象。您可以在仅使用特定API时请求通知(例如
s3:ObjectCreated:Put
),也可以在创建对象时使用通配符(例如s3:ObjectCreated:*
)来请求通知,而无需考虑API使用。