我在 SNS 测试帐户中实施了订阅过滤策略,仅将 S3 中特定子文件夹中的数据发送到我的 SQS 队列。这完全符合预期
{
"Records": {
"s3": {
"object": {
"key": [
{
"prefix": "test/test1"
}
]
}
},
"eventName": [
{
"prefix": "ObjectCreated:"
}
]
}
}
但是,我尝试使用 CDK 实施相同的订阅策略,但它给了我以下错误。
CDK代码:
topic.addSubscription(new subs.SqsSubscription(queue, {
filterPolicyWithMessageBody: {
"Records": {
"s3": {
"object": {
"key": [
{
"prefix": "test/test1"
}
]
}
},
"eventName": [
{
"prefix": "ObjectCreated:"
}
]
}
}
}))
错误:
Type '{ s3: { object: { key: { prefix: string; }[]; }; }; eventName: { prefix: string; }[]; }' is not assignable to type 'FilterOrPolicy'.
Object literal may only specify known properties, and 's3' does not exist in type 'FilterOrPolicy'.
不幸的是,您需要使用 FilterOrPolicy 类。我花了一段时间才自己弄清楚。我转换了你的示例(未测试)...但是尝试一下:
const imageSqsSubscription = new SqsSubscription(imageConversionQueue, {
filterPolicyWithMessageBody: {
Records: FilterOrPolicy.policy({
s3: FilterOrPolicy.policy({
object: FilterOrPolicy.policy({
key: FilterOrPolicy.filter(
SubscriptionFilter.stringFilter({
matchPrefixes: ["test/test1"],
})
),
}),
}),
eventName: FilterOrPolicy.filter(
SubscriptionFilter.stringFilter({
allowlist: ["ObjectCreated:"],
})
),
}),
},
});