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

use*_*234 3 amazon-web-services aws-lambda

所以只是为了清楚起见,我花了几个小时在谷歌上搜索东西,但这些都不起作用。这不是一个“低努力的帖子”。

这是我一直在尝试的代码示例。它不起作用。做这样的响应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);
}
Run Code Online (Sandbox Code Playgroud)

我试过以下链接:

Jos*_*hee 5

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

'use strict';

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

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

更新:

否则,请尝试使用此示例函数页面中的此示例

'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);
};
Run Code Online (Sandbox Code Playgroud)