基于Cookie的两个来源之间的Lambda @ Edge路由

时间:2018-08-31 12:23:03

标签: aws-lambda amazon-cloudfront aws-lambda-edge

能否请我告诉我如何使用Lambda @ Edge将基于cookie的两个不同来源路由?我尝试了下面的代码,但无法正常工作。预先感谢。

 'use strict';

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

    //Setup the two different origins
    const originA = "site1.example.com";
    const originB = "site2.example.com";


    //Determine whether the user has visited before based on a cookie value
    //Grab the 'origin' cookie if it's been set before
    if (headers.cookie) {
        for (let i = 0; i < headers.cookie.length; i++) {
            if (headers.cookie[i].value.indexOf('origin=A') >= 0) {
                console.log('Origin A cookie found');
                headers['host'] = [{key: 'host',          value: originA}];
                origin.s3.domainName = originA;
                break;
            } else if (headers.cookie[i].value.indexOf('origin=B') >= 0) {
                console.log('Origin B cookie found');
                headers['host'] = [{key: 'host',          value: originB}];
                origin.s3.domainName = originB;
                break;
            }
        }
    }    

    callback(null, request);
};

1 个答案:

答案 0 :(得分:0)

您似乎没有使用s3原点。在代码中将s3更改为custom可以解决此问题。

origin.custom.domainName = originA;
origin.custom.domainName = originB;