【问题标题】:Amazon Lambda@edge response亚马逊 Lambda@edge 响应
【发布时间】:2018-05-07 11:42:17
【问题描述】:

我正在使用 AWS S3 和 cloudfront 来托管一个静态站点。当用户代理是whatsapp或其他时,我需要捕捉......

所以,我正在使用lambda@edge 与带有跳跳虎的云端相关联的函数。那么,Tigger 有 4 个选项(viewer_requestorigin_requestorigin_responseviewer_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


【解决方案1】:

我遇到了同样的问题,我通过分成 2 个 lambda 函数来解决我的问题,一个处理 request,另一个处理 response

  1. 在 CloudFront 行为设置中,为一个进程 request 选择 viewer request。并为response 的一个进程选择origin response
  2. 对于request 之一,请记住@michael-sqlbot 提到的return callback(null, request)

注意:

  • viewer request 包含来自请求标头的原始用户代理,例如"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
  • origin request 包含 CloudFront 处理的标头,例如"Amazon CloudFront"

希望对你有帮助,如有错误请指正。

参考:

【讨论】:

    【解决方案2】:

    要允许 CloudFront 从查看器请求或源请求触发器继续处理请求:

    return callback(null,request);
    

    【讨论】:

    • 没有工作...“Lambda 函数返回无效 json:json 输出必须是对象类型”。
    • 如果这是一个查看器请求或源请求触发器,那么这是正确的解决方案,假设request 等效于event.Records[0].cf.requestExample 1, Example 2, Docs.
    • 使用 apigateway 我们可以更改响应内容类型并强制成为 json...但在这种情况下我们不会通过 api 网关...
    • 此错误是指来自 Lambda 的序列化 JSON 响应(它始终是 JSON 响应,与您的有效负载内容无关)。这意味着您返回的不是 JavaScript 对象。如果您坚持使用已弃用的 context 方法,请不要包含前导 null。 context.succeed(request) 等价于 callback(null,request)
    猜你喜欢
    • 2014-04-08
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    相关资源
    最近更新 更多