【发布时间】:2018-05-07 11:42:17
【问题描述】:
我正在使用 AWS S3 和 cloudfront 来托管一个静态站点。当用户代理是whatsapp或其他时,我需要捕捉......
所以,我正在使用lambda@edge 与带有跳跳虎的云端相关联的函数。那么,Tigger 有 4 个选项(viewer_request、origin_request、origin_response、viewer_response)。我正在开发一个小脚本,在viewer_request 上触发,如果用户代理是 Whatsapp,则响应将是纯 html,但如果用户代理是其他任何响应,则响应应该继续自然流动,因此云端应该响应索引。在 cloudfront 属性中配置的 html。我不能让流程继续到 index.html...
我的代码:
let content = ``;
exports.handler = (event, context, callback) => {
var response = event.Records[0].cf.response;
const request = event.Records[0].cf.request;
const headers = JSON.stringify(request.headers);
if(headers.toUpperCase().indexOf("WHATSAPP")>0) {
console.log("is whatsapp");
var html = `
<html prefix="og: http://ogp.me/ns#">
<head>
<meta property="og:url"
content="http://www.nytimes.com/2015/02/19/arts/international/when-
great-minds-dont-think-alike.html" />
<meta property="og:type" content="article" />
<meta property="og:title" content="When Great Minds Don’t Think
Alike" />
<meta property="og:description" content="How much does culture
influence creative thinking?" />
<meta property="og:image"
content="http://static01.nyt.com/images/2015/02/19/arts/international/19iht-btnumbers19A/19iht-btnumbers19A-facebookJumbo-v2.jpg" />
</head>
<body>Whatsapp</body>
</html>
`;
response = {
status: '200',
statusDescription: 'OK',
headers: {
'cache-control': [{
key: 'Cache-Control',
value: 'max-age=100'
}],
'content-type': [{
key: 'Content-Type',
value: 'text/html'
}],
'content-encoding': [{
key: 'Content-Encoding',
value: 'UTF-8'
}],
},
body: html,
};
context.succeed(response);
} else {
context.succeed(null);
}
【问题讨论】:
-
我认为您仍然需要传递
context.succeed()未修改的响应,而不仅仅是null。 -
谢谢,但没用...我试过 context.succeed(null,response);
-
嘿@criabdala 你能解决它吗?任何帮助将不胜感激
标签: amazon-web-services aws-lambda amazon-cloudfront