AWS-通过Lambda读取SQS消息

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

以下代码是从AWS文档中复制的,但是除了队列URL定义部分之外,我的代码几乎相同。

我想以JSON格式打印出邮件正文,但似乎有些额外的东西。如何不使用子字符串就摆脱它们?

# Create SQS client
# blah blah

# Receive message from SQS queue
response = sqs.receive_message(
    QueueUrl=queue_url,
    AttributeNames=[
        'SentTimestamp'
    ],
    MaxNumberOfMessages=1,
    MessageAttributeNames=[
        'All'
    ],
    VisibilityTimeout=0,
    WaitTimeSeconds=0
)

message = response['Messages'][0]
receipt_handle = message['ReceiptHandle']

print('Received and deleted message: %s' % message)

此打印的消息具有以下格式:

START RequestId: fe107bc8-3829-4600-9bfc-df89f59b0c70 Version: $LATEST
{JSON body}
END RequestId: fe107bc8-3829-4600-9bfc-df89f59b0c70
REPORT RequestId: fe107bc8-3829-4600-9bfc-df89f59b0c70  Duration: 914.38 ms Billed Duration: 1000 ms    Memory Size: 128 MB Max Memory Used: 71 MB  Init Duration: 247.03 ms

我真正想要的只是{JSON body}。我如何摆脱其余的?

amazon-web-services aws-lambda amazon-sqs
1个回答
0
投票

很遗憾您无法删除

START RequestId: fe107bc8-3829-4600-9bfc-df89f59b0c70 Version: $LATEST
END RequestId: fe107bc8-3829-4600-9bfc-df89f59b0c70
REPORT RequestId: fe107bc8-3829-4600-9bfc-df89f59b0c70  Duration: 914.38 ms Billed Duration: 1000 ms    Memory Size: 128 MB Max Memory Used: 71 MB  Init Duration: 247.03 ms

来自CloudWatch Logs。这是lambda函数的标准打印输出行为。

但是,您可以在控制台中使用log event filters,这可以帮助您找到感兴趣的特定{JSON body}。但是,它是最基本,最快的解决方案。

也可以对日志进行更复杂的过滤,但是我认为这不是您想要的。

© www.soinside.com 2019 - 2024. All rights reserved.