告诉Lambda @ Edge函数进行重定向的代码是什么?

问题描述 投票:3回答:1

所以,为了清楚起见,我花了几个小时搜索一些东西,但这些都没有。这不是一个“低功耗的帖子”。

这是我一直在尝试的代码示例。它不起作用。也没有做像response.headers=[{Location:"foo"}]response.headers=[{location:"foo"}]这样的反应或我试过的其他八种方式。

exports.handler = (event, context, callback) => {
    if(request.uri === "/") {
    var response = {
        statusCode: 301,
        headers: {
            "location" : [{
                key: "Location",
                value: "foo"
            }]
        },
        body: null
    };
    callback(null, response);
}

我试过以下链接:

amazon-web-services aws-lambda
1个回答
4
投票

您在问题中提到了此示例的链接;它应该与Lambda代理集成一起使用:

'use strict';

exports.handler = function(event, context, callback) {
var response = {
    statusCode: 301,
    headers: {
        "Location" : "http://example.com"
    },
    body: null
};
callback(null, response);
};

来源:http://blog.ryangreen.ca/2016/01/04/how-to-http-redirects-with-api-gateway-and-lambda/

Update:

另外,尝试使用this page of example functions中的这个例子:

'use strict';

exports.handler = (event, context, callback) => {
/*
 * Generate HTTP redirect response with 302 status code and Location header.
 */
const response = {
    status: '302',
    statusDescription: 'Found',
    headers: {
        location: [{
            key: 'Location',
            value: 'http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html',
        }],
    },
};
callback(null, response);
};
© www.soinside.com 2019 - 2024. All rights reserved.