gpt4 book ai didi

angular - AWS Lambda@Edge Viewer 请求失败,返回 'The body is not a string, is not an object, or exceeds the maximum size'

转载 作者:行者123 更新时间:2023-12-05 08:21:53 25 4
gpt4 key购买 nike

我正在尝试创建一个 Lambda@Edge 函数来为我的 Angular SPA 应用程序返回 Open Graph HTML。我已将它安装到 CloudFrond“查看器请求”生命周期中。这个 lambda 检查用户代理,如果它是 Facebook 或 Twitter 爬虫,它返回 HTML(目前硬编码在 lambda 中用于测试)。如果请求来自任何其他用户代理,则该请求将传递到源。传递逻辑工作正常,但如果我尝试拦截并返回爬虫的 Open Graph HTML,则会出现错误。

在CloudWatch中,CloudFront报错是:

ERROR Validation error: The Lambda function returned an invalid body,body should be of object type.

在 Postman 中(通过伪造用户代理),我得到一个 502:

The Lambda function result failed validation: The body is not astring, is not an object, or exceeds the maximum size.

我用这个把我的头发拔掉了。有任何想法吗?这是我的 lambda。

'use strict';

function buildReleaseResponse( request ) {

const content = `<\!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />

<meta property="og:type" content="website" />
<meta property="og:title" content="Hello, World" />
<meta property="og:description" content="This is an Open Graph test" />
<meta property="og:image" content="https://cdn.website.com/photos/SGmzAlNmwOGpnMeijMoW9.jpg" />
<meta property="og:url" content="https://www.website.com/hello-world" />

<title>Hello, World</title>
</head>
<body>
<h1>Open Graph Test</h1>
</body>
</html>`;

return {
statusCode: 200,
statusDescription: 'OK',
headers: {
"content-type": [
{
"key": "Content-Type",
"value": "text/html; charset=utf-8"
}
]
},
body: content.toString()
};

}


exports.handler = ( event, context, callback ) => {
const { request, response } = event.Records[0].cf;

let userAgentStr = "";

if (request.headers['user-agent']) {
if (request.headers['user-agent'].length > 0) {
userAgentStr = request.headers['user-agent'][0].value;
}
}

let newResponse = null;

if ( userAgentStr.match(/facebookexternalhit|twitterbot/i) ) {
if ( request.uri.startsWith("/radio/release/") ) {
newResponse = buildReleaseResponse(request);
}
}

if ( newResponse === null ) {
console.log("Passthrough.");
callback(null, request);
}
else {
console.log("Overriding response with: " + JSON.stringify(newResponse));
callback(null, newResponse);
}
};

这是 cloudwatch (conole.log) 中显示的响应

{
"statusCode": 200,
"statusDescription": "OK",
"headers": {
"content-type": [{
"key": "Content-Type",
"value": "text/html; charset=utf-8"
}],
"cache-control": [{
"key": "Cache-Control",
"value": "max-age=100"
}]
},
"body": "<!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n \n <meta property=\"og:type\" content=\"website\" />\n <meta property=\"og:title\" content=\"Yo Dog\" />\n <meta property=\"og:description\" content=\"Song by Raf Fiol and Mike Patterson\" />\n <meta property=\"og:image\" content=\"https://cdn.kompoz.com/photos/SGmzAlNmwOGpnMeijMoW9.jpeg\" />\n <meta property=\"og:url\" content=\"https://3-www.kompoz.com/radio/release/yo_dog\" />\n \n <title>Kompoz.com</title>\n </head>\n <body>\n <h1>Yo Dog</h1>\n </body>\n </html>"
}

这是 CloudFront Lambda 配置

CloudFront Lambda configuration

最佳答案

已解决!我很尴尬地报告这个问题是由我的错字引起的。在我的响应对象中,我有:

"statusCode": 200,

但它应该是:

"status": 200,

很高兴地报告它现在正在运行。话虽如此,我希望 AWS 错误消息更好。消息“正文不是字符串,不是对象,或者超过最大大小”真的让我失望了。

关于angular - AWS Lambda@Edge Viewer 请求失败,返回 'The body is not a string, is not an object, or exceeds the maximum size',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69843453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com