首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Amazon @edge回复

Amazon @edge回复
EN

Stack Overflow用户
提问于 2017-11-09 16:34:08
回答 2查看 2.1K关注 0票数 0

我使用AWS S3和cloudfront托管一个静态站点。当用户代理是whatsapp或其他..。

因此,我使用与tiggers关联的cloudfront的lambda@edge函数。然后,Tigger有4个选项(viewer_requestorigin_requestorigin_responseviewer_response)。我正在开发一个viewer_request上的小脚本,如果用户代理是Whatsapp,那么响应将是纯html,但是如果用户代理是任何其他的,响应应该继续自然流,因此cloudfront应该响应在cloudfront属性中配置的index.html。我不能继续流到index.html.

我的代码:

代码语言:javascript
复制
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);
   }
EN

回答 2

Stack Overflow用户

发布于 2018-05-07 01:05:18

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

  1. 在CloudFront行为设置中,选择viewer request for one processes request。并选择origin response作为一个进程response
  2. 对于request one,请记住@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"

希望这有帮助,如果有什么问题请纠正我。

参考资料:

票数 1
EN

Stack Overflow用户

发布于 2017-11-09 23:26:09

若要允许CloudFront从查看器请求或原产地请求触发器继续处理请求,请执行以下操作:

代码语言:javascript
复制
return callback(null,request);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47207065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档