简体   繁体   English

与 API 网关相比,AWS Lambda function URL 返回不同的内容长度值

[英]AWS Lambda function URL returns a different content-length value compared to API Gateway

I have a lambda that loads an S3 file and returns it as a Base64:我有一个 lambda 加载 S3 文件并将其作为 Base64 返回:

return {
    "isBase64Encoded": True,
    'statusCode': 200,
    'body': b64encode(...).decode('utf-8'),
    'headers': {
        'Content-Type': 'application/octet-stream',
    }
}

I created an API Gateway and when I call it I get:我创建了一个 API 网关,当我调用它时,我得到:

<Response [200]>
Content-Type application/octet-stream
Content-Length 998356
apparent_encoding ascii

But calling the function URL returns:但是调用 function URL 返回:

<Response [200]>
Content-Type application/octet-stream
Content-Length 748765
apparent_encoding None

The correct content-length header value is the one API gateway is returning however the header is different when using the function URL even though they are both linked to the same Lambda function version & are invoking the same exact code.正确的content-length header 值是 API 网关返回的值,但是当使用 function URL 时 header 是不同的,即使它们都链接到相同的 Lambda 8835809888 Lambda 883580988

What is the issue?问题是什么?

When isBase64Encoded is set to true , the Lambda function is reversing the encoding to obtain the original data.isBase64Encoded设置为true时, Lambda function 正在反转编码以获得原始数据。 This is why you're getting a lower content-length value.这就是您获得较低content-length值的原因。

However, b64encode(...).decode('utf-8') encodes your body content & then decodes it back to non-base64-encoded content.但是, b64encode(...).decode('utf-8')您的body内容进行编码,然后将其解码回非 base64 编码的内容。 You're not actually returning base64-encoded data but you're specifying that you are.你实际上并没有返回 base64 编码的数据,但你指定你是。

Set isBase64Encoded to false and you should receive the correct full amount of data - as to why it works with API Gateway, it probably does an internal check, realises that you're not actually returning base-64 encoded data and returns the data as is.isBase64Encoded设置为false ,您应该会收到正确的全部数据 - 至于为什么它与 API 网关一起工作,它可能会进行内部检查,意识到您实际上并没有返回 base-64 编码数据并按原样返回数据.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM