You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从SQS队列接收SNS消息时,Lambda应使用何种事件类型?

正确的Lambda事件类型:SQSEvent

你的问题核心在于触发Lambda的是SQS队列,而非直接的SNS主题,所以Lambda收到的是SQS标准事件格式,而非SNS直接触发的SNSEvent。

正确的函数签名

把参数换成SQSEvent:

public string FunctionHandler(SQSEvent sqsEvent, ILambdaContext context)

如何提取SNS消息内容

SQS的每条消息体(Record.Body)才是你看到的SNS Notification格式。你可以用Amazon.Lambda.SNSEvents包中的SNSEvent.SNSNotification类来反序列化,或者自定义一个匹配结构的类。

示例代码:

using Amazon.Lambda.SQSEvents;
using Amazon.Lambda.SNSEvents;
using System.Text.Json;

public string FunctionHandler(SQSEvent sqsEvent, ILambdaContext context)
{
    foreach (var record in sqsEvent.Records)
    {
        // 反序列化SQS消息体为SNS通知对象
        var snsNotification = JsonSerializer.Deserialize<SNSEvent.SNSNotification>(record.Body);
        
        // 这里可以处理SNS消息内容,比如获取Message、TopicArn等
        context.Logger.LogInformation($"收到SNS消息: {snsNotification.Message}");
    }
    return "处理完成";
}

如果不想依赖Amazon.Lambda.SNSEvents包,也可以自定义一个和SNS通知结构匹配的类:

public class SnsNotification
{
    public string Type { get; set; }
    public string MessageId { get; set; }
    public string TopicArn { get; set; }
    public string Subject { get; set; }
    public string Message { get; set; }
    public string Timestamp { get; set; }
    public string SignatureVersion { get; set; }
    public string Signature { get; set; }
    public string SigningCertURL { get; set; }
    public string UnsubscribeURL { get; set; }
}

之后用这个类来反序列化record.Body即可。

内容的提问来源于stack exchange,提问作者kbd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 01:37:27