将 CORS 与 AWS SAM 结合使用 [英] Using CORS with AWS SAM

查看:29
本文介绍了将 CORS 与 AWS SAM 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些由 API 网关事件触发的 Lambda 函数.

I've created a few Lambda functions that get triggered by API-Gateway events.

现在我想为这些端点启用 CORS,但它似乎不起作用.在 AWS SAM 的最后几个版本中,添加或更新了 CORS 功能,但我仍然无法使其正常工作.

Now I want to enable CORS for these endpoints, but it doesn't seem to work. In the last few versions of AWS SAM the CORS functionality was added or updated, but I still can't get it to work.

这是我试过的:

Gobals:
  Api:
    Cors: "'*'"

推荐答案

您也可以在 lambda 处理程序中指定这些(Python 示例):

You can also specify these in your lambda handlers (Python example):

return {
        'statusCode': status_code,
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*'
        },
        'body': status_message if status_code == 200 else 'Failure'
    }

这篇关于将 CORS 与 AWS SAM 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆