AWS CDK - 嵌套 SNS 订阅过滤策略

问题描述 投票:0回答:1

我在 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'.
amazon-sqs aws-cdk amazon-sns aws-cdk-typescript
1个回答
0
投票

不幸的是,您需要使用 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:"],
            })
          ),
        }),
      },
    });
© www.soinside.com 2019 - 2024. All rights reserved.