AWSLambda和ALB配置中为什么有1MB的有效载荷限制?
创始人
2024-09-26 06:01:10
0

AWS Lambda和ALB之间具有1MB有效载荷限制。这意味着当您使用ALB触发Lambda函数时,有效载荷的大小不能超过1MB。这是因为ALB不能将超过1MB的有效负载发送到Lambda,因此必须在使用ALB作为触发器时遵守此限制。

为了解决此问题,您可以将有效负载拆分为小块,处理每个块并将它们组合起来。您可以使用以下示例代码来解决此问题:

import json import boto3

def lambda_handler(event, context): # Get the S3 and ALB objects from the event s3 = boto3.client('s3') alb = boto3.client('elbv2')

# Get the object key and bucket from the S3 event
bucket_name = event['Records'][0]['s3']['bucket']['name']
object_key = event['Records'][0]['s3']['object']['key']

# Get the object size
object_info = s3.head_object(Bucket=bucket_name, Key=object_key)

# Get the object data in chunks
chunk_size = 1024 * 1024  # 1MB
start_byte = 0
end_byte = chunk_size
chunks = []

while start_byte < object_info['ContentLength']:
    obj = s3.get_object(Bucket=bucket_name, Key=object_key, Range=f'bytes={start_byte}-{end_byte - 1}')
    chunks.append(obj['Body'].read())
    start_byte = end_byte
    end_byte += chunk_size

# Concatenate the chunks and return the data
data = b''.join(chunks)
return {
    'statusCode': 200,
    'body': data.decode('utf-8')
}

在这个示例中,我们的Lambda函数触发ALB并从S3下载对象。我们使用S3 API来获取对象的大小并将其分割成1MB的块。我们使用boto3 APIs从S3中获取数据块,然后使用b''join()将它们组合起来。最后,我们以文本格式返回数据。

注意:当使用ALB作为Lambda函数触发器时,请注意有效负载的大小限制。如果超过1MB,我们建议您使用其他Lambda触发器,例如API网关。

相关内容

热门资讯

据文件显示!wepoker透视... 据文件显示!wepoker透视脚本苹果版,如何下载wpk透视版(透视)详细教程(有挂方法)-哔哩哔哩...
今天下午!智星德州插件2024... 今天下午!智星德州插件2024最新版,wepoker透视脚本网页(透视)推荐教程(新版有挂)-哔哩哔...
现有说明如下!aapoker辅... 现有说明如下!aapoker辅助工具安全吗,wepoker分析(透视)科普教程(新版有挂)-哔哩哔哩...
反观!wpk模拟器是什么,we... 反观!wpk模拟器是什么,wepoker辅助软件视频(透视)有挂教程(有挂详细)-哔哩哔哩1、wep...
事发当天!wepoker高级辅... 事发当天!wepoker高级辅助,cloudpoker辅助(透视)了解教程(有挂详细)-哔哩哔哩1、...
透视有挂!来玩app破解版,哈... 透视有挂!来玩app破解版,哈糖大菠萝能开挂吗(透视)曝光教程(有挂方法)-哔哩哔哩1、这是跨平台的...
据统计!wepoker怎么破解... 据统计!wepoker怎么破解游戏,wepoker正确养号方法(透视)必备教程(揭秘有挂)-哔哩哔哩...
据报道!wepoker插件程序... 据报道!wepoker插件程序激活码,aapoker透视怎么用(透视)曝光教程(有挂工具)-哔哩哔哩...
透视ai!wepoker私人局... 透视ai!wepoker私人局开挂视频,sohoo poker辅助(透视)了解教程(有挂方法)-哔哩...
辅助透视!wepokerplu... 辅助透视!wepokerplus到底是挂了吗,werplan免费挂下载(透视)推荐教程(发现有挂)-...