amazon-web-services - 告诉 Lambda@Edge 函数执行重定向的代码是什么?

标签 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);
}

我试过以下链接:

最佳答案

您在问题中提到了指向此示例的链接;它应该与 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/

更新:

否则,请尝试使用 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);
};

关于amazon-web-services - 告诉 Lambda@Edge 函数执行重定向的代码是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47973704/

相关文章:

amazon-web-services - 如何为 Elastic Beanstalk 单实例配置 SSL

amazon-web-services - 在 aws ubuntu 服务器的 chmod 777/home 上使用 chmod,现在每个 ubuntu 服务器都无法登录

amazon-web-services - 归档到亚马逊 AWS 的冰川存储并从中检索

amazon-web-services - 如何在 AWS 中的特定时间触发 Lambda 函数?

python - 在 python AWS lambda 中使用 aws 加密 SDK

node.js - 访问拒绝异常 : User is not authorized to perform: lambda:InvokeFunction

api - 如何在生产中为基于微服务的应用程序部署多个版本的应用程序

amazon-web-services - 初始加载后 AWS Fargate 502 网关错误

aws-lambda - 在 SAM json 中将授权者添加到 api 网关

Python boto3 SNS 电子邮件格式(每个字符串换行)