Is it reasonable to be concerned an SES object won't be available in S3?

时间:2019-04-16 22:20:31

标签: amazon-s3 aws-lambda boto3 amazon-ses

I've setup SES Rule in the following way:

Actions:

1) S3: Saves SES object to an S3 bucket

2) Lambda: Triggers my lambda function for email processing

In my testing, I've always been able to retrieve my SES object from the bucket using the messageID in the very first line of code. I'm then able to parse and read it without issue.

My question is, is it reasonable to be concerned that the SES object may not always be immediately available? I'm considering adding error handling incase the object isn't there. Basically to wait 1/2 a second and try again until the lambda times out. But I don't want to complicate the code if this is not a reasonable concern, handled by boto3, ect. Thoughts?

1 个答案:

答案 0 :(得分:0)

对于您而言,最好仅使用配置了SNS主题通知的一个 S3操作,并使Lambda订阅该主题。

您的Lambda将收到一条SNS事件,其中包含消息中的字符串化SES事件:

{
    "Records": [
        {
            "EventSource": "aws:sns",
            "EventVersion": "1.0",
            ...
            "Sns": {
                "Type": "Notification",
                "MessageId": "...",
                "TopicArn": "...",
                "Subject": "Amazon SES Email Receipt Notification",
                "Message": "<STRINGIFIED SES EVENT>",
                ...
            }
        }
    ]
}

如果您解析Message,则会得到类似以下内容的信息:

{
    "notificationType": "Received",
    "mail": {
        "timestamp": "...",
        "source": "...",
        "messageId": "...",
        "destination": [
           ...
        ],
        "headersTruncated": false,
        "headers": [
            ...
        ],
        "commonHeaders": {
            "returnPath": "...",
            "from": [
                "..."
            ],
            "date": "...",
            "to": [
                ...
            ],
            "messageId": "...",
            "subject": "..."
        }
    },
    "receipt": {
        ...
        "action": {
            "type": "S3",
            "topicArn": "...",
            "bucketName": "<YOUR_BUCKET>",
            "objectKey": "<YOUR_OBJECT_KEY>"
        }
    }
}

,您可以在其中找到对存储桶(receipt.action.bucketNamereceipt.action.objectKey)中上载对象的确切引用。

使用此设置,可以合理地考虑到触发Lambda时该对象可用。