Lambda函数通过SNS接收空的S3事件对象
我试图使用携带S3事件有效负载的SNS事件调用Lambda函数(即S3 Put - >触发发布到SNS主题的事件 - >传递给订阅的Lambda函数),但它似乎是我拥有的唯一方法能够获得实际的S3事件信息是作为JsonNode访问它,我知道必须有更好的(例如反序列化)。
由于我在这里找到的评论,我真的认为我可以让我的Lambda函数接受S3EventNotification:
https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-s3/src/main/java/com/amazonaws/services/s3/event/S3EventNotification.java
表示发送到SQS,SNS或Lambda的强类型S3 EventNotification项的助手类。
那么,我如何才能将S3EventNotification作为POJO接收?
以下是我尝试过的各种方法:
public class LambdaFunction implements RequestHandler<S3EventNotification, Object>{
@Override
public Object handleRequest(S3EventNotification input, Context context) {
System.out.println(JsonUtil.MAPPER.writeValueAsString(input));
return null;
}
}
导致:
{
"Records": [
{
"awsRegion": null,
"eventName": null,
"eventSource": null,
"eventTime": null,
"eventVersion": null,
"requestParameters": null,
"responseElements": null,
"s3": null,
"userIdentity": null
}
]
}
我也尝试了以下(注意:JsonUtil.MAPPER只是返回一个Jackson ObjectMapper):
public class LambdaFunction {
public Object handleRequest(S3EventNotification records, Context context) throws IOException {
System.out.println(JsonUtil.MAPPER.writeValueAsString(records));
return null;
}
}
这返回与以前相同:
{
"Records": [
{
"awsRegion": null,
"eventName": null,
"eventSource": null,
"eventTime": null,
"eventVersion": null,
"requestParameters": null,
"responseElements": null,
"s3": null,
"userIdentity": null
}
]
}
我可以通过简单地接收SNSEvent来访问S3事件有效载荷,但是当我尝试将msg有效载荷反序列化为S3EventRecord或S3EventNotification时,字段中存在差异。 我真的不希望手动走下JsonNode ...
public class LambdaFunction {
public Object handleRequest(SNSEvent input, Context context) throws IOException {
System.out.println("Records: " + JsonUtil.MAPPER.writeValueAsString(input));
for (SNSEvent.SNSRecord record : input.getRecords()) {
System.out.println("Record Direct: " + record.getSNS().getMessage());
JsonNode node = JsonUtil.MAPPER.readTree(record.getSNS().getMessage());
JsonNode recordNode = ((ArrayNode) node.get("Records")).get(0);
System.out.println(recordNode.toString());
S3EventNotification s3events = JsonUtil.MAPPER.readValue(record.getSNS().getMessage(), new TypeReference<S3EventNotification>() {});
System.out.println(s3events == null);
}
return null;
}
这返回以下内容:
{
"eventVersion": "2.0",
"eventSource": "aws:s3",
"awsRegion": "us-east-1",
"eventTime": "2017-03-04T05:34:25.149Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "AWS:XXXXXXXXXXXXX"
},
"requestParameters": {
"sourceIPAddress": "<<IP ADDRESS>>"
},
"responseElements": {
"x-amz-request-id": "XXXXXXXX",
"x-amz-id-2": "XXXXXXXXXXXXX="
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "NotifyNewRawArticle",
"bucket": {
"name": "MYBUCKET",
"ownerIdentity": {
"principalId": "XXXXXXXXXXXXXXX"
},
"arn": "arn:aws:s3:::MYBUCKET"
},
"object": {
"key": "news/test",
"size": 0,
"eTag": "d41d8cd98f00b204e9800998ecf8427e",
"sequencer": "0058BA51E113A948C3"
}
}
}
Unrecognized field "sequencer" (class com.amazonaws.services.s3.event.S3EventNotification$S3ObjectEntity), not marked as ignorable (4 known properties: "size", "versionId", "eTag", "key"])
我依赖于aws-java-sdk-s3-1.11.77和aws-java-sdk-sns-1.11.77。
你应该处理SNSEvent而不是S3Event,因为lambda消耗你的SNS事件。 下面的代码适用于我。
public Object handleRequest(SNSEvent request, Context context) {
request.getRecords().forEach(snsRecord -> {
System.out.println("Record Direct: " +snsRecord.getSNS().getMessage());
S3EventNotification s3eventNotifcation=S3Event.parseJson(snsRecord.getSNS().getMessage());
System.out.println(s3eventNotifcation.toJson());
}
);
}
链接地址: http://www.djcxy.com/p/32253.html